team-request.service.spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. import { PrismaService } from '../prisma/prisma.service';
  2. import { TeamCollectionService } from '../team-collection/team-collection.service';
  3. import { TeamService } from '../team/team.service';
  4. import { TeamRequestService } from './team-request.service';
  5. import {
  6. TEAM_REQ_INVALID_TARGET_COLL_ID,
  7. TEAM_INVALID_COLL_ID,
  8. TEAM_INVALID_ID,
  9. TEAM_REQ_NOT_FOUND,
  10. TEAM_REQ_REORDERING_FAILED,
  11. TEAM_COLL_NOT_FOUND,
  12. } from 'src/errors';
  13. import * as E from 'fp-ts/Either';
  14. import { mockDeep, mockReset } from 'jest-mock-extended';
  15. import { TeamRequest } from './team-request.model';
  16. import { MoveTeamRequestArgs } from './input-type.args';
  17. import {
  18. TeamRequest as DbTeamRequest,
  19. Team as DbTeam,
  20. TeamCollection as DbTeamCollection,
  21. } from '@prisma/client';
  22. const mockPrisma = mockDeep<PrismaService>();
  23. const mockTeamService = mockDeep<TeamService>();
  24. const mockTeamCollectionService = mockDeep<TeamCollectionService>();
  25. const mockPubSub = { publish: jest.fn().mockResolvedValue(null) };
  26. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  27. // @ts-ignore
  28. const teamRequestService = new TeamRequestService(
  29. mockPrisma,
  30. mockTeamService as any,
  31. mockTeamCollectionService as any,
  32. mockPubSub as any,
  33. );
  34. const team: DbTeam = {
  35. id: 'team-a',
  36. name: 'Team A',
  37. };
  38. const teamCollection: DbTeamCollection = {
  39. id: 'team-coll-1',
  40. parentID: null,
  41. teamID: team.id,
  42. title: 'Team Collection 1',
  43. orderIndex: 1,
  44. createdOn: new Date(),
  45. updatedOn: new Date(),
  46. };
  47. const dbTeamRequests: DbTeamRequest[] = [];
  48. for (let i = 1; i <= 10; i++) {
  49. dbTeamRequests.push({
  50. id: `test-request-${i}`,
  51. collectionID: teamCollection.id,
  52. teamID: team.id,
  53. request: {},
  54. title: `Test Request ${i}`,
  55. orderIndex: i,
  56. createdOn: new Date(),
  57. updatedOn: new Date(),
  58. });
  59. }
  60. const teamRequests: TeamRequest[] = dbTeamRequests.map((tr) => ({
  61. id: tr.id,
  62. collectionID: tr.collectionID,
  63. teamID: tr.teamID,
  64. title: tr.title,
  65. request: JSON.stringify(tr.request),
  66. }));
  67. beforeEach(async () => {
  68. mockReset(mockPrisma);
  69. });
  70. describe('updateTeamRequest', () => {
  71. test('resolves correctly if title not given in parameter', async () => {
  72. const dbRequest = dbTeamRequests[0];
  73. mockPrisma.teamRequest.update.mockResolvedValue(dbRequest);
  74. await expect(
  75. teamRequestService.updateTeamRequest(
  76. dbRequest.id,
  77. undefined, // title
  78. JSON.stringify(dbRequest.request), // request
  79. ),
  80. ).resolves.toBeDefined();
  81. });
  82. test('resolves correctly if request not given in parameter', async () => {
  83. const dbRequest = dbTeamRequests[0];
  84. mockPrisma.teamRequest.update.mockResolvedValue(dbRequest);
  85. await expect(
  86. teamRequestService.updateTeamRequest(
  87. dbRequest.id,
  88. dbRequest.title,
  89. undefined,
  90. ),
  91. ).resolves.toBeDefined();
  92. });
  93. test('resolves correctly if both request and title are null', async () => {
  94. const dbRequest = dbTeamRequests[0];
  95. mockPrisma.teamRequest.update.mockResolvedValue(dbRequest);
  96. await expect(
  97. teamRequestService.updateTeamRequest(dbRequest.id, undefined, undefined),
  98. ).resolves.toBeDefined();
  99. });
  100. test('resolves correctly for non-null request and title', async () => {
  101. const dbRequest = dbTeamRequests[0];
  102. mockPrisma.teamRequest.update.mockResolvedValue(dbRequest);
  103. await expect(
  104. teamRequestService.updateTeamRequest(
  105. dbRequest.id,
  106. dbRequest.title,
  107. JSON.stringify(dbRequest.request),
  108. ),
  109. ).resolves.toBeDefined();
  110. });
  111. test('rejects for invalid request id', async () => {
  112. mockPrisma.teamRequest.update.mockRejectedValue('RecordNotFound');
  113. await expect(
  114. teamRequestService.updateTeamRequest(
  115. 'invalidtestreq',
  116. undefined,
  117. undefined,
  118. ),
  119. ).resolves.toEqualLeft(TEAM_REQ_NOT_FOUND);
  120. });
  121. test('resolves for valid request id', async () => {
  122. const dbRequest = dbTeamRequests[0];
  123. mockPrisma.teamRequest.update.mockResolvedValue(dbRequest);
  124. await expect(
  125. teamRequestService.updateTeamRequest(dbRequest.id, undefined, undefined),
  126. ).resolves.toBeDefined();
  127. });
  128. test('publishes update to pubsub topic "team_req/<team_id>/req_updated"', async () => {
  129. const dbRequest = dbTeamRequests[0];
  130. const request = teamRequests[0];
  131. mockPrisma.teamRequest.update.mockResolvedValue(dbRequest);
  132. await teamRequestService.updateTeamRequest(
  133. dbRequest.id,
  134. undefined,
  135. undefined,
  136. );
  137. expect(mockPubSub.publish).toHaveBeenCalledWith(
  138. `team_req/${dbRequest.teamID}/req_updated`,
  139. request,
  140. );
  141. });
  142. });
  143. describe('searchRequest', () => {
  144. test('resolves with the correct info with a null cursor', async () => {
  145. const dbRequest = dbTeamRequests[0];
  146. mockPrisma.teamRequest.findMany.mockResolvedValue(dbTeamRequests);
  147. await expect(
  148. teamRequestService.searchRequest(
  149. dbRequest.teamID,
  150. dbRequest.title,
  151. null,
  152. 10,
  153. ),
  154. ).resolves.toBeDefined();
  155. });
  156. test('resolves with an empty array when a match with the search term is not found', async () => {
  157. const dbRequest = dbTeamRequests[0];
  158. mockPrisma.teamRequest.findMany.mockResolvedValue([]);
  159. await expect(
  160. teamRequestService.searchRequest(
  161. dbRequest.teamID,
  162. 'unknown_title',
  163. null,
  164. 10,
  165. ),
  166. ).resolves.toBeDefined();
  167. });
  168. test('resolves with the correct info with a set cursor', async () => {
  169. const dbRequest = dbTeamRequests[0];
  170. mockPrisma.teamRequest.findMany.mockResolvedValue(dbTeamRequests);
  171. await expect(
  172. teamRequestService.searchRequest(
  173. dbRequest.teamID,
  174. dbRequest.title,
  175. dbRequest.id,
  176. 10,
  177. ),
  178. ).resolves.toBeDefined();
  179. });
  180. });
  181. describe('deleteTeamRequest', () => {
  182. test('rejects if the request id is not found', async () => {
  183. mockPrisma.teamRequest.findFirst.mockResolvedValue(null as any);
  184. const response = teamRequestService.deleteTeamRequest('invalidrequest');
  185. expect(response).resolves.toEqualLeft(TEAM_REQ_NOT_FOUND);
  186. expect(mockPrisma.teamRequest.delete).not.toHaveBeenCalled();
  187. });
  188. test('resolves for a valid request id', async () => {
  189. const dbRequest = dbTeamRequests[0];
  190. mockPrisma.teamRequest.findFirst.mockResolvedValue(dbRequest);
  191. mockPrisma.teamRequest.delete.mockResolvedValue(dbRequest);
  192. await expect(
  193. teamRequestService.deleteTeamRequest(dbRequest.id),
  194. ).resolves.toEqualRight(true);
  195. });
  196. test('publishes deletion to pubsub topic "team_req/<team_id>/req_deleted"', async () => {
  197. const dbRequest = dbTeamRequests[0];
  198. mockPrisma.teamRequest.findFirst.mockResolvedValue(dbRequest);
  199. mockPrisma.teamRequest.delete.mockResolvedValue(dbRequest);
  200. await teamRequestService.deleteTeamRequest(dbRequest.id);
  201. expect(mockPubSub.publish).toHaveBeenCalledWith(
  202. `team_req/${dbRequest.teamID}/req_deleted`,
  203. dbRequest.id,
  204. );
  205. });
  206. });
  207. describe('createTeamRequest', () => {
  208. test('rejects for invalid collection id', async () => {
  209. mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
  210. E.left(TEAM_INVALID_COLL_ID),
  211. );
  212. const response = await teamRequestService.createTeamRequest(
  213. 'invalidcollid',
  214. team.id,
  215. 'Test Request',
  216. '{}',
  217. );
  218. expect(response).toEqualLeft(TEAM_INVALID_COLL_ID);
  219. expect(mockPrisma.teamRequest.create).not.toHaveBeenCalled();
  220. });
  221. test('resolves for valid collection id', async () => {
  222. const dbRequest = dbTeamRequests[0];
  223. const teamRequest = teamRequests[0];
  224. mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
  225. E.right(team),
  226. );
  227. mockPrisma.teamRequest.create.mockResolvedValue(dbRequest);
  228. const response = teamRequestService.createTeamRequest(
  229. 'testcoll',
  230. team.id,
  231. teamRequest.title,
  232. teamRequest.request,
  233. );
  234. expect(response).resolves.toEqualRight(teamRequest);
  235. });
  236. test('publishes creation to pubsub topic "team_req/<team_id>/req_created"', async () => {
  237. const dbRequest = dbTeamRequests[0];
  238. const teamRequest = teamRequests[0];
  239. mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
  240. E.right(team),
  241. );
  242. mockPrisma.teamRequest.create.mockResolvedValue(dbRequest);
  243. await teamRequestService.createTeamRequest(
  244. 'testcoll',
  245. team.id,
  246. 'Test Request',
  247. '{}',
  248. );
  249. expect(mockPubSub.publish).toHaveBeenCalledWith(
  250. `team_req/${dbRequest.teamID}/req_created`,
  251. teamRequest,
  252. );
  253. });
  254. });
  255. describe('getRequestsInCollection', () => {
  256. test('resolves with an empty array if the collection id does not exist', async () => {
  257. mockPrisma.teamRequest.findMany.mockResolvedValue([]);
  258. await expect(
  259. teamRequestService.getRequestsInCollection('invalidCollID', null, 10),
  260. ).resolves.toEqual([]);
  261. });
  262. test('resolves with the correct info for the collection id and null cursor', async () => {
  263. mockPrisma.teamRequest.findMany.mockResolvedValue(dbTeamRequests);
  264. const response = await teamRequestService.getRequestsInCollection(
  265. 'testcoll',
  266. null,
  267. 10,
  268. );
  269. expect(response).toEqual(teamRequests);
  270. });
  271. test('resolves with the correct info for the collection id and a valid cursor', async () => {
  272. mockPrisma.teamRequest.findMany.mockResolvedValue([dbTeamRequests[1]]);
  273. const response = teamRequestService.getRequestsInCollection(
  274. dbTeamRequests[1].collectionID,
  275. dbTeamRequests[0].id,
  276. 1,
  277. );
  278. expect(response).resolves.toEqual([teamRequests[1]]);
  279. });
  280. });
  281. describe('getRequest', () => {
  282. test('resolves with the correct request info for valid request id', async () => {
  283. mockPrisma.teamRequest.findUnique.mockResolvedValue(dbTeamRequests[0]);
  284. expect(teamRequestService.getRequest('testrequest')).resolves.toEqualSome(
  285. expect.objectContaining(teamRequests[0]),
  286. );
  287. });
  288. test('resolves with null if the request id does not exist', async () => {
  289. mockPrisma.teamRequest.findUnique.mockResolvedValue(null as any);
  290. await expect(
  291. teamRequestService.getRequest('testrequest'),
  292. ).resolves.toBeNone();
  293. });
  294. });
  295. describe('getTeamOfRequest', () => {
  296. test('rejects for invalid team id', async () => {
  297. mockTeamService.getTeamWithID.mockResolvedValue(null as any);
  298. expect(
  299. teamRequestService.getTeamOfRequest(teamRequests[0]),
  300. ).resolves.toEqualLeft(TEAM_INVALID_ID);
  301. });
  302. test('resolves for valid team id', async () => {
  303. mockTeamService.getTeamWithID.mockResolvedValue(team);
  304. expect(
  305. teamRequestService.getTeamOfRequest(teamRequests[0]),
  306. ).resolves.toEqualRight(expect.objectContaining(team));
  307. });
  308. });
  309. describe('getCollectionOfRequest', () => {
  310. test('rejects for invalid collection id', async () => {
  311. mockTeamCollectionService.getCollection.mockResolvedValue(
  312. E.left(TEAM_COLL_NOT_FOUND),
  313. );
  314. expect(
  315. teamRequestService.getCollectionOfRequest(teamRequests[0]),
  316. ).resolves.toEqualLeft(TEAM_INVALID_COLL_ID);
  317. });
  318. test('resolves for valid collection id', async () => {
  319. mockTeamCollectionService.getCollection.mockResolvedValue(
  320. E.right(teamCollection),
  321. );
  322. expect(
  323. teamRequestService.getCollectionOfRequest(teamRequests[0]),
  324. ).resolves.toEqualRight(expect.objectContaining(teamCollection));
  325. });
  326. });
  327. describe('getTeamOfRequestFromID', () => {
  328. test('rejects for invalid request id', async () => {
  329. mockPrisma.teamRequest.findUnique.mockResolvedValue(null as any);
  330. expect(
  331. teamRequestService.getTeamOfRequestFromID('invalidrequest'),
  332. ).resolves.toBeNone();
  333. });
  334. test('resolves for valid request id', async () => {
  335. mockPrisma.teamRequest.findUnique.mockResolvedValue(dbTeamRequests[0]);
  336. expect(
  337. teamRequestService.getTeamOfRequestFromID('testrequest'),
  338. ).resolves.toBeDefined();
  339. });
  340. });
  341. describe('reorderRequests', () => {
  342. test('Should resolve left if transaction throws an error', async () => {
  343. const srcCollID = dbTeamRequests[0].collectionID;
  344. const request = dbTeamRequests[0];
  345. const destCollID = dbTeamRequests[4].collectionID;
  346. const nextRequest = dbTeamRequests[4];
  347. mockPrisma.$transaction.mockRejectedValueOnce(new Error());
  348. const result = await teamRequestService.reorderRequests(
  349. request,
  350. srcCollID,
  351. nextRequest,
  352. destCollID,
  353. );
  354. expect(result).toEqual(E.left(TEAM_REQ_REORDERING_FAILED));
  355. });
  356. test('Should resolve right and call transaction with the correct data', async () => {
  357. const srcCollID = dbTeamRequests[0].collectionID;
  358. const request = dbTeamRequests[0];
  359. const destCollID = dbTeamRequests[4].collectionID;
  360. const nextRequest = dbTeamRequests[4];
  361. const updatedReq: DbTeamRequest = {
  362. ...request,
  363. collectionID: destCollID,
  364. orderIndex: nextRequest.orderIndex,
  365. };
  366. mockPrisma.$transaction.mockResolvedValueOnce(E.right(updatedReq));
  367. const result = await teamRequestService.reorderRequests(
  368. request,
  369. srcCollID,
  370. nextRequest,
  371. destCollID,
  372. );
  373. expect(mockPrisma.$transaction).toHaveBeenCalledWith(expect.any(Function));
  374. expect(result).toEqual(E.right(updatedReq));
  375. });
  376. });
  377. describe('findRequestAndNextRequest', () => {
  378. test('Should resolve right if the request and the next request are found', async () => {
  379. const args: MoveTeamRequestArgs = {
  380. srcCollID: teamRequests[0].collectionID,
  381. destCollID: teamRequests[4].collectionID,
  382. requestID: teamRequests[0].id,
  383. nextRequestID: teamRequests[4].id,
  384. };
  385. mockPrisma.teamRequest.findFirst
  386. .mockResolvedValueOnce(dbTeamRequests[0])
  387. .mockResolvedValueOnce(dbTeamRequests[4]);
  388. const result = await teamRequestService.findRequestAndNextRequest(
  389. args.srcCollID,
  390. args.requestID,
  391. args.destCollID,
  392. args.nextRequestID,
  393. );
  394. expect(result).toEqualRight({
  395. request: dbTeamRequests[0],
  396. nextRequest: dbTeamRequests[4],
  397. });
  398. });
  399. test('Should resolve right if the request and next request null', () => {
  400. const args: MoveTeamRequestArgs = {
  401. srcCollID: teamRequests[0].collectionID,
  402. destCollID: teamRequests[4].collectionID,
  403. requestID: teamRequests[0].id,
  404. nextRequestID: null,
  405. };
  406. mockPrisma.teamRequest.findFirst
  407. .mockResolvedValueOnce(dbTeamRequests[0])
  408. .mockResolvedValueOnce(null);
  409. const result = teamRequestService.findRequestAndNextRequest(
  410. args.srcCollID,
  411. args.requestID,
  412. args.destCollID,
  413. args.nextRequestID,
  414. );
  415. expect(result).resolves.toEqualRight({
  416. request: dbTeamRequests[0],
  417. nextRequest: null,
  418. });
  419. });
  420. test('Should resolve left if the request is not found', () => {
  421. const args: MoveTeamRequestArgs = {
  422. srcCollID: teamRequests[0].collectionID,
  423. destCollID: teamRequests[4].collectionID,
  424. requestID: 'invalid',
  425. nextRequestID: null,
  426. };
  427. mockPrisma.teamRequest.findFirst.mockResolvedValueOnce(null);
  428. const result = teamRequestService.findRequestAndNextRequest(
  429. args.srcCollID,
  430. args.requestID,
  431. args.destCollID,
  432. args.nextRequestID,
  433. );
  434. expect(result).resolves.toEqualLeft(TEAM_REQ_NOT_FOUND);
  435. });
  436. test('Should resolve left if the nextRequest is not found', () => {
  437. const args: MoveTeamRequestArgs = {
  438. srcCollID: teamRequests[0].collectionID,
  439. destCollID: teamRequests[1].collectionID,
  440. requestID: teamRequests[0].id,
  441. nextRequestID: 'invalid',
  442. };
  443. mockPrisma.teamRequest.findFirst
  444. .mockResolvedValueOnce(dbTeamRequests[0])
  445. .mockResolvedValueOnce(null);
  446. const result = teamRequestService.findRequestAndNextRequest(
  447. args.srcCollID,
  448. args.requestID,
  449. args.destCollID,
  450. args.nextRequestID,
  451. );
  452. expect(result).resolves.toEqualLeft(TEAM_REQ_NOT_FOUND);
  453. });
  454. });
  455. describe('moveRequest', () => {
  456. test('Should resolve right and the request', () => {
  457. const args: MoveTeamRequestArgs = {
  458. srcCollID: teamRequests[0].collectionID,
  459. destCollID: teamRequests[0].collectionID,
  460. requestID: teamRequests[0].id,
  461. nextRequestID: null,
  462. };
  463. jest
  464. .spyOn(teamRequestService, 'findRequestAndNextRequest')
  465. .mockResolvedValue(
  466. E.right({ request: dbTeamRequests[0], nextRequest: null }),
  467. );
  468. jest
  469. .spyOn(teamRequestService, 'reorderRequests')
  470. .mockResolvedValue(E.right(dbTeamRequests[0]));
  471. const result = teamRequestService.moveRequest(
  472. args.srcCollID,
  473. args.requestID,
  474. args.destCollID,
  475. args.nextRequestID,
  476. 'moveRequest',
  477. );
  478. expect(result).resolves.toEqualRight(teamRequests[0]);
  479. });
  480. test('Should resolve right and publish message to pubnub if callerFunction is moveRequest', async () => {
  481. const args: MoveTeamRequestArgs = {
  482. srcCollID: teamRequests[0].collectionID,
  483. destCollID: teamRequests[0].collectionID,
  484. requestID: teamRequests[0].id,
  485. nextRequestID: null,
  486. };
  487. jest
  488. .spyOn(teamRequestService, 'findRequestAndNextRequest')
  489. .mockResolvedValue(
  490. E.right({ request: dbTeamRequests[0], nextRequest: null }),
  491. );
  492. jest
  493. .spyOn(teamRequestService, 'reorderRequests')
  494. .mockResolvedValue(E.right(dbTeamRequests[0]));
  495. await teamRequestService.moveRequest(
  496. args.srcCollID,
  497. args.requestID,
  498. args.destCollID,
  499. args.nextRequestID,
  500. 'moveRequest',
  501. );
  502. expect(mockPubSub.publish).toHaveBeenCalledWith(
  503. `team_req/${teamRequests[0].teamID}/req_moved`,
  504. teamRequests[0],
  505. );
  506. });
  507. test('Should resolve right and publish message to pubnub if callerFunction is updateLookUpRequestOrder', async () => {
  508. const args: MoveTeamRequestArgs = {
  509. srcCollID: teamRequests[0].collectionID,
  510. destCollID: teamRequests[0].collectionID,
  511. requestID: teamRequests[0].id,
  512. nextRequestID: null,
  513. };
  514. jest
  515. .spyOn(teamRequestService, 'findRequestAndNextRequest')
  516. .mockResolvedValue(
  517. E.right({ request: dbTeamRequests[0], nextRequest: null }),
  518. );
  519. jest
  520. .spyOn(teamRequestService, 'reorderRequests')
  521. .mockResolvedValue(E.right(dbTeamRequests[0]));
  522. await teamRequestService.moveRequest(
  523. args.srcCollID,
  524. args.requestID,
  525. args.destCollID,
  526. args.nextRequestID,
  527. 'updateLookUpRequestOrder',
  528. );
  529. expect(mockPubSub.publish).toHaveBeenCalledWith(
  530. `team_req/${teamRequests[0].teamID}/req_order_updated`,
  531. { request: teamRequests[0], nextRequest: null },
  532. );
  533. });
  534. test('Should resolve left if finding the requests fails', () => {
  535. const args: MoveTeamRequestArgs = {
  536. srcCollID: teamRequests[0].collectionID,
  537. destCollID: teamRequests[0].collectionID,
  538. requestID: teamRequests[0].id,
  539. nextRequestID: null,
  540. };
  541. jest
  542. .spyOn(teamRequestService, 'findRequestAndNextRequest')
  543. .mockResolvedValue(E.left(TEAM_REQ_NOT_FOUND));
  544. expect(
  545. teamRequestService.moveRequest(
  546. args.srcCollID,
  547. args.requestID,
  548. args.destCollID,
  549. args.nextRequestID,
  550. 'moveRequest',
  551. ),
  552. ).resolves.toEqualLeft(TEAM_REQ_NOT_FOUND);
  553. });
  554. test('Should resolve left if mismatch team/collection of requests fails', () => {
  555. const args: MoveTeamRequestArgs = {
  556. srcCollID: teamRequests[0].collectionID,
  557. destCollID: teamRequests[0].collectionID,
  558. requestID: teamRequests[0].id,
  559. nextRequestID: null,
  560. };
  561. jest
  562. .spyOn(teamRequestService, 'findRequestAndNextRequest')
  563. .mockResolvedValue(E.left(TEAM_REQ_INVALID_TARGET_COLL_ID));
  564. expect(
  565. teamRequestService.moveRequest(
  566. args.srcCollID,
  567. args.requestID,
  568. args.destCollID,
  569. args.nextRequestID,
  570. 'moveRequest',
  571. ),
  572. ).resolves.toEqualLeft(TEAM_REQ_INVALID_TARGET_COLL_ID);
  573. });
  574. test('Should resolve left if reorder fails', () => {
  575. const args: MoveTeamRequestArgs = {
  576. srcCollID: teamRequests[0].collectionID,
  577. destCollID: teamRequests[0].collectionID,
  578. requestID: teamRequests[0].id,
  579. nextRequestID: null,
  580. };
  581. jest
  582. .spyOn(teamRequestService, 'findRequestAndNextRequest')
  583. .mockResolvedValue(
  584. E.right({ request: dbTeamRequests[0], nextRequest: null }),
  585. );
  586. jest
  587. .spyOn(teamRequestService, 'reorderRequests')
  588. .mockResolvedValue(E.left(TEAM_REQ_REORDERING_FAILED));
  589. expect(
  590. teamRequestService.moveRequest(
  591. args.srcCollID,
  592. args.requestID,
  593. args.destCollID,
  594. args.nextRequestID,
  595. 'moveRequest',
  596. ),
  597. ).resolves.toEqualLeft(TEAM_REQ_REORDERING_FAILED);
  598. });
  599. });
  600. describe('totalRequestsInATeam', () => {
  601. test('should resolve right and return a total team reqs count ', async () => {
  602. mockPrisma.teamRequest.count.mockResolvedValueOnce(2);
  603. const result = await teamRequestService.totalRequestsInATeam('id1');
  604. expect(mockPrisma.teamRequest.count).toHaveBeenCalledWith({
  605. where: {
  606. teamID: 'id1',
  607. },
  608. });
  609. expect(result).toEqual(2);
  610. });
  611. test('should resolve left and return an error when no team reqs found', async () => {
  612. mockPrisma.teamRequest.count.mockResolvedValueOnce(0);
  613. const result = await teamRequestService.totalRequestsInATeam('id1');
  614. expect(mockPrisma.teamRequest.count).toHaveBeenCalledWith({
  615. where: {
  616. teamID: 'id1',
  617. },
  618. });
  619. expect(result).toEqual(0);
  620. });
  621. describe('getTeamRequestsCount', () => {
  622. test('should return count of all Team Collections in the organization', async () => {
  623. mockPrisma.teamRequest.count.mockResolvedValueOnce(10);
  624. const result = await teamRequestService.getTeamRequestsCount();
  625. expect(result).toEqual(10);
  626. });
  627. });
  628. });