Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
feat(meetings): new plugin (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
enkhtuvshinD authored Sep 26, 2023
1 parent 5faaf56 commit 1fe97b1
Show file tree
Hide file tree
Showing 56 changed files with 4,567 additions and 365 deletions.
125 changes: 64 additions & 61 deletions packages/api-plugin-template.erxes/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,74 +224,77 @@ async function startServer() {
const apolloServer = await generateApolloServer(serviceDiscovery);
await apolloServer.start();

app.use('/graphql', expressMiddleware(apolloServer, {
context: async ({ req, res }) => {
let user: any = null;

if (req.headers.user) {
if (Array.isArray(req.headers.user)) {
throw new Error(`Multiple user headers`);
app.use(
'/graphql',
expressMiddleware(apolloServer, {
context: async ({ req, res }) => {
let user: any = null;

if (req.headers.user) {
if (Array.isArray(req.headers.user)) {
throw new Error(`Multiple user headers`);
}
const userJson = Buffer.from(req.headers.user, 'base64').toString(
'utf-8'
);
user = JSON.parse(userJson);
}
const userJson = Buffer.from(req.headers.user, 'base64').toString(
'utf-8'
);
user = JSON.parse(userJson);
}

let context;

if (USE_BRAND_RESTRICTIONS !== 'true') {
context = {
brandIdSelector: {},
singleBrandIdSelector: {},
userBrandIdsSelector: {},
docModifier: doc => doc,
commonQuerySelector: {},
user,
res
};
} else {
let scopeBrandIds = JSON.parse(req.cookies.scopeBrandIds || '[]');
let brandIds = [];
let brandIdSelector = {};
let commonQuerySelector = {};
let commonQuerySelectorElk;
let userBrandIdsSelector = {};
let singleBrandIdSelector = {};

if (user) {
brandIds = user.brandIds || [];

if (scopeBrandIds.length === 0) {
scopeBrandIds = brandIds;
let context;

if (USE_BRAND_RESTRICTIONS !== 'true') {
context = {
brandIdSelector: {},
singleBrandIdSelector: {},
userBrandIdsSelector: {},
docModifier: doc => doc,
commonQuerySelector: {},
user,
res
};
} else {
let scopeBrandIds = JSON.parse(req.cookies.scopeBrandIds || '[]');
let brandIds = [];
let brandIdSelector = {};
let commonQuerySelector = {};
let commonQuerySelectorElk;
let userBrandIdsSelector = {};
let singleBrandIdSelector = {};

if (user) {
brandIds = user.brandIds || [];

if (scopeBrandIds.length === 0) {
scopeBrandIds = brandIds;
}

if (!user.isOwner && scopeBrandIds.length > 0) {
brandIdSelector = { _id: { $in: scopeBrandIds } };
commonQuerySelector = { scopeBrandIds: { $in: scopeBrandIds } };
commonQuerySelectorElk = { terms: { scopeBrandIds } };
userBrandIdsSelector = { brandIds: { $in: scopeBrandIds } };
singleBrandIdSelector = { brandId: { $in: scopeBrandIds } };
}
}

if (!user.isOwner && scopeBrandIds.length > 0) {
brandIdSelector = { _id: { $in: scopeBrandIds } };
commonQuerySelector = { scopeBrandIds: { $in: scopeBrandIds } };
commonQuerySelectorElk = { terms: { scopeBrandIds } };
userBrandIdsSelector = { brandIds: { $in: scopeBrandIds } };
singleBrandIdSelector = { brandId: { $in: scopeBrandIds } };
}
context = {
brandIdSelector,
singleBrandIdSelector,
docModifier: doc => ({ ...doc, scopeBrandIds }),
commonQuerySelector,
commonQuerySelectorElk,
userBrandIdsSelector,
user,
res
};
}

context = {
brandIdSelector,
singleBrandIdSelector,
docModifier: doc => ({ ...doc, scopeBrandIds }),
commonQuerySelector,
commonQuerySelectorElk,
userBrandIdsSelector,
user,
res
};
}

await configs.apolloServerContext(context, req, res);
await configs.apolloServerContext(context, req, res);

return context;
}
}));
return context;
}
})
);

await new Promise<void>(resolve =>
httpServer.listen({ port: PORT }, resolve)
Expand Down
6 changes: 4 additions & 2 deletions packages/api-utils/src/random.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { customAlphabet } from 'nanoid';

export const randomAlphanumeric = customAlphabet('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
export const randomLowercase = customAlphabet('abcdefghijklmnopqrstuvwxyz');
export const randomAlphanumeric = customAlphabet(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
);
export const randomLowercase = customAlphabet('abcdefghijklmnopqrstuvwxyz');
3 changes: 1 addition & 2 deletions packages/api-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,4 @@ export interface IAttachment {
type: string;
}


// trigger
// trigger
131 changes: 67 additions & 64 deletions packages/core/src/apolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,81 @@ export const initApolloServer = async (app, httpServer) => {
}
]),
// for graceful shutdowns
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })]
});

await apolloServer.start();

