-
-
Notifications
You must be signed in to change notification settings - Fork 976
/
Copy pathcancelMembershipRequest.ts
128 lines (112 loc) · 3.85 KB
/
cancelMembershipRequest.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { MutationResolvers } from "../../types/generatedGraphQLTypes";
import { User, Organization, MembershipRequest } from "../../models";
import { errors, requestContext } from "../../libraries";
import {
ORGANIZATION_NOT_FOUND_ERROR,
USER_NOT_FOUND_ERROR,
USER_NOT_AUTHORIZED_ERROR,
MEMBERSHIP_REQUEST_NOT_FOUND_ERROR,
} from "../../constants";
import { findOrganizationsInCache } from "../../services/OrganizationCache/findOrganizationsInCache";
import { cacheOrganizations } from "../../services/OrganizationCache/cacheOrganizations";
/**
* This function enables to cancel membership request.
* @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 membership request exists
* 2. If the organization exists
* 3. If the user exists
* 4. If the user is the creator of the request
* @returns Deleted membership request
*/
export const cancelMembershipRequest: MutationResolvers["cancelMembershipRequest"] =
async (_parent, args, context) => {
const membershipRequest = await MembershipRequest.findOne({
_id: args.membershipRequestId,
}).lean();
// Checks whether membershipRequest exists.
if (!membershipRequest) {
throw new errors.NotFoundError(
requestContext.translate(MEMBERSHIP_REQUEST_NOT_FOUND_ERROR.MESSAGE),
MEMBERSHIP_REQUEST_NOT_FOUND_ERROR.CODE,
MEMBERSHIP_REQUEST_NOT_FOUND_ERROR.PARAM
);
}
let organization;
const organizationFoundInCache = await findOrganizationsInCache([
membershipRequest.organization,
]);
organization = organizationFoundInCache[0];
if (organizationFoundInCache.includes(null)) {
organization = await Organization.findOne({
_id: membershipRequest.organization,
}).lean();
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 currentUser = await User.findOne({
_id: context.userId,
}).lean();
// Checks whether currentUser exists.
if (!currentUser) {
throw new errors.NotFoundError(
requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE),
USER_NOT_FOUND_ERROR.CODE,
USER_NOT_FOUND_ERROR.PARAM
);
}
const currentUserCreatedMembershipRequest = currentUser._id.equals(
membershipRequest.user
);
// Checks whether currentUser is the creator of membershipRequest.
if (currentUserCreatedMembershipRequest === false) {
throw new errors.UnauthorizedError(
requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE),
USER_NOT_AUTHORIZED_ERROR.CODE,
USER_NOT_AUTHORIZED_ERROR.PARAM
);
}
// Deletes the membershipRequest.
await MembershipRequest.deleteOne({
_id: membershipRequest._id,
});
// Removes membershipRequest._id from membershipRequests list on organization's document.
const updatedOrganization = await Organization.findOneAndUpdate(
{
_id: organization._id,
},
{
$pull: {
membershipRequests: membershipRequest._id,
},
},
{
new: true,
}
);
if (updatedOrganization !== null) {
await cacheOrganizations([updatedOrganization]);
}
// Removes membershipRequest._id from membershipRequests list on currentUser's document.
await User.updateOne(
{
_id: currentUser._id,
},
{
$pull: {
membershipRequests: membershipRequest._id,
},
}
);
// Returns the deleted membershipRequest.
return membershipRequest;
};