Skip to content

Commit

Permalink
perf(tags): dettaching connected items when tag remove
Browse files Browse the repository at this point in the history
  • Loading branch information
batamar committed Mar 25, 2021
1 parent b79bc18 commit db6b309
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
47 changes: 32 additions & 15 deletions api/src/__tests__/tagDb.test.ts
@@ -1,5 +1,10 @@
import { engageMessageFactory, tagsFactory } from '../db/factories';
import { EngageMessages, Tags } from '../db/models';
import {
conversationFactory,
engageMessageFactory,
tagsFactory
} from '../db/factories';
import { Conversations, EngageMessages, Tags } from '../db/models';
import { IConversationDocument } from '../db/models/definitions/conversations';

import './setup.ts';

Expand Down Expand Up @@ -229,18 +234,8 @@ describe('Test tags model', () => {
}
});

test("Can't remove a tag", async () => {
expect.assertions(2);
try {
await EngageMessages.updateMany(
{ _id: _message._id },
{ $set: { tagIds: [_tag._id] } }
);

await Tags.removeTag(_tag._id);
} catch (e) {
expect(e.message).toEqual("Can't remove a tag with tagged object(s)");
}
test('Remove tag with child', async () => {
expect.assertions(1);

try {
await Tags.createTag({
Expand All @@ -251,7 +246,29 @@ describe('Test tags model', () => {

await Tags.removeTag(_tag._id);
} catch (e) {
expect(e.message).toEqual("Can't remove a tag");
expect(e.message).toEqual('Please remove child tags first');
}
});

test('Remove tag success', async () => {
expect.assertions(1);

const t1 = await tagsFactory({ type: 'conversation' });
const t2 = await tagsFactory({ type: 'conversation' });
const t3 = await tagsFactory({ type: 'conversation' });

let conv: IConversationDocument | null = await conversationFactory({});

await Tags.tagsTag('conversation', [conv._id], [t1._id, t2._id, t3._id]);

await Tags.removeTag(t2._id);

conv = await Conversations.findOne({ _id: conv._id });

if (conv) {
expect(JSON.stringify(conv.tagIds)).toEqual(
JSON.stringify([t1._id, t3._id])
);
}
});
});
23 changes: 10 additions & 13 deletions api/src/db/models/Tags.ts
Expand Up @@ -51,6 +51,7 @@ const removeRelatedIds = async (tag: ITagDocument) => {
}

const relatedIds: string[] = tag.relatedIds || [];

relatedIds.push(tag._id);

const doc: Array<{
Expand Down Expand Up @@ -312,22 +313,18 @@ export const loadClass = () => {
const childCount = await Tags.countDocuments({ parentId: _id });

if (childCount > 0) {
throw new Error("Can't remove a tag");
throw new Error('Please remove child tags first');
}

const selector = { tagIds: { $in: [_id] } };

let count = 0;
count += await Customers.countDocuments(selector);
count += await Conversations.countDocuments(selector);
count += await EngageMessages.countDocuments(selector);
count += await Companies.countDocuments(selector);
count += await Integrations.findIntegrations(selector).countDocuments();
count += await Products.countDocuments(selector);

if (count > 0) {
throw new Error("Can't remove a tag with tagged object(s)");
}
const modifier = { $pull: { tagIds: { $in: [_id] } } };

await Customers.updateMany(selector, modifier);
await Conversations.updateMany(selector, modifier);
await EngageMessages.updateMany(selector, modifier);
await Companies.updateMany(selector, modifier);
await Integrations.updateMany(selector, modifier);
await Products.updateMany(selector, modifier);

await removeRelatedIds(tag);

Expand Down
4 changes: 3 additions & 1 deletion ui/src/modules/tags/containers/List.tsx
Expand Up @@ -22,7 +22,9 @@ const ListContainer = (props: FinalProps) => {
const { tagsQuery, removeMutation, type } = props;

const remove = tag => {
confirm()
confirm(
`All associated ${type}(s) with the tag will be permanently deleted (no undo). Are you sure ?`
)
.then(() => {
removeMutation({ variables: { _id: tag._id } })
.then(() => {
Expand Down

0 comments on commit db6b309

Please sign in to comment.