app.use('/graphql', expressMiddleware(apolloServer, {
context: async ({ req, res }) => {
const subdomain = getSubdomain(req);
const models = await generateModels(subdomain);
app.use(
'/graphql',
expressMiddleware(apolloServer, {
context: async ({ req, res }) => {
const subdomain = getSubdomain(req);
const models = await generateModels(subdomain);

let user: any = null;
let user: any = null;

if (req.headers.user) {
const userJson = Buffer.from(req.headers.user, 'base64').toString(
'utf-8'
);
user = JSON.parse(userJson);
}
if (req.headers.user) {
const userJson = Buffer.from(req.headers.user, 'base64').toString(
'utf-8'
);
user = JSON.parse(userJson);
}

const dataLoaders: IDataLoaders = generateAllDataLoaders(models);

const requestInfo = {
secure: req.secure,
cookies: req.cookies
};

const dataLoaders: IDataLoaders = generateAllDataLoaders(models);
if (USE_BRAND_RESTRICTIONS !== 'true') {
return {
brandIdSelector: {},
singleBrandIdSelector: {},
userBrandIdsSelector: {},
docModifier: doc => doc,
commonQuerySelector: {},
user,
res,
requestInfo,
dataLoaders,
subdomain,
models
};
}

const requestInfo = {
secure: req.secure,
cookies: req.cookies
};
let scopeBrandIds = JSON.parse(req.cookies.scopeBrandIds || '[]');
let brandIds = [];
let brandIdSelector = {};
let commonQuerySelector = {};
let commonQuerySelectorElk;
let userBrandIdsSelector = {};
let singleBrandIdSelector = {};

if (user) {
brandIds = user.brandIds || [];

if (scopeBrandIds.length === 0) {
scopeBrandIds = brandIds;
}

if (!user.isOwner && scopeBrandIds.length > 0) {
brandIdSelector = { _id: { $in: scopeBrandIds } };
commonQuerySelector = { scopeBrandIds: { $in: scopeBrandIds } };
commonQuerySelectorElk = { terms: { scopeBrandIds } };
userBrandIdsSelector = { brandIds: { $in: scopeBrandIds } };
singleBrandIdSelector = { brandId: { $in: scopeBrandIds } };
}
}

if (USE_BRAND_RESTRICTIONS !== 'true') {
return {
brandIdSelector: {},
singleBrandIdSelector: {},
userBrandIdsSelector: {},
docModifier: doc => doc,
commonQuerySelector: {},
brandIdSelector,
singleBrandIdSelector,
docModifier: doc => ({ ...doc, scopeBrandIds }),
commonQuerySelector,
commonQuerySelectorElk,
userBrandIdsSelector,
user,
res,
requestInfo,
Expand All @@ -79,47 +121,8 @@ export const initApolloServer = async (app, httpServer) => {
models
};
}

let scopeBrandIds = JSON.parse(req.cookies.scopeBrandIds || '[]');
let brandIds = [];
let brandIdSelector = {};
let commonQuerySelector = {};
let commonQuerySelectorElk;
let userBrandIdsSelector = {};
let singleBrandIdSelector = {};

if (user) {
brandIds = user.brandIds || [];

if (scopeBrandIds.length === 0) {
scopeBrandIds = brandIds;
}

if (!user.isOwner && scopeBrandIds.length > 0) {
brandIdSelector = { _id: { $in: scopeBrandIds } };
commonQuerySelector = { scopeBrandIds: { $in: scopeBrandIds } };
commonQuerySelectorElk = { terms: { scopeBrandIds } };
userBrandIdsSelector = { brandIds: { $in: scopeBrandIds } };
singleBrandIdSelector = { brandId: { $in: scopeBrandIds } };
}
}

return {
brandIdSelector,
singleBrandIdSelector,
docModifier: doc => ({ ...doc, scopeBrandIds }),
commonQuerySelector,
commonQuerySelectorElk,
userBrandIdsSelector,
user,
res,
requestInfo,
dataLoaders,
subdomain,
models
};
}
}));
})
);

return apolloServer;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/db/models/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export const getUniqueValue = async (
defaultValue?: string
) => {
const getRandomValue = (type: string) =>
type === 'email' ? faker.internet.email().toLowerCase() : randomAlphanumeric();
type === 'email'
? faker.internet.email().toLowerCase()
: randomAlphanumeric();

let uniqueValue = defaultValue || getRandomValue(fieldName);

Expand Down
8 changes: 6 additions & 2 deletions packages/gateway/src/apollo-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ const startRouter = async (

const devOptions = ['--dev', '--hot-reload'];

console.log("-------------------------------routerConfigPath--------------------------------")
console.log(
'-------------------------------routerConfigPath--------------------------------'
);
console.log([
...(NODE_ENV === 'development' ? devOptions : []),
'--log',
Expand All @@ -116,7 +118,9 @@ const startRouter = async (
`--config`,
routerConfigPath
]);
console.log("----------------------------------------------------------------------")
console.log(
'----------------------------------------------------------------------'
);

const routerProcess = spawn(
routerPath,
Expand Down
4 changes: 1 addition & 3 deletions packages/gateway/src/subscription/SubscriptionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ export default class SubscriptionResolver {
}

private async query(graphqlRequest: GraphQLRequest): Promise<FetchResult> {
const response = await toPromise(
execute(this.apolloLink, graphqlRequest)
);
const response = await toPromise(execute(this.apolloLink, graphqlRequest));
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { nanoid } from 'nanoid';
/*
* Mongoose field options wrapper
*/
export const field = (options) => {
export const field = options => {
const { pkey, type, optional } = options;

if (type === String && !pkey && !optional) {
Expand All @@ -19,7 +19,7 @@ export const field = (options) => {
return options;
};

export const schemaWrapper = (schema) => {
export const schemaWrapper = schema => {
schema.add({ scopeBrandIds: [String] });

return schema;
Expand All @@ -33,7 +33,7 @@ const hookList = [
'updateMany',
'deleteOne',
'deleteMany',
'findOneAndUpdate',
'findOneAndUpdate'
];

export const schemaHooksWrapper = (schema, cacheKey: string) => {
Expand Down
Loading

0 comments on commit 1fe97b1

Please sign in to comment.