-
-
Notifications
You must be signed in to change notification settings - Fork 655
/
removeDirectChat.ts
81 lines (71 loc) · 2.42 KB
/
removeDirectChat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { DirectChat, DirectChatMessage, Organization } from "../../models";
import { adminCheck } from "../../utilities";
import { errors, requestContext } from "../../libraries";
import {
CHAT_NOT_FOUND_ERROR,
ORGANIZATION_NOT_FOUND_ERROR,
} from "../../constants";
import { findOrganizationsInCache } from "../../services/OrganizationCache/findOrganizationsInCache";
import { cacheOrganizations } from "../../services/OrganizationCache/cacheOrganizations";
/**
* This function enables to remove direct chat.
* @param _parent - parent of current request
* @param args - payload provided with the request
* @param context - context of entire application
* @remarks The following checks are done:
* 1. If the organization exists
* 2. If the chat exists
* 3. If the user is an admin of the organization.
* @returns Deleted chat.
*/
export const removeDirectChat: MutationResolvers["removeDirectChat"] = async (
_parent,
args,
context,
) => {
let organization;
const organizationFoundInCache = await findOrganizationsInCache([
args.organizationId,
]);
organization = organizationFoundInCache[0];
if (organizationFoundInCache.includes(null)) {
organization = await Organization.findOne({
_id: args.organizationId,
}).lean();
if (organization) await cacheOrganizations([organization]);
}
// Checks whether organization exists.
if (!organization) {
throw new errors.NotFoundError(
requestContext.translate(ORGANIZATION_NOT_FOUND_ERROR.MESSAGE),
ORGANIZATION_NOT_FOUND_ERROR.CODE,
ORGANIZATION_NOT_FOUND_ERROR.PARAM,
);
}
const directChat = await DirectChat.findOne({
_id: args.chatId,
}).lean();
// Checks whether directChat exists.
if (!directChat) {
throw new errors.NotFoundError(
requestContext.translate(CHAT_NOT_FOUND_ERROR.MESSAGE),
CHAT_NOT_FOUND_ERROR.CODE,
CHAT_NOT_FOUND_ERROR.PARAM,
);
}
// Checks whether currentUser with _id === context.userId is an admin of organzation.
await adminCheck(context.userId, organization);
// Deletes all directChatMessages with _id as one of the ids in directChat.messages list.
await DirectChatMessage.deleteMany({
_id: {
$in: directChat.messages,
},
});
// Deletes the directChat.
await DirectChat.deleteOne({
_id: args.chatId,
});
// Returns deleted directChat.
return directChat;
};