|
1 | 1 | import validate from 'express-validation';
|
2 | 2 | import _ from 'lodash';
|
3 | 3 | import Joi from 'joi';
|
| 4 | +import { Op } from 'sequelize'; |
4 | 5 | import { middleware as tcMiddleware } from 'tc-core-library-js';
|
5 | 6 | import models from '../../models';
|
6 | 7 | import util from '../../util';
|
7 | 8 | import { INVITE_STATUS, EVENT, RESOURCES, COPILOT_APPLICATION_STATUS, COPILOT_OPPORTUNITY_STATUS, COPILOT_REQUEST_STATUS, INVITE_SOURCE } from '../../constants';
|
8 | 9 | import { PERMISSION } from '../../permissions/constants';
|
9 | 10 |
|
| 11 | + |
10 | 12 | /**
|
11 | 13 | * API to update invite member to project.
|
12 | 14 | *
|
@@ -170,6 +172,79 @@ module.exports = [
|
170 | 172 | }, {
|
171 | 173 | transaction: t,
|
172 | 174 | });
|
| 175 | + } else if (source === INVITE_SOURCE.WORK_MANAGER) { |
| 176 | + const allCopilotRequestsByProjectId = await models.CopilotRequest.findAll({ |
| 177 | + where: { |
| 178 | + projectId: invite.projectId, |
| 179 | + }, |
| 180 | + transaction: t, |
| 181 | + }); |
| 182 | + |
| 183 | + const requestIds = allCopilotRequestsByProjectId.map(item => item.id); |
| 184 | + |
| 185 | + await models.CopilotRequest.update({ |
| 186 | + status: COPILOT_REQUEST_STATUS.CANCELED, |
| 187 | + }, { |
| 188 | + where: { |
| 189 | + id: { |
| 190 | + [Op.in]: requestIds, |
| 191 | + } |
| 192 | + }, |
| 193 | + transaction: t, |
| 194 | + }); |
| 195 | + |
| 196 | + const allCopilotOpportunityByRequestIds = await models.CopilotOpportunity.findAll({ |
| 197 | + where: { |
| 198 | + copilotRequestId: { |
| 199 | + [Op.in]: requestIds, |
| 200 | + }, |
| 201 | + }, |
| 202 | + transaction: t, |
| 203 | + }); |
| 204 | + |
| 205 | + await models.CopilotOpportunity.update({ |
| 206 | + status: COPILOT_OPPORTUNITY_STATUS.CANCELED, |
| 207 | + }, { |
| 208 | + where: { |
| 209 | + id: { |
| 210 | + [Op.in]: allCopilotOpportunityByRequestIds.map(item => item.id), |
| 211 | + }, |
| 212 | + }, |
| 213 | + transaction: t, |
| 214 | + }); |
| 215 | + |
| 216 | + const copilotApplications = await models.CopilotApplication.findAll({ |
| 217 | + where: { |
| 218 | + opportunityId: { |
| 219 | + [Op.in]: allCopilotOpportunityByRequestIds.map(item => item.id), |
| 220 | + }, |
| 221 | + }, |
| 222 | + transaction: t, |
| 223 | + }); |
| 224 | + |
| 225 | + await models.CopilotApplication.update({ |
| 226 | + status: COPILOT_APPLICATION_STATUS.CANCELED, |
| 227 | + }, { |
| 228 | + where: { |
| 229 | + id: { |
| 230 | + [Op.in]: copilotApplications.map(item => item.id), |
| 231 | + }, |
| 232 | + }, |
| 233 | + transaction: t, |
| 234 | + }); |
| 235 | + |
| 236 | + // Cancel the existing invites which are opened via |
| 237 | + // applications |
| 238 | + await models.ProjectMemberInvite.update({ |
| 239 | + status: INVITE_STATUS.CANCELED, |
| 240 | + }, { |
| 241 | + where: { |
| 242 | + applicationId: { |
| 243 | + [Op.in]: copilotApplications.map(item => item.id), |
| 244 | + } |
| 245 | + }, |
| 246 | + transaction: t, |
| 247 | + }); |
173 | 248 | }
|
174 | 249 |
|
175 | 250 | await t.commit();
|
|
0 commit comments