|
| 1 | +import { CR_Type, Organization, Scope_CR_Why_Type, User, WBS_Element_Status } from '@prisma/client'; |
| 2 | +import { createTestOrganization, createTestUser, resetUsers } from '../test-utils'; |
| 3 | +import ChangeRequestsService from '../../src/services/change-requests.services'; |
| 4 | +import { supermanAdmin } from '../test-data/users.test-data'; |
| 5 | +import { ProjectProposedChangesCreateArgs, WorkPackageProposedChangesCreateArgs } from 'shared'; |
| 6 | +import prisma from '../../src/prisma/prisma'; |
| 7 | + |
| 8 | +describe('Change Request Tests', () => { |
| 9 | + let orgId: string; |
| 10 | + let organization: Organization; |
| 11 | + let user: User; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + organization = await createTestOrganization(); |
| 15 | + orgId = organization.organizationId; |
| 16 | + user = await createTestUser(supermanAdmin, orgId); |
| 17 | + await prisma.wBS_Element.create({ |
| 18 | + data: { |
| 19 | + carNumber: 12, |
| 20 | + projectNumber: 13, |
| 21 | + workPackageNumber: 14, |
| 22 | + name: 'test wbs', |
| 23 | + organizationId: orgId, |
| 24 | + status: WBS_Element_Status.INACTIVE |
| 25 | + } |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(async () => { |
| 30 | + await resetUsers(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('Create Change Request', () => { |
| 34 | + it('create change request on an inactive project - project changes', async () => { |
| 35 | + const projPropChanges: ProjectProposedChangesCreateArgs = { |
| 36 | + name: 'Project name changes', |
| 37 | + descriptionBullets: [], |
| 38 | + links: [], |
| 39 | + budget: 10, |
| 40 | + summary: 'Summary', |
| 41 | + teamIds: [], |
| 42 | + workPackageProposedChanges: [] |
| 43 | + }; |
| 44 | + |
| 45 | + const cr = await ChangeRequestsService.createStandardChangeRequest( |
| 46 | + user, |
| 47 | + 12, |
| 48 | + 13, |
| 49 | + 14, |
| 50 | + CR_Type.DEFINITION_CHANGE, |
| 51 | + 'What', |
| 52 | + [ |
| 53 | + { |
| 54 | + type: Scope_CR_Why_Type.COMPETITION, |
| 55 | + explain: 'Explaining' |
| 56 | + } |
| 57 | + ], |
| 58 | + [], |
| 59 | + organization, |
| 60 | + projPropChanges, |
| 61 | + null |
| 62 | + ); |
| 63 | + |
| 64 | + expect(cr.submitter.userId).toEqual(user.userId); |
| 65 | + expect(cr.wbsNum?.carNumber).toEqual(12); |
| 66 | + expect(cr.wbsNum?.projectNumber).toEqual(13); |
| 67 | + expect(cr.wbsNum?.workPackageNumber).toEqual(14); |
| 68 | + |
| 69 | + expect(cr.type).toEqual(CR_Type.DEFINITION_CHANGE); |
| 70 | + expect(cr.what).toEqual('What'); |
| 71 | + expect(cr.proposedSolutions).toHaveLength(0); |
| 72 | + |
| 73 | + expect(cr.wbsNum).toBeDefined(); |
| 74 | + expect(cr.wbsNum).not.toBeNull(); |
| 75 | + |
| 76 | + const wbsElement = await prisma.wBS_Element.findUnique({ |
| 77 | + where: { |
| 78 | + wbsNumber: { |
| 79 | + carNumber: 12, |
| 80 | + projectNumber: 13, |
| 81 | + workPackageNumber: 14, |
| 82 | + organizationId: organization.organizationId |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + expect(wbsElement?.status).toEqual(WBS_Element_Status.INACTIVE); |
| 88 | + expect(wbsElement?.carNumber).toEqual(12); |
| 89 | + expect(wbsElement?.projectNumber).toEqual(13); |
| 90 | + expect(wbsElement?.workPackageNumber).toEqual(14); |
| 91 | + }); |
| 92 | + it('create change request does not make active project inactive - project changes', async () => { |
| 93 | + await prisma.wBS_Element.update({ |
| 94 | + where: { |
| 95 | + wbsNumber: { |
| 96 | + carNumber: 12, |
| 97 | + projectNumber: 13, |
| 98 | + workPackageNumber: 14, |
| 99 | + organizationId: organization.organizationId |
| 100 | + } |
| 101 | + }, |
| 102 | + data: { |
| 103 | + status: WBS_Element_Status.ACTIVE |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + const wpPropChanges: WorkPackageProposedChangesCreateArgs = { |
| 108 | + name: 'wp', |
| 109 | + descriptionBullets: [], |
| 110 | + links: [], |
| 111 | + duration: 3, |
| 112 | + startDate: '2025-09-13', |
| 113 | + blockedBy: [], |
| 114 | + leadId: user.userId, |
| 115 | + managerId: user.userId |
| 116 | + }; |
| 117 | + |
| 118 | + await ChangeRequestsService.createStandardChangeRequest( |
| 119 | + user, |
| 120 | + 12, |
| 121 | + 13, |
| 122 | + 14, |
| 123 | + CR_Type.DEFINITION_CHANGE, |
| 124 | + 'What', |
| 125 | + [ |
| 126 | + { |
| 127 | + type: Scope_CR_Why_Type.COMPETITION, |
| 128 | + explain: 'Explaining' |
| 129 | + } |
| 130 | + ], |
| 131 | + [], |
| 132 | + organization, |
| 133 | + null, |
| 134 | + wpPropChanges |
| 135 | + ); |
| 136 | + |
| 137 | + const wbsElement = await prisma.wBS_Element.findUnique({ |
| 138 | + where: { |
| 139 | + wbsNumber: { |
| 140 | + carNumber: 12, |
| 141 | + projectNumber: 13, |
| 142 | + workPackageNumber: 14, |
| 143 | + organizationId: organization.organizationId |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + expect(wbsElement?.status).toEqual(WBS_Element_Status.ACTIVE); |
| 149 | + expect(wbsElement?.carNumber).toEqual(12); |
| 150 | + expect(wbsElement?.projectNumber).toEqual(13); |
| 151 | + expect(wbsElement?.workPackageNumber).toEqual(14); |
| 152 | + }); |
| 153 | + it('create change request does not make active project inactive - work package changes', async () => { |
| 154 | + await prisma.wBS_Element.update({ |
| 155 | + where: { |
| 156 | + wbsNumber: { |
| 157 | + carNumber: 12, |
| 158 | + projectNumber: 13, |
| 159 | + workPackageNumber: 14, |
| 160 | + organizationId: organization.organizationId |
| 161 | + } |
| 162 | + }, |
| 163 | + data: { |
| 164 | + status: WBS_Element_Status.ACTIVE |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + const wpPropChanges: WorkPackageProposedChangesCreateArgs = { |
| 169 | + name: 'wp', |
| 170 | + descriptionBullets: [], |
| 171 | + links: [], |
| 172 | + duration: 3, |
| 173 | + startDate: '2025-09-13', |
| 174 | + blockedBy: [], |
| 175 | + leadId: user.userId, |
| 176 | + managerId: user.userId |
| 177 | + }; |
| 178 | + |
| 179 | + const cr = await ChangeRequestsService.createStandardChangeRequest( |
| 180 | + user, |
| 181 | + 12, |
| 182 | + 13, |
| 183 | + 14, |
| 184 | + CR_Type.DEFINITION_CHANGE, |
| 185 | + 'What', |
| 186 | + [ |
| 187 | + { |
| 188 | + type: Scope_CR_Why_Type.COMPETITION, |
| 189 | + explain: 'Explaining' |
| 190 | + } |
| 191 | + ], |
| 192 | + [], |
| 193 | + organization, |
| 194 | + null, |
| 195 | + wpPropChanges |
| 196 | + ); |
| 197 | + |
| 198 | + const wbsElement = await prisma.wBS_Element.findUnique({ |
| 199 | + where: { |
| 200 | + wbsNumber: { |
| 201 | + carNumber: 12, |
| 202 | + projectNumber: 13, |
| 203 | + workPackageNumber: 14, |
| 204 | + organizationId: organization.organizationId |
| 205 | + } |
| 206 | + } |
| 207 | + }); |
| 208 | + expect(cr.submitter.userId).toEqual(user.userId); |
| 209 | + expect(cr.wbsNum?.carNumber).toEqual(12); |
| 210 | + expect(cr.wbsNum?.projectNumber).toEqual(13); |
| 211 | + expect(cr.wbsNum?.workPackageNumber).toEqual(14); |
| 212 | + |
| 213 | + expect(cr.type).toEqual(CR_Type.DEFINITION_CHANGE); |
| 214 | + expect(cr.what).toEqual('What'); |
| 215 | + expect(cr.proposedSolutions).toHaveLength(0); |
| 216 | + |
| 217 | + expect(cr.wbsNum).toBeDefined(); |
| 218 | + expect(cr.wbsNum).not.toBeNull(); |
| 219 | + |
| 220 | + expect(wbsElement?.status).toEqual(WBS_Element_Status.ACTIVE); |
| 221 | + expect(wbsElement?.carNumber).toEqual(12); |
| 222 | + expect(wbsElement?.projectNumber).toEqual(13); |
| 223 | + expect(wbsElement?.workPackageNumber).toEqual(14); |
| 224 | + }); |
| 225 | + it('create change request on an inactive project - work package changes', async () => { |
| 226 | + await prisma.wBS_Element.update({ |
| 227 | + where: { |
| 228 | + wbsNumber: { |
| 229 | + carNumber: 12, |
| 230 | + projectNumber: 13, |
| 231 | + workPackageNumber: 14, |
| 232 | + organizationId: organization.organizationId |
| 233 | + } |
| 234 | + }, |
| 235 | + data: { |
| 236 | + status: WBS_Element_Status.INACTIVE |
| 237 | + } |
| 238 | + }); |
| 239 | + |
| 240 | + const wpPropChanges: WorkPackageProposedChangesCreateArgs = { |
| 241 | + name: 'wp', |
| 242 | + descriptionBullets: [], |
| 243 | + links: [], |
| 244 | + duration: 3, |
| 245 | + startDate: '2025-09-13', |
| 246 | + blockedBy: [], |
| 247 | + leadId: user.userId, |
| 248 | + managerId: user.userId |
| 249 | + }; |
| 250 | + |
| 251 | + await ChangeRequestsService.createStandardChangeRequest( |
| 252 | + user, |
| 253 | + 12, |
| 254 | + 13, |
| 255 | + 14, |
| 256 | + CR_Type.DEFINITION_CHANGE, |
| 257 | + 'What', |
| 258 | + [ |
| 259 | + { |
| 260 | + type: Scope_CR_Why_Type.COMPETITION, |
| 261 | + explain: 'Explaining' |
| 262 | + } |
| 263 | + ], |
| 264 | + [], |
| 265 | + organization, |
| 266 | + null, |
| 267 | + wpPropChanges |
| 268 | + ); |
| 269 | + |
| 270 | + const wbsElement = await prisma.wBS_Element.findUnique({ |
| 271 | + where: { |
| 272 | + wbsNumber: { |
| 273 | + carNumber: 12, |
| 274 | + projectNumber: 13, |
| 275 | + workPackageNumber: 14, |
| 276 | + organizationId: organization.organizationId |
| 277 | + } |
| 278 | + } |
| 279 | + }); |
| 280 | + |
| 281 | + expect(wbsElement?.status).toEqual(WBS_Element_Status.INACTIVE); |
| 282 | + expect(wbsElement?.carNumber).toEqual(12); |
| 283 | + expect(wbsElement?.projectNumber).toEqual(13); |
| 284 | + expect(wbsElement?.workPackageNumber).toEqual(14); |
| 285 | + }); |
| 286 | + }); |
| 287 | +}); |
0 commit comments