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

Commit

Permalink
feat(integration): archive
Browse files Browse the repository at this point in the history
close #624
  • Loading branch information
Buyantogtokh authored and batamar committed Nov 14, 2019
1 parent af7573a commit 0f0ae7a
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/data/modules/coc/companies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const sortBuilder = (params: IListArgs): TSortBuilder => {
* Brand filter
*/
export const brandFilter = async (brandId: string): Promise<IBrandFilter> => {
const integrations = await Integrations.find({ brandId }, { _id: 1 });
const integrations = await Integrations.findIntegrations({ brandId }, { _id: 1 });
const integrationIds = integrations.map(i => i._id);

const customers = await Customers.find({ integrationId: { $in: integrationIds } }, { companyIds: 1 });
Expand Down
22 changes: 14 additions & 8 deletions src/data/modules/coc/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ export class Builder {
this.params = params;
}

public defaultFilters(): { status: {}; profileScore: { $gt: number } } {
public async defaultFilters(): Promise<any> {
const activeIntegrations = await Integrations.findIntegrations({}, { _id: 1 });

return {
status: { $ne: STATUSES.DELETED },
profileScore: { $gt: 0 },
$or: [
{
integrationId: { $in: [null, undefined, ''] },
},
{ integrationId: { $in: activeIntegrations.map(integration => integration._id) } },
],
};
}

Expand All @@ -80,7 +88,7 @@ export class Builder {
const brands = await Brands.find({});

for (const brand of brands) {
const integrations = await Integrations.find({ brandId: brand._id });
const integrations = await Integrations.findIntegrations({ brandId: brand._id });

const integrationIds = integrations.map(integration => integration._id);

Expand All @@ -92,23 +100,21 @@ export class Builder {

// filter by brand
public async brandFilter(brandId: string): Promise<IIntegrationIds> {
const integrations = await Integrations.find({ brandId });
const integrations = await Integrations.findIntegrations({ brandId });

return { integrationId: { $in: integrations.map(i => i._id) } };
}

// filter by integration kind
public async integrationTypeFilter(kind: string): Promise<IIntegrationIds> {
const integrations = await Integrations.find({ kind });
const integrations = await Integrations.findIntegrations({ kind });

return { integrationId: { $in: integrations.map(i => i._id) } };
}

// filter by integration
public async integrationFilter(integration: string): Promise<IIntegrationIds> {
const integrations = await Integrations.find({
kind: integration,
});
const integrations = await Integrations.findIntegrations({ kind: integration });
/**
* Since both of brand and integration filters use a same integrationId field
* we need to intersect two arrays of integration ids.
Expand Down Expand Up @@ -174,7 +180,7 @@ export class Builder {
*/
public async buildAllQueries(): Promise<void> {
this.queries = {
default: this.defaultFilters(),
default: await this.defaultFilters(),
type: {},
segment: {},
tag: {},
Expand Down
2 changes: 1 addition & 1 deletion src/data/modules/insights/exportData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export const generateTagReport = async (args: IListArgs, user: IUserDocument) =>

const tags = await Tags.find({ type: TAG_TYPES.CONVERSATION }).select('name');

const integrationIds = await Integrations.find(filterSelector.integration).select('_id');
const integrationIds = await Integrations.findIntegrations(filterSelector.integration).select('_id');

const rawIntegrationIds = integrationIds.map(row => row._id);

Expand Down
4 changes: 2 additions & 2 deletions src/data/modules/insights/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const getConversationSelector = async (
fieldName: string = 'createdAt',
): Promise<any> => {
if (Object.keys(filterSelector.integration).length > 0) {
const integrationIds = await Integrations.find(filterSelector.integration).select('_id');
const integrationIds = await Integrations.findIntegrations(filterSelector.integration).select('_id');
conversationSelector.integrationId = { $in: integrationIds.map(row => row._id) };
}

Expand Down Expand Up @@ -547,7 +547,7 @@ export const getConversationSelectorToMsg = async (
}

if (Object.keys(filterSelector.integration).length > 0) {
const integrationIdsList = await Integrations.find(filterSelector.integration).select('_id');
const integrationIdsList = await Integrations.findIntegrations(filterSelector.integration).select('_id');
conversationSelector.integrationId = { $in: integrationIdsList.map(row => row._id) };
}
return { ...conversationSelector };
Expand Down
5 changes: 5 additions & 0 deletions src/data/permissions/actions/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ export const moduleObjects = {
'integrationsCreateLeadIntegration',
'integrationsEditLeadIntegration',
'integrationsRemove',
'integrationsArchive',
],
},
{
Expand Down Expand Up @@ -901,6 +902,10 @@ export const moduleObjects = {
name: 'integrationsRemove',
description: 'Remove integration',
},
{
name: 'integrationsArchive',
description: 'Archive an integration',
},
],
},
inbox: {
Expand Down
2 changes: 1 addition & 1 deletion src/data/resolvers/brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { IBrandDocument } from '../../db/models/definitions/brands';

export default {
integrations(brand: IBrandDocument) {
return Integrations.find({ brandId: brand._id });
return Integrations.findIntegrations({ brandId: brand._id });
},
};
2 changes: 1 addition & 1 deletion src/data/resolvers/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IChannelDocument } from '../../db/models/definitions/channels';

export default {
integrations(channel: IChannelDocument) {
return Integrations.find({ _id: { $in: channel.integrationIds } });
return Integrations.findIntegrations({ _id: { $in: channel.integrationIds } });
},

members(channel: IChannelDocument) {
Expand Down
20 changes: 20 additions & 0 deletions src/data/resolvers/mutations/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ const integrationMutations = {
data: JSON.stringify(doc),
});
},

async integrationsArchive(_root, { _id }: { _id: string }, { user }: IContext) {
const integration = await Integrations.findOne({ _id });
await Integrations.updateOne({ _id }, { $set: { isActive: false } });

if (integration) {
await putUpdateLog(
{
type: 'integration',
object: integration,
newData: JSON.stringify({ isActive: false }),
description: `Integration "${integration.name}" has been archived.`,
},
user,
);
}

return Integrations.findOne({ _id });
},
};

checkPermission(
Expand All @@ -242,5 +261,6 @@ checkPermission(integrationMutations, 'integrationsSaveMessengerConfigs', 'integ
checkPermission(integrationMutations, 'integrationsCreateLeadIntegration', 'integrationsCreateLeadIntegration');
checkPermission(integrationMutations, 'integrationsEditLeadIntegration', 'integrationsEditLeadIntegration');
checkPermission(integrationMutations, 'integrationsRemove', 'integrationsRemove');
checkPermission(integrationMutations, 'integrationsArchive', 'integrationsArchive');

export default integrationMutations;
44 changes: 25 additions & 19 deletions src/data/resolvers/queries/conversationQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ export default class Builder {
public user: IUserArgs;
public queries: any;
public unassignedQuery?: IUnassignedFilter;
public activeIntegrationIds: string[] = [];

constructor(params: IListArgs, user: IUserArgs) {
this.params = params;
this.user = user;
}

public defaultFilters(): { [index: string]: {} } {
public async defaultFilters(): Promise<any> {
const activeIntegrations = await Integrations.findIntegrations({}, { _id: 1 });
this.activeIntegrationIds = activeIntegrations.map(integ => integ._id);

let statusFilter = this.statusFilter([CONVERSATION_STATUSES.NEW, CONVERSATION_STATUSES.OPEN]);

if (this.params.status === 'closed') {
Expand All @@ -80,7 +84,7 @@ export default class Builder {
};
}

public intersectIntegrationIds(...queries: any[]): { integrationId: IIn } {
public async intersectIntegrationIds(...queries: any[]): Promise<{ integrationId: IIn }> {
// filter only queries with $in field
const withIn = queries.filter(q => q.integrationId && q.integrationId.$in && q.integrationId.$in.length > 0);

Expand All @@ -91,7 +95,7 @@ export default class Builder {
const nestedIntegrationIds = _.pluck($ins, '$in');

// ['id1']
const integrationids: any = _.intersection(...nestedIntegrationIds);
const integrationids: string[] = _.intersection(...nestedIntegrationIds);

return {
integrationId: { $in: integrationids },
Expand All @@ -102,20 +106,21 @@ export default class Builder {
* find integrationIds from channel && brand
*/
public async integrationsFilter(): Promise<IIntersectIntegrationIds> {
const channelFilter = {
memberIds: this.user._id,
};

// find all posssible integrations
let availIntegrationIds: any = [];
let availIntegrationIds: string[] = [];

const channels = await Channels.find(channelFilter);
const channels = await Channels.find({ memberIds: this.user._id });

channels.forEach(channel => {
availIntegrationIds = _.union(availIntegrationIds, channel.integrationIds || '');
availIntegrationIds = _.union(
availIntegrationIds,
(channel.integrationIds || []).filter(id => this.activeIntegrationIds.includes(id)),
);
});

const nestedIntegrationIds: any = [{ integrationId: { $in: availIntegrationIds } }];
const nestedIntegrationIds: Array<{ integrationId: { $in: string[] } }> = [
{ integrationId: { $in: availIntegrationIds } },
];

// filter by channel
if (this.params.channelId) {
Expand All @@ -135,18 +140,19 @@ export default class Builder {
// filter by channel
public async channelFilter(channelId: string): Promise<{ integrationId: IIn }> {
const channel = await Channels.findOne({ _id: channelId });

if (channel && channel.integrationIds) {
return {
integrationId: { $in: channel.integrationIds },
integrationId: { $in: channel.integrationIds.filter(id => this.activeIntegrationIds.includes(id)) },
};
} else {
return { integrationId: { $in: [] } };
}

return { integrationId: { $in: [] } };
}

// filter by brand
public async brandFilter(brandId: string): Promise<{ integrationId: IIn }> {
const integrations = await Integrations.find({ brandId });
const integrations = await Integrations.findIntegrations({ brandId });
const integrationIds = _.pluck(integrations, '_id');

return {
Expand All @@ -171,8 +177,8 @@ export default class Builder {
}

// filter by starred
public starredFilter(): { _id: IIn | { $in: any[] } } {
let ids: any = [];
public starredFilter(): { _id: IIn | { $in: string[] } } {
let ids: string[] = [];

if (this.user) {
ids = this.user.starredConversationIds || [];
Expand All @@ -191,7 +197,7 @@ export default class Builder {

// filter by integration type
public async integrationTypeFilter(integrationType: string): Promise<{ $and: IIntersectIntegrationIds[] }> {
const integrations = await Integrations.find({ kind: integrationType });
const integrations = await Integrations.findIntegrations({ kind: integrationType });

return {
$and: [
Expand Down Expand Up @@ -225,7 +231,7 @@ export default class Builder {
*/
public async buildAllQueries(): Promise<void> {
this.queries = {
default: this.defaultFilters(),
default: await this.defaultFilters(),
starred: {},
status: {},
unassigned: {},
Expand Down
3 changes: 2 additions & 1 deletion src/data/resolvers/queries/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const countByBrands = async (qb: any): Promise<ICountBy> => {
for (const brand of brands) {
byBrands[brand._id] = await count({
...qb.mainQuery(),
...qb.intersectIntegrationIds(qb.queries.channel, await qb.brandFilter(brand._id)),
...(await qb.intersectIntegrationIds(qb.queries.channel, await qb.brandFilter(brand._id))),
});
}

Expand Down Expand Up @@ -265,6 +265,7 @@ const conversationQueries = {
async conversationsTotalUnreadCount(_root, _args, { user }: IContext) {
// initiate query builder
const qb = new QueryBuilder({}, { _id: user._id });
await qb.buildAllQueries();

// get all possible integration ids
const integrationsFilter = await qb.integrationsFilter();
Expand Down
2 changes: 1 addition & 1 deletion src/data/resolvers/queries/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const countByField = async (
const countByIntegration = async (mainQuery: any): Promise<ICountBy> => {
const counts: ICountBy = {};

const integrations = await Integrations.find({ kind: { $in: INTEGRATION_KIND_CHOICES.ALL } }).select({
const integrations = await Integrations.findIntegrations({ kind: { $in: INTEGRATION_KIND_CHOICES.ALL } }).select({
_id: 1,
name: 1,
kind: 1,
Expand Down
4 changes: 3 additions & 1 deletion src/data/resolvers/queries/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ const fieldQueries = {
let fields: Array<{ _id: number; name: string; label?: string; brandName?: string; brandId?: string }> = [];

if (contentType === FIELD_CONTENT_TYPES.CUSTOMER) {
const messengerIntegrations = await Integrations.find({ kind: INTEGRATION_KIND_CHOICES.MESSENGER });
const messengerIntegrations = await Integrations.findIntegrations({
kind: INTEGRATION_KIND_CHOICES.MESSENGER,
});

// generate messengerData.customData fields
for (const integration of messengerIntegrations) {
Expand Down
4 changes: 2 additions & 2 deletions src/data/resolvers/queries/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const insightQueries = {

// count conversations by each integration kind
for (const kind of INTEGRATION_KIND_CHOICES.ALL) {
const integrationIds = await Integrations.find({
const integrationIds = await Integrations.findIntegrations({
...filterSelector.integration,
kind,
}).select('_id');
Expand Down Expand Up @@ -78,7 +78,7 @@ const insightQueries = {

const tags = await Tags.find({ type: TAG_TYPES.CONVERSATION }).select('name');

const integrationIdsByTag = await Integrations.find(filterSelector.integration).select('_id');
const integrationIdsByTag = await Integrations.findIntegrations(filterSelector.integration).select('_id');

const rawIntegrationIdsByTag = integrationIdsByTag.map(row => row._id);

Expand Down
8 changes: 4 additions & 4 deletions src/data/resolvers/queries/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { paginate } from '../../utils';
* Common helper for integrations & integrationsTotalCount
*/
const generateFilterQuery = async ({ kind, channelId, brandId, searchValue, tag }) => {
const query: any = {};
const query: any = { isActive: true };

if (kind) {
query.kind = kind;
Expand Down Expand Up @@ -54,7 +54,7 @@ const integrationQueries = {
},
) {
const query = await generateFilterQuery(args);
const integrations = paginate(Integrations.find(query), args);
const integrations = paginate(Integrations.findIntegrations(query), args);

return integrations.sort({ name: 1 });
},
Expand All @@ -66,7 +66,7 @@ const integrationQueries = {
const usedTypes: Array<{ _id: string; name: string }> = [];

for (const kind of KIND_CHOICES.ALL) {
if ((await Integrations.find({ kind }).countDocuments()) > 0) {
if ((await Integrations.findIntegrations({ kind }).countDocuments()) > 0) {
usedTypes.push({ _id: kind, name: INTEGRATION_NAMES_MAP[kind] });
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ const integrationQueries = {
};

const count = query => {
return Integrations.find(query).countDocuments();
return Integrations.findIntegrations(query).countDocuments();
};

// Counting integrations by tag
Expand Down
2 changes: 1 addition & 1 deletion src/data/resolvers/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export default {
},

leads(script: IScriptDocument) {
return Integrations.find({ _id: { $in: script.leadIds || [] } });
return Integrations.findIntegrations({ _id: { $in: script.leadIds || [] } });
},
};
Loading

0 comments on commit 0f0ae7a

Please sign in to comment.