Skip to content

Commit 1d19d15

Browse files
committed
fix: complete the copilot requests if the incoming role is observer or customer
1 parent c701b1e commit 1d19d15

File tree

1 file changed

+78
-72
lines changed

1 file changed

+78
-72
lines changed

src/routes/projectMembers/update.js

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,78 @@ const updateProjectMemberValdiations = {
3535
},
3636
};
3737

38+
const completeAllCopilotRequests = async (req, projectId, _transaction) => {
39+
const allCopilotRequests = await models.CopilotRequest.findAll({
40+
where: {
41+
projectId,
42+
},
43+
transaction: _transaction,
44+
});
45+
46+
req.log.debug(`all copilot requests ${JSON.stringify(allCopilotRequests)}`);
47+
48+
await models.CopilotRequest.update({
49+
status: COPILOT_REQUEST_STATUS.FULFILLED,
50+
}, {
51+
where: {
52+
id: {
53+
[Op.in]: allCopilotRequests.map(item => item.id),
54+
}
55+
},
56+
transaction: _transaction,
57+
});
58+
59+
req.log.debug(`updated all copilot requests`);
60+
61+
const copilotOpportunites = await models.CopilotOpportunity.findAll({
62+
where: {
63+
copilotRequestId: {
64+
[Op.in]: allCopilotRequests.map(item => item.id),
65+
},
66+
},
67+
transaction: _transaction,
68+
});
69+
70+
req.log.debug(`all copilot opportunities ${JSON.stringify(copilotOpportunites)}`);
71+
72+
await models.CopilotOpportunity.update({
73+
status: COPILOT_OPPORTUNITY_STATUS.COMPLETED,
74+
}, {
75+
where: {
76+
id: {
77+
[Op.in]: copilotOpportunites.map(item => item.id),
78+
}
79+
},
80+
transaction: _transaction,
81+
});
82+
83+
req.log.debug(`updated all copilot opportunities`);
84+
85+
const allCopilotApplications = await models.CopilotApplication.findAll({
86+
where: {
87+
opportunityId: {
88+
[Op.in]: copilotOpportunites.map(item => item.id),
89+
},
90+
},
91+
transaction: _transaction,
92+
});
93+
94+
req.log.debug(`all copilot applications ${JSON.stringify(allCopilotApplications)}`);
95+
96+
await models.CopilotApplication.update({
97+
status: COPILOT_APPLICATION_STATUS.CANCELED,
98+
}, {
99+
where: {
100+
id: {
101+
[Op.in]: allCopilotApplications.map(item => item.id),
102+
},
103+
},
104+
transaction: _transaction,
105+
});
106+
107+
req.log.debug(`updated all copilot applications`);
108+
};
109+
38110
module.exports = [
39111
// handles request validations
40112
validate(updateProjectMemberValdiations),
@@ -84,77 +156,7 @@ module.exports = [
84156
if ((updatedProps.role === previousValue.role || updatedProps.action === 'overwrite') &&
85157
(_.isUndefined(updatedProps.isPrimary) ||
86158
updatedProps.isPrimary === previousValue.isPrimary)) {
87-
88-
const allCopilotRequests = await models.CopilotRequest.findAll({
89-
where: {
90-
projectId,
91-
},
92-
transaction: _transaction,
93-
});
94-
95-
req.log.debug(`all copilot requests ${JSON.stringify(allCopilotRequests)}`);
96-
97-
await models.CopilotRequest.update({
98-
status: COPILOT_REQUEST_STATUS.FULFILLED,
99-
}, {
100-
where: {
101-
id: {
102-
[Op.in]: allCopilotRequests.map(item => item.id),
103-
}
104-
},
105-
transaction: _transaction,
106-
});
107-
108-
req.log.debug(`updated all copilot requests`);
109-
110-
const copilotOpportunites = await models.CopilotOpportunity.findAll({
111-
where: {
112-
copilotRequestId: {
113-
[Op.in]: allCopilotRequests.map(item => item.id),
114-
},
115-
},
116-
transaction: _transaction,
117-
});
118-
119-
req.log.debug(`all copilot opportunities ${JSON.stringify(copilotOpportunites)}`);
120-
121-
await models.CopilotOpportunity.update({
122-
status: COPILOT_OPPORTUNITY_STATUS.COMPLETED,
123-
}, {
124-
where: {
125-
id: {
126-
[Op.in]: copilotOpportunites.map(item => item.id),
127-
}
128-
},
129-
transaction: _transaction,
130-
});
131-
132-
req.log.debug(`updated all copilot opportunities`);
133-
134-
const allCopilotApplications = await models.CopilotApplication.findAll({
135-
where: {
136-
opportunityId: {
137-
[Op.in]: copilotOpportunites.map(item => item.id),
138-
},
139-
},
140-
transaction: _transaction,
141-
});
142-
143-
req.log.debug(`all copilot applications ${JSON.stringify(allCopilotApplications)}`);
144-
145-
await models.CopilotApplication.update({
146-
status: COPILOT_APPLICATION_STATUS.CANCELED,
147-
}, {
148-
where: {
149-
id: {
150-
[Op.in]: allCopilotApplications.map(item => item.id),
151-
},
152-
},
153-
transaction: _transaction,
154-
});
155-
156-
req.log.debug(`updated all copilot applications`);
157-
159+
await completeAllCopilotRequests(req, projectId, _transaction);
158160
return Promise.resolve();
159161
}
160162

@@ -196,9 +198,13 @@ module.exports = [
196198
});
197199
})
198200
.then(() => projectMember.reload(projectMember.id))
199-
.then(() => {
201+
.then(async () => {
200202
projectMember = projectMember.get({ plain: true });
201203
projectMember = _.omit(projectMember, ['deletedAt']);
204+
205+
if (['observer', 'customer'].includes(updatedProps.role)) {
206+
await completeAllCopilotRequests(req, projectId, _transaction);
207+
}
202208
})
203209
.then(() => (
204210
util.getObjectsWithMemberDetails([projectMember], fields, req)

0 commit comments

Comments
 (0)