diff --git a/api/src/services/application-flagged-set.service.ts b/api/src/services/application-flagged-set.service.ts index 4e234aa998..64d03bbaa3 100644 --- a/api/src/services/application-flagged-set.service.ts +++ b/api/src/services/application-flagged-set.service.ts @@ -612,7 +612,11 @@ export class ApplicationFlaggedSetService implements OnModuleInit { }); const builtRuleKey = this.buildRuleKey(application, rule, listingId); - if (!alreadyFoundMatch && applicationsThatMatched.length) { + if ( + !alreadyFoundMatch && + applicationsThatMatched.length && + builtRuleKey + ) { // if there were duplicates (application could be a part of a flagged set) if (flagSetsThisAppBelongsTo.length) { // if application is part of a flagged set already @@ -672,9 +676,12 @@ export class ApplicationFlaggedSetService implements OnModuleInit { ): string { if (rule == RuleEnum.email) { return `${listingId}-email-${application.applicant.emailAddress}`; - } else { + } else if ( + application.applicant.firstName && + application.applicant.lastName + ) { return ( - `${listingId}-nameAndDOB-${application.applicant.firstName.toLowerCase()}-${application.applicant.lastName.toLowerCase()}` + + `${listingId}-nameAndDOB-${application.applicant.firstName?.toLowerCase()}-${application.applicant.lastName?.toLowerCase()}` + `-${application.applicant.birthMonth}-${application.applicant.birthDay}-${application.applicant.birthYear}` ); } diff --git a/api/test/unit/services/application-csv-export.service.spec.ts b/api/test/unit/services/application-csv-export.service.spec.ts index 12880b7cad..7d390eda76 100644 --- a/api/test/unit/services/application-csv-export.service.spec.ts +++ b/api/test/unit/services/application-csv-export.service.spec.ts @@ -559,7 +559,8 @@ describe('Testing application CSV export service', () => { expect(readable).toContain(firstApp); }); - it('should build csv with submission date defaulted to PST', async () => { + it('should build csv with submission date defaulted to TIME_ZONE variable', async () => { + process.env.TIME_ZONE = 'America/Los_Angeles'; jest.useFakeTimers(); jest.setSystemTime(new Date('2024-01-01')); diff --git a/api/test/unit/services/application-flagged-set.service.spec.ts b/api/test/unit/services/application-flagged-set.service.spec.ts index e293d1538d..3c86254544 100644 --- a/api/test/unit/services/application-flagged-set.service.spec.ts +++ b/api/test/unit/services/application-flagged-set.service.spec.ts @@ -196,263 +196,102 @@ describe('Testing application flagged set service', () => { }; }; - it('should build where clause with listingId filter only', async () => { - expect(await service.buildWhere({ listingId: 'example id' })).toEqual({ - AND: [ - { - listingId: 'example id', - }, - ], + describe('Test buildWhere', () => { + it('should build where clause with listingId filter only', async () => { + expect(await service.buildWhere({ listingId: 'example id' })).toEqual({ + AND: [ + { + listingId: 'example id', + }, + ], + }); }); - }); - it('should build where clause with listingId and view of pending', async () => { - expect( - await service.buildWhere({ listingId: 'example id', view: View.pending }), - ).toEqual({ - AND: [ - { + it('should build where clause with listingId and view of pending', async () => { + expect( + await service.buildWhere({ listingId: 'example id', - }, - { - status: FlaggedSetStatusEnum.pending, - }, - ], + view: View.pending, + }), + ).toEqual({ + AND: [ + { + listingId: 'example id', + }, + { + status: FlaggedSetStatusEnum.pending, + }, + ], + }); }); - }); - it('should build where clause with listingId and view of pending with search', async () => { - expect( - await service.buildWhere({ - listingId: 'example id', - view: View.pending, - search: 'simple search', - }), - ).toEqual({ - AND: [ - { + it('should build where clause with listingId and view of pending with search', async () => { + expect( + await service.buildWhere({ listingId: 'example id', - }, - { - status: FlaggedSetStatusEnum.pending, - }, - { - applications: { - some: { - applicant: { - OR: [ - { - firstName: { - contains: 'simple search', - mode: 'insensitive', + view: View.pending, + search: 'simple search', + }), + ).toEqual({ + AND: [ + { + listingId: 'example id', + }, + { + status: FlaggedSetStatusEnum.pending, + }, + { + applications: { + some: { + applicant: { + OR: [ + { + firstName: { + contains: 'simple search', + mode: 'insensitive', + }, }, - }, - { - lastName: { - contains: 'simple search', - mode: 'insensitive', + { + lastName: { + contains: 'simple search', + mode: 'insensitive', + }, }, - }, - ], + ], + }, }, }, }, - }, - ], - }); - }); - - it('should build where clause with listingId and view of pendingNameAndDoB', async () => { - expect( - await service.buildWhere({ - listingId: 'example id', - view: View.pendingNameAndDoB, - }), - ).toEqual({ - AND: [ - { - listingId: 'example id', - }, - { - status: FlaggedSetStatusEnum.pending, - rule: RuleEnum.nameAndDOB, - }, - ], - }); - }); - - it('should build where clause with listingId and view of pendingEmail', async () => { - expect( - await service.buildWhere({ - listingId: 'example id', - view: View.pendingEmail, - }), - ).toEqual({ - AND: [ - { - listingId: 'example id', - }, - { - status: FlaggedSetStatusEnum.pending, - rule: RuleEnum.email, - }, - ], + ], + }); }); - }); - it('should build where clause with listingId and view of resolved', async () => { - expect( - await service.buildWhere({ - listingId: 'example id', - view: View.resolved, - }), - ).toEqual({ - AND: [ - { + it('should build where clause with listingId and view of pendingNameAndDoB', async () => { + expect( + await service.buildWhere({ listingId: 'example id', - }, - { - status: FlaggedSetStatusEnum.resolved, - }, - ], - }); - }); - - it('should build meta data helper query with status and rule arguments present', async () => { - prisma.applicationFlaggedSet.count = jest.fn().mockResolvedValue(1); - expect( - await service.metaDataQueryBuilder( - 'example id', - FlaggedSetStatusEnum.pending, - RuleEnum.email, - ), - ).toEqual(1); - expect(prisma.applicationFlaggedSet.count).toHaveBeenCalledWith({ - where: { - listingId: 'example id', - status: FlaggedSetStatusEnum.pending, - rule: RuleEnum.email, - }, - }); - }); - - it('should build meta data helper query without status or rule arguments', async () => { - prisma.applicationFlaggedSet.count = jest.fn().mockResolvedValue(1); - expect(await service.metaDataQueryBuilder('example id')).toEqual(1); - expect(prisma.applicationFlaggedSet.count).toHaveBeenCalledWith({ - where: { - listingId: 'example id', - }, - }); - }); - - it('should build rule key when rule is email', async () => { - expect( - await service.buildRuleKey( - { - applicant: { - emailAddress: 'email address', - }, - } as unknown as Application, - RuleEnum.email, - 'example id', - ), - ).toEqual('example id-email-email address'); - }); - - it('should build rule key when rule is nameAndDOB', async () => { - expect( - await service.buildRuleKey( - { - applicant: { - firstName: 'first name', - lastName: 'last name', - birthMonth: 5, - birthDay: 6, - birthYear: 2000, - }, - } as unknown as Application, - RuleEnum.nameAndDOB, - 'example id', - ), - ).toEqual('example id-nameAndDOB-first name-last name-5-6-2000'); - }); - - it('should build rule key in lowercase when rule is nameAndDOB', async () => { - expect( - await service.buildRuleKey( - { - applicant: { - firstName: 'FIRST Name', - lastName: 'lAsT nAMe', - birthMonth: 5, - birthDay: 6, - birthYear: 2000, - }, - } as unknown as Application, - RuleEnum.nameAndDOB, - 'example id', - ), - ).toEqual('example id-nameAndDOB-first name-last name-5-6-2000'); - }); - - it('should get a list of flagged sets when view is pendingEmail', async () => { - const mockCount = jest - .fn() - .mockResolvedValueOnce(1) - .mockResolvedValueOnce(2); - prisma.applicationFlaggedSet.count = mockCount; - prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([ - { - id: 'example id', - }, - ]); - expect( - await service.list({ listingId: 'example id', view: View.pendingEmail }), - ).toEqual({ - items: [ - { - id: 'example id', - }, - ], - meta: { - currentPage: 1, - itemCount: 1, - itemsPerPage: 1, - totalItems: 1, - totalPages: 1, - totalFlagged: 2, - }, - }); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ - include: { - listings: true, - applications: { - include: { - applicant: true, - }, - }, - }, - where: { + view: View.pendingNameAndDoB, + }), + ).toEqual({ AND: [ { listingId: 'example id', }, { status: FlaggedSetStatusEnum.pending, - rule: RuleEnum.email, + rule: RuleEnum.nameAndDOB, }, ], - }, - orderBy: { - id: OrderByEnum.DESC, - }, - skip: 0, + }); }); - expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(1, { - where: { + it('should build where clause with listingId and view of pendingEmail', async () => { + expect( + await service.buildWhere({ + listingId: 'example id', + view: View.pendingEmail, + }), + ).toEqual({ AND: [ { listingId: 'example id', @@ -462,1507 +301,1595 @@ describe('Testing application flagged set service', () => { rule: RuleEnum.email, }, ], - }, + }); }); - expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(2, { - where: { - listingId: 'example id', - status: FlaggedSetStatusEnum.pending, - }, + it('should build where clause with listingId and view of resolved', async () => { + expect( + await service.buildWhere({ + listingId: 'example id', + view: View.resolved, + }), + ).toEqual({ + AND: [ + { + listingId: 'example id', + }, + { + status: FlaggedSetStatusEnum.resolved, + }, + ], + }); }); }); - it('should get a flagged set', async () => { - prisma.applicationFlaggedSet.findUnique = jest.fn().mockResolvedValue({ - id: 'example id', - }); - expect(await service.findOne('example id')).toEqual({ - id: 'example id', - }); - - expect(prisma.applicationFlaggedSet.findUnique).toHaveBeenCalledWith({ - include: { - applications: { - include: { - applicant: true, - }, + describe('Test metaDataQueryBuilder', () => { + it('should build meta data helper query with status and rule arguments present', async () => { + prisma.applicationFlaggedSet.count = jest.fn().mockResolvedValue(1); + expect( + await service.metaDataQueryBuilder( + 'example id', + FlaggedSetStatusEnum.pending, + RuleEnum.email, + ), + ).toEqual(1); + expect(prisma.applicationFlaggedSet.count).toHaveBeenCalledWith({ + where: { + listingId: 'example id', + status: FlaggedSetStatusEnum.pending, + rule: RuleEnum.email, }, - listings: true, - }, - where: { - id: 'example id', - }, + }); }); - }); - - it('should error getting a flagged set that does not exist', async () => { - prisma.applicationFlaggedSet.findUnique = jest.fn().mockResolvedValue(null); - await expect( - async () => await service.findOne('example id'), - ).rejects.toThrowError( - 'applicationFlaggedSetId example id was requested but not found', - ); - expect(prisma.applicationFlaggedSet.findUnique).toHaveBeenCalledWith({ - include: { - applications: { - include: { - applicant: true, - }, + it('should build meta data helper query without status or rule arguments', async () => { + prisma.applicationFlaggedSet.count = jest.fn().mockResolvedValue(1); + expect(await service.metaDataQueryBuilder('example id')).toEqual(1); + expect(prisma.applicationFlaggedSet.count).toHaveBeenCalledWith({ + where: { + listingId: 'example id', }, - listings: true, - }, - where: { - id: 'example id', - }, + }); }); }); - it('should update showConfirmationAlert', async () => { - prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ - id: 'example id', - }); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ - id: 'example id', - }); - expect(await service.resetConfirmationAlert('example id')).toEqual({ - success: true, + describe('Test buildRuleKey', () => { + it('should build rule key when rule is email', async () => { + expect( + await service.buildRuleKey( + { + applicant: { + emailAddress: 'email address', + }, + } as unknown as Application, + RuleEnum.email, + 'example id', + ), + ).toEqual('example id-email-email address'); }); - expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ - where: { - id: 'example id', - }, + it('should build rule key when rule is nameAndDOB', async () => { + expect( + await service.buildRuleKey( + { + applicant: { + firstName: 'first name', + lastName: 'last name', + birthMonth: 5, + birthDay: 6, + birthYear: 2000, + }, + } as unknown as Application, + RuleEnum.nameAndDOB, + 'example id', + ), + ).toEqual('example id-nameAndDOB-first name-last name-5-6-2000'); }); - expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ - data: { - showConfirmationAlert: false, - }, - where: { - id: 'example id', - }, + it('should build rule key in lowercase when rule is nameAndDOB', async () => { + expect( + await service.buildRuleKey( + { + applicant: { + firstName: 'FIRST Name', + lastName: 'lAsT nAMe', + birthMonth: 5, + birthDay: 6, + birthYear: 2000, + }, + } as unknown as Application, + RuleEnum.nameAndDOB, + 'example id', + ), + ).toEqual('example id-nameAndDOB-first name-last name-5-6-2000'); }); - }); - - it('should error updating showConfirmationAlert for flagged set that does not exist', async () => { - prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue(null); - await expect( - async () => await service.resetConfirmationAlert('example id'), - ).rejects.toThrowError( - 'applicationFlaggedSet example id was requested but not found', - ); - expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ - where: { - id: 'example id', - }, + it('should build rule key if name does not exist', async () => { + expect( + await service.buildRuleKey( + { + applicant: { + firstName: undefined, + lastName: undefined, + birthMonth: 5, + birthDay: 6, + birthYear: 2000, + }, + } as unknown as Application, + RuleEnum.nameAndDOB, + 'example id', + ), + ).toEqual(undefined); }); }); - it('should grab meta data', async () => { - const mockCount = jest - .fn() - .mockResolvedValueOnce(1) - .mockResolvedValueOnce(2) - .mockResolvedValueOnce(3); - - prisma.applicationFlaggedSet.count = mockCount; - prisma.applications.count = jest.fn().mockResolvedValueOnce(12); - - expect(await service.meta({ listingId: 'example id' })).toEqual({ - totalCount: 12, - totalResolvedCount: 1, - totalPendingCount: 5, - totalNamePendingCount: 2, - totalEmailPendingCount: 3, - }); - - expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(1, { - where: { - listingId: 'example id', - status: FlaggedSetStatusEnum.resolved, - }, - }); + describe('Test list', () => { + it('should get a list of flagged sets when view is pendingEmail', async () => { + const mockCount = jest + .fn() + .mockResolvedValueOnce(1) + .mockResolvedValueOnce(2); + prisma.applicationFlaggedSet.count = mockCount; + prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([ + { + id: 'example id', + }, + ]); + expect( + await service.list({ + listingId: 'example id', + view: View.pendingEmail, + }), + ).toEqual({ + items: [ + { + id: 'example id', + }, + ], + meta: { + currentPage: 1, + itemCount: 1, + itemsPerPage: 1, + totalItems: 1, + totalPages: 1, + totalFlagged: 2, + }, + }); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ + include: { + listings: true, + applications: { + include: { + applicant: true, + }, + }, + }, + where: { + AND: [ + { + listingId: 'example id', + }, + { + status: FlaggedSetStatusEnum.pending, + rule: RuleEnum.email, + }, + ], + }, + orderBy: { + id: OrderByEnum.DESC, + }, + skip: 0, + }); - expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(2, { - where: { - listingId: 'example id', - status: FlaggedSetStatusEnum.pending, - rule: RuleEnum.nameAndDOB, - }, - }); + expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(1, { + where: { + AND: [ + { + listingId: 'example id', + }, + { + status: FlaggedSetStatusEnum.pending, + rule: RuleEnum.email, + }, + ], + }, + }); - expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(3, { - where: { - listingId: 'example id', - status: FlaggedSetStatusEnum.pending, - rule: RuleEnum.email, - }, + expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(2, { + where: { + listingId: 'example id', + status: FlaggedSetStatusEnum.pending, + }, + }); }); }); - it('should return true for flagged set that exists', async () => { - prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ - id: 'example id', + describe('Test findOne', () => { + it('should get a flagged set', async () => { + prisma.applicationFlaggedSet.findUnique = jest.fn().mockResolvedValue({ + id: 'example id', + }); + expect(await service.findOne('example id')).toEqual({ + id: 'example id', + }); + + expect(prisma.applicationFlaggedSet.findUnique).toHaveBeenCalledWith({ + include: { + applications: { + include: { + applicant: true, + }, + }, + listings: true, + }, + where: { + id: 'example id', + }, + }); }); - expect(await service.findOrThrow('example id')).toEqual(true); + it('should error getting a flagged set that does not exist', async () => { + prisma.applicationFlaggedSet.findUnique = jest + .fn() + .mockResolvedValue(null); + await expect( + async () => await service.findOne('example id'), + ).rejects.toThrowError( + 'applicationFlaggedSetId example id was requested but not found', + ); - expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ - where: { - id: 'example id', - }, + expect(prisma.applicationFlaggedSet.findUnique).toHaveBeenCalledWith({ + include: { + applications: { + include: { + applicant: true, + }, + }, + listings: true, + }, + where: { + id: 'example id', + }, + }); }); }); - it('should error getting a flagged set that does not exist', async () => { - prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue(null); - await expect( - async () => await service.findOrThrow('example id'), - ).rejects.toThrowError( - 'applicationFlaggedSet example id was requested but not found', - ); - - expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ - where: { + describe('Test resetConfirmationAlert', () => { + it('should update showConfirmationAlert', async () => { + prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ id: 'example id', - }, - }); - }); + }); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ + id: 'example id', + }); + expect(await service.resetConfirmationAlert('example id')).toEqual({ + success: true, + }); - it('should mark existing job as begun', async () => { - prisma.cronJob.findFirst = jest.fn().mockResolvedValue({ - id: 'example id', - }); + expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ + where: { + id: 'example id', + }, + }); - prisma.cronJob.update = jest.fn().mockResolvedValue({ - id: 'example id', + expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ + data: { + showConfirmationAlert: false, + }, + where: { + id: 'example id', + }, + }); }); - await service.markCronJobAsStarted(); - - expect(prisma.cronJob.findFirst).toHaveBeenCalledWith({ - where: { - name: 'AFS_CRON_JOB', - }, - }); + it('should error updating showConfirmationAlert for flagged set that does not exist', async () => { + prisma.applicationFlaggedSet.findFirst = jest + .fn() + .mockResolvedValue(null); + await expect( + async () => await service.resetConfirmationAlert('example id'), + ).rejects.toThrowError( + 'applicationFlaggedSet example id was requested but not found', + ); - expect(prisma.cronJob.update).toHaveBeenCalledWith({ - data: { - lastRunDate: expect.anything(), - }, - where: { - id: 'example id', - }, + expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ + where: { + id: 'example id', + }, + }); }); }); - it('should create cronjob as begun', async () => { - prisma.cronJob.findFirst = jest.fn().mockResolvedValue(null); - - prisma.cronJob.create = jest.fn().mockResolvedValue({ - id: 'example id', - }); - - await service.markCronJobAsStarted(); + describe('Test meta', () => { + it('should grab meta data', async () => { + const mockCount = jest + .fn() + .mockResolvedValueOnce(1) + .mockResolvedValueOnce(2) + .mockResolvedValueOnce(3); + + prisma.applicationFlaggedSet.count = mockCount; + prisma.applications.count = jest.fn().mockResolvedValueOnce(12); + + expect(await service.meta({ listingId: 'example id' })).toEqual({ + totalCount: 12, + totalResolvedCount: 1, + totalPendingCount: 5, + totalNamePendingCount: 2, + totalEmailPendingCount: 3, + }); + + expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(1, { + where: { + listingId: 'example id', + status: FlaggedSetStatusEnum.resolved, + }, + }); - expect(prisma.cronJob.findFirst).toHaveBeenCalledWith({ - where: { - name: 'AFS_CRON_JOB', - }, - }); + expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(2, { + where: { + listingId: 'example id', + status: FlaggedSetStatusEnum.pending, + rule: RuleEnum.nameAndDOB, + }, + }); - expect(prisma.cronJob.create).toHaveBeenCalledWith({ - data: { - lastRunDate: expect.anything(), - name: 'AFS_CRON_JOB', - }, + expect(prisma.applicationFlaggedSet.count).toHaveBeenNthCalledWith(3, { + where: { + listingId: 'example id', + status: FlaggedSetStatusEnum.pending, + rule: RuleEnum.email, + }, + }); }); }); - it('should get matching applications based on email', async () => { - prisma.applications.findMany = jest - .fn() - .mockResolvedValue([{ id: 'example id 1' }, { id: 'example id 2' }]); + describe('Test findOrThrow', () => { + it('should return true for flagged set that exists', async () => { + prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ + id: 'example id', + }); - expect( - await service.checkAgainstEmail( - testApplicationInfo('example id 3'), - 'example listing id', - ), - ).toEqual([ - { - id: 'example id 1', - }, - { - id: 'example id 2', - }, - ]); + expect(await service.findOrThrow('example id')).toEqual(true); - expect(prisma.applications.findMany).toHaveBeenCalledWith({ - select: { - id: true, - }, - where: { - id: { - not: 'example id 3', - }, - status: ApplicationStatusEnum.submitted, - listingId: 'example listing id', - applicant: { - emailAddress: 'example email', + expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ + where: { + id: 'example id', }, - }, + }); }); - }); - - it('should get [] when applicant email is empty', async () => { - prisma.applications.findMany = jest.fn(); - - expect( - await service.checkAgainstEmail( - { - id: 'example id 3', - } as unknown as Application, - 'example listing id', - ), - ).toEqual([]); - - expect(prisma.applications.findMany).not.toHaveBeenCalled(); - }); - it('should get matching applications based on nameAndDOB', async () => { - prisma.applications.findMany = jest.fn().mockResolvedValue([ - { - id: 'example id 1', - }, - { - id: 'Example id 2', - }, - ]); - - expect( - await service.checkAgainstNameAndDOB( - testApplicationInfo('example id 3'), - 'example listing id', - ), - ).toEqual([ - { - id: 'example id 1', - }, - { - id: 'Example id 2', - }, - ]); + it('should error getting a flagged set that does not exist', async () => { + prisma.applicationFlaggedSet.findFirst = jest + .fn() + .mockResolvedValue(null); + await expect( + async () => await service.findOrThrow('example id'), + ).rejects.toThrowError( + 'applicationFlaggedSet example id was requested but not found', + ); - expect(prisma.applications.findMany).toHaveBeenCalledWith({ - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('example id 3', 'example listing id'), + expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ + where: { + id: 'example id', + }, + }); }); }); - it('should delete flagged set', async () => { - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue({ - id: 'example id 1', - }); + describe('Test markCronJobAsStarted', () => { + it('should mark existing job as begun', async () => { + prisma.cronJob.findFirst = jest.fn().mockResolvedValue({ + id: 'example id', + }); - prisma.applications.update = jest.fn().mockResolvedValue({ - id: 'example id 1', - }); + prisma.cronJob.update = jest.fn().mockResolvedValue({ + id: 'example id', + }); - await service.disconnectApplicationFromFlaggedSet( - 'example afs id', - 2, - 'example application id', - ); + await service.markCronJobAsStarted(); - expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ - where: { - id: 'example afs id', - }, - }); + expect(prisma.cronJob.findFirst).toHaveBeenCalledWith({ + where: { + name: 'AFS_CRON_JOB', + }, + }); - expect(prisma.applications.update).toHaveBeenCalledWith({ - data: { - markedAsDuplicate: false, - reviewStatus: ApplicationReviewStatusEnum.valid, - }, - where: { - id: 'example application id', - }, + expect(prisma.cronJob.update).toHaveBeenCalledWith({ + data: { + lastRunDate: expect.anything(), + }, + where: { + id: 'example id', + }, + }); }); - }); - it('should remove application from flagged set', async () => { - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ - id: 'example id 1', - }); + it('should create cronjob as begun', async () => { + prisma.cronJob.findFirst = jest.fn().mockResolvedValue(null); - prisma.applications.update = jest.fn().mockResolvedValue({ - id: 'example id 1', - }); + prisma.cronJob.create = jest.fn().mockResolvedValue({ + id: 'example id', + }); - await service.disconnectApplicationFromFlaggedSet( - 'example afs id', - 3, - 'example application id', - ); + await service.markCronJobAsStarted(); - expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ - where: { - id: 'example afs id', - }, - data: { - applications: { - disconnect: { - id: 'example application id', - }, + expect(prisma.cronJob.findFirst).toHaveBeenCalledWith({ + where: { + name: 'AFS_CRON_JOB', }, - }, - }); + }); - expect(prisma.applications.update).toHaveBeenCalledWith({ - data: { - markedAsDuplicate: false, - reviewStatus: ApplicationReviewStatusEnum.valid, - }, - where: { - id: 'example application id', - }, + expect(prisma.cronJob.create).toHaveBeenCalledWith({ + data: { + lastRunDate: expect.anything(), + name: 'AFS_CRON_JOB', + }, + }); }); }); - it('should create a new flagged set', async () => { - prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ - id: 'example id 1', - }); + describe('Test checkAgainstEmail', () => { + it('should get matching applications based on email', async () => { + prisma.applications.findMany = jest + .fn() + .mockResolvedValue([{ id: 'example id 1' }, { id: 'example id 2' }]); - await service.createOrConnectToFlaggedSet( - RuleEnum.email, - 'example rule key', - 'example listing id', - [ + expect( + await service.checkAgainstEmail( + testApplicationInfo('example id 3'), + 'example listing id', + ), + ).toEqual([ { id: 'example id 1', }, { id: 'example id 2', }, - { - id: 'example id 3', - }, - ], - ); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ - where: { - listingId: 'example listing id', - ruleKey: 'example rule key', - }, - }); + ]); - expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ - data: { - rule: RuleEnum.email, - ruleKey: 'example rule key', - resolvedTime: null, - status: FlaggedSetStatusEnum.pending, - listings: { - connect: { - id: 'example listing id', - }, + expect(prisma.applications.findMany).toHaveBeenCalledWith({ + select: { + id: true, }, - applications: { - connect: [ - { - id: 'example id 1', - }, - { - id: 'example id 2', - }, - { - id: 'example id 3', - }, - ], + where: { + id: { + not: 'example id 3', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'example listing id', + applicant: { + emailAddress: 'example email', + }, }, - }, + }); }); - }); - it('should add applications to flagged set', async () => { - prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([ - { - id: 'example afs id', - }, - ]); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ - id: 'example id 1', + it('should get [] when applicant email is empty', async () => { + prisma.applications.findMany = jest.fn(); + + expect( + await service.checkAgainstEmail( + { + id: 'example id 3', + } as unknown as Application, + 'example listing id', + ), + ).toEqual([]); + + expect(prisma.applications.findMany).not.toHaveBeenCalled(); }); + }); - await service.createOrConnectToFlaggedSet( - RuleEnum.email, - 'example rule key', - 'example listing id', - [ + describe('Test checkAgainstNameAndDOB', () => { + it('should get matching applications based on nameAndDOB', async () => { + prisma.applications.findMany = jest.fn().mockResolvedValue([ { id: 'example id 1', }, { - id: 'example id 2', + id: 'Example id 2', + }, + ]); + + expect( + await service.checkAgainstNameAndDOB( + testApplicationInfo('example id 3'), + 'example listing id', + ), + ).toEqual([ + { + id: 'example id 1', }, { - id: 'example id 3', + id: 'Example id 2', }, - ], - ); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ - where: { - listingId: 'example listing id', - ruleKey: 'example rule key', - }, - }); + ]); - expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ - data: { - applications: { - connect: [ - { - id: 'example id 1', - }, - { - id: 'example id 2', - }, - { - id: 'example id 3', - }, - ], + expect(prisma.applications.findMany).toHaveBeenCalledWith({ + select: { + id: true, }, - // regardless of former status the afs should be reviewed again - status: FlaggedSetStatusEnum.pending, - resolvedTime: null, - resolvingUserId: null, - }, - where: { - id: 'example afs id', - ruleKey: 'example rule key', - }, + where: whereClauseForNameAndDOBTest( + 'example id 3', + 'example listing id', + ), + }); }); }); - it('should get matching applications checking against email', async () => { - prisma.applications.findMany = jest - .fn() - .mockResolvedValue([{ id: 'example id 1' }, { id: 'example id 2' }]); + describe('Test disconnectApplicationFromFlaggedSet', () => { + it('should delete flagged set', async () => { + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue({ + id: 'example id 1', + }); - expect( - await service.checkForMatchesAgainstRule( - { - id: 'example id 3', - applicant: { - emailAddress: 'example email', - }, - } as unknown as Application, - RuleEnum.email, - 'example listing id', - ), - ).toEqual([ - { + prisma.applications.update = jest.fn().mockResolvedValue({ id: 'example id 1', - }, - { - id: 'example id 2', - }, - ]); + }); - expect(prisma.applications.findMany).toHaveBeenCalledWith({ - select: { - id: true, - }, - where: { - id: { - not: 'example id 3', + await service.disconnectApplicationFromFlaggedSet( + 'example afs id', + 2, + 'example application id', + ); + + expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ + where: { + id: 'example afs id', + }, + }); + + expect(prisma.applications.update).toHaveBeenCalledWith({ + data: { + markedAsDuplicate: false, + reviewStatus: ApplicationReviewStatusEnum.valid, }, - status: ApplicationStatusEnum.submitted, - listingId: 'example listing id', - applicant: { - emailAddress: 'example email', + where: { + id: 'example application id', }, - }, + }); }); - }); - it('should get matching applications checking against nameAndDOB', async () => { - prisma.applications.findMany = jest.fn().mockResolvedValue([ - { + it('should remove application from flagged set', async () => { + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ id: 'example id 1', - }, - { - id: 'Example id 2', - }, - ]); + }); - expect( - await service.checkForMatchesAgainstRule( - testApplicationInfo('example id 3'), - RuleEnum.nameAndDOB, - 'example listing id', - ), - ).toEqual([ - { + prisma.applications.update = jest.fn().mockResolvedValue({ id: 'example id 1', - }, - { - id: 'Example id 2', - }, - ]); + }); - expect(prisma.applications.findMany).toHaveBeenCalledWith({ - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('example id 3', 'example listing id'), + await service.disconnectApplicationFromFlaggedSet( + 'example afs id', + 3, + 'example application id', + ); + + expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ + where: { + id: 'example afs id', + }, + data: { + applications: { + disconnect: { + id: 'example application id', + }, + }, + }, + }); + + expect(prisma.applications.update).toHaveBeenCalledWith({ + data: { + markedAsDuplicate: false, + reviewStatus: ApplicationReviewStatusEnum.valid, + }, + where: { + id: 'example application id', + }, + }); }); }); - it('should error trying to resolve flagged set on open listing', async () => { - prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ - id: 'example id', - listings: { - id: 'listing id', - status: ListingsStatusEnum.active, - }, - }); - await expect( - async () => - await service.resolve( + describe('Test createOrConnectToFlaggedSet', () => { + it('should create a new flagged set', async () => { + prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ + id: 'example id 1', + }); + + await service.createOrConnectToFlaggedSet( + RuleEnum.email, + 'example rule key', + 'example listing id', + [ { - afsId: 'example id', - status: FlaggedSetStatusEnum.resolved, - applications: [], + id: 'example id 1', }, { - id: 'user id', - } as unknown as User, - ), - ).rejects.toThrowError( - 'Listing listing id must be closed before resolving any duplicates', - ); - - expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ - where: { - AND: [ + id: 'example id 2', + }, { - id: 'example id', + id: 'example id 3', }, ], - }, - include: { - listings: true, - applications: { - where: { - id: { - in: [], - }, - }, - }, - }, - }); - }); + ); - it('should resolve flagged set to pending', async () => { - prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ - id: 'example id', - listings: { - id: 'listing id', - status: ListingsStatusEnum.closed, - }, - applications: [ - { - id: 'app id 1', - }, - { - id: 'app id 2', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ + where: { + listingId: 'example listing id', + ruleKey: 'example rule key', }, - ], - }); - prisma.applications.updateMany = jest.fn().mockResolvedValue({ - id: 'example id', - }); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ - id: 'example id', - }); + }); - expect( - await service.resolve( - { - afsId: 'example id', + expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ + data: { + rule: RuleEnum.email, + ruleKey: 'example rule key', + resolvedTime: null, status: FlaggedSetStatusEnum.pending, - applications: [ - { - id: 'app id 1', - }, - { - id: 'app id 2', + listings: { + connect: { + id: 'example listing id', }, - ], - }, - { - id: 'user id', - } as unknown as User, - ), - ).toEqual({ - id: 'example id', - applications: [ - { - id: 'app id 1', + }, + applications: { + connect: [ + { + id: 'example id 1', + }, + { + id: 'example id 2', + }, + { + id: 'example id 3', + }, + ], + }, }, + }); + }); + + it('should add applications to flagged set', async () => { + prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([ { - id: 'app id 2', + id: 'example afs id', }, - ], - }); + ]); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ + id: 'example id 1', + }); - expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ - where: { - AND: [ + await service.createOrConnectToFlaggedSet( + RuleEnum.email, + 'example rule key', + 'example listing id', + [ { - id: 'example id', + id: 'example id 1', }, { - applications: { - some: { - id: { - in: ['app id 1', 'app id 2'], - }, - }, - }, + id: 'example id 2', }, - ], - }, - include: { - listings: true, - applications: { - where: { - id: { - in: ['app id 1', 'app id 2'], - }, + { + id: 'example id 3', }, - }, - }, - }); + ], + ); - expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(1, { - data: { - reviewStatus: ApplicationReviewStatusEnum.pendingAndValid, - markedAsDuplicate: false, - }, - where: { - id: { - in: ['app id 1', 'app id 2'], + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ + where: { + listingId: 'example listing id', + ruleKey: 'example rule key', }, - }, - }); + }); - expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(2, { - data: { - reviewStatus: ApplicationReviewStatusEnum.pending, - markedAsDuplicate: false, - }, - where: { - applicationFlaggedSet: { - some: { - id: 'example id', + expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ + data: { + applications: { + connect: [ + { + id: 'example id 1', + }, + { + id: 'example id 2', + }, + { + id: 'example id 3', + }, + ], }, + // regardless of former status the afs should be reviewed again + status: FlaggedSetStatusEnum.pending, + resolvedTime: null, + resolvingUserId: null, }, - id: { - notIn: ['app id 1', 'app id 2'], - }, - }, - }); - - expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ - where: { - id: 'example id', - }, - data: { - resolvedTime: expect.anything(), - status: FlaggedSetStatusEnum.pending, - showConfirmationAlert: false, - userAccounts: { - connect: { - id: 'user id', - }, + where: { + id: 'example afs id', + ruleKey: 'example rule key', }, - }, + }); }); }); - it('should resolve flagged set to resolved', async () => { - prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ - id: 'example id', - listings: { - id: 'listing id', - status: ListingsStatusEnum.closed, - }, - applications: [ + describe('Test checkForMatchesAgainstRule', () => { + it('should get matching applications checking against email', async () => { + prisma.applications.findMany = jest + .fn() + .mockResolvedValue([{ id: 'example id 1' }, { id: 'example id 2' }]); + + expect( + await service.checkForMatchesAgainstRule( + { + id: 'example id 3', + applicant: { + emailAddress: 'example email', + }, + } as unknown as Application, + RuleEnum.email, + 'example listing id', + ), + ).toEqual([ { - id: 'app id 1', + id: 'example id 1', }, { - id: 'app id 2', + id: 'example id 2', }, - ], - }); - prisma.applications.updateMany = jest.fn().mockResolvedValue({ - id: 'example id', - }); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ - id: 'example id', - }); + ]); - expect( - await service.resolve( - { - afsId: 'example id', - status: FlaggedSetStatusEnum.resolved, - applications: [ - { - id: 'app id 1', - }, - { - id: 'app id 2', - }, - ], - }, - { - id: 'user id', - } as unknown as User, - ), - ).toEqual({ - id: 'example id', - applications: [ - { - id: 'app id 1', - }, - { - id: 'app id 2', + expect(prisma.applications.findMany).toHaveBeenCalledWith({ + select: { + id: true, }, - ], - }); - - expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ - where: { - AND: [ - { - id: 'example id', - }, - { - applications: { - some: { - id: { - in: ['app id 1', 'app id 2'], - }, - }, - }, + where: { + id: { + not: 'example id 3', }, - ], - }, - include: { - listings: true, - applications: { - where: { - id: { - in: ['app id 1', 'app id 2'], - }, + status: ApplicationStatusEnum.submitted, + listingId: 'example listing id', + applicant: { + emailAddress: 'example email', }, }, - }, + }); }); - expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(1, { - data: { - reviewStatus: ApplicationReviewStatusEnum.valid, - markedAsDuplicate: false, - }, - where: { - id: { - in: ['app id 1', 'app id 2'], + it('should get matching applications checking against nameAndDOB', async () => { + prisma.applications.findMany = jest.fn().mockResolvedValue([ + { + id: 'example id 1', }, - }, - }); + { + id: 'Example id 2', + }, + ]); - expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(2, { - data: { - reviewStatus: ApplicationReviewStatusEnum.duplicate, - markedAsDuplicate: true, - }, - where: { - applicationFlaggedSet: { - some: { - id: 'example id', - }, + expect( + await service.checkForMatchesAgainstRule( + testApplicationInfo('example id 3'), + RuleEnum.nameAndDOB, + 'example listing id', + ), + ).toEqual([ + { + id: 'example id 1', }, - id: { - notIn: ['app id 1', 'app id 2'], + { + id: 'Example id 2', }, - }, - }); + ]); - expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ - where: { - id: 'example id', - }, - data: { - resolvedTime: expect.anything(), - status: FlaggedSetStatusEnum.resolved, - showConfirmationAlert: true, - userAccounts: { - connect: { - id: 'user id', - }, + expect(prisma.applications.findMany).toHaveBeenCalledWith({ + select: { + id: true, }, - }, + where: whereClauseForNameAndDOBTest( + 'example id 3', + 'example listing id', + ), + }); }); }); - it('should testApplication with no duplicates present and no existing flagged set', async () => { - prisma.applications.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); - - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); - - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + describe('Test resolve', () => { + it('should error trying to resolve flagged set on open listing', async () => { + prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ + id: 'example id', + listings: { + id: 'listing id', + status: ListingsStatusEnum.active, }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + }); + await expect( + async () => + await service.resolve( + { + afsId: 'example id', + status: FlaggedSetStatusEnum.resolved, + applications: [], + }, + { + id: 'user id', + } as unknown as User, + ), + ).rejects.toThrowError( + 'Listing listing id must be closed before resolving any duplicates', + ); + + expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ + where: { + AND: [ + { + id: 'example id', + }, + ], }, - }, - }); - - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + include: { + listings: true, + applications: { + where: { + id: { + in: [], + }, + }, }, }, - rule: RuleEnum.email, - }, + }); }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', - }, + it('should resolve flagged set to pending', async () => { + prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ + id: 'example id', + listings: { + id: 'listing id', + status: ListingsStatusEnum.closed, }, - rule: RuleEnum.nameAndDOB, - }, - }); - - expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); - - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + applications: [ + { + id: 'app id 1', + }, + { + id: 'app id 2', + }, + ], + }); + prisma.applications.updateMany = jest.fn().mockResolvedValue({ + id: 'example id', + }); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ + id: 'example id', + }); - expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); - }); + expect( + await service.resolve( + { + afsId: 'example id', + status: FlaggedSetStatusEnum.pending, + applications: [ + { + id: 'app id 1', + }, + { + id: 'app id 2', + }, + ], + }, + { + id: 'user id', + } as unknown as User, + ), + ).toEqual({ + id: 'example id', + applications: [ + { + id: 'app id 1', + }, + { + id: 'app id 2', + }, + ], + }); - it('should testApplication with no duplicates present and existing flagged set for nameAndDOB', async () => { - const mockCall = jest - .fn() - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([ - { - id: 'found afs id', - applications: [ + expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ + where: { + AND: [ { - id: 'id 1', + id: 'example id', }, { - id: 'id 2', + applications: { + some: { + id: { + in: ['app id 1', 'app id 2'], + }, + }, + }, }, ], }, - ]); - prisma.applications.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.findMany = mockCall; - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); - prisma.applications.update = jest.fn().mockResolvedValue(null); - - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); + include: { + listings: true, + applications: { + where: { + id: { + in: ['app id 1', 'app id 2'], + }, + }, + }, + }, + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(1, { + data: { + reviewStatus: ApplicationReviewStatusEnum.pendingAndValid, + markedAsDuplicate: false, }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + where: { + id: { + in: ['app id 1', 'app id 2'], + }, }, - }, - }); + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(2, { + data: { + reviewStatus: ApplicationReviewStatusEnum.pending, + markedAsDuplicate: false, + }, + where: { + applicationFlaggedSet: { + some: { + id: 'example id', + }, + }, + id: { + notIn: ['app id 1', 'app id 2'], }, }, - rule: RuleEnum.email, - }, - }); + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ + where: { + id: 'example id', + }, + data: { + resolvedTime: expect.anything(), + status: FlaggedSetStatusEnum.pending, + showConfirmationAlert: false, + userAccounts: { + connect: { + id: 'user id', + }, }, }, - rule: RuleEnum.nameAndDOB, - }, + }); }); - expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ - where: { - id: 'found afs id', - }, - }); - - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + it('should resolve flagged set to resolved', async () => { + prisma.applicationFlaggedSet.findFirst = jest.fn().mockResolvedValue({ + id: 'example id', + listings: { + id: 'listing id', + status: ListingsStatusEnum.closed, + }, + applications: [ + { + id: 'app id 1', + }, + { + id: 'app id 2', + }, + ], + }); + prisma.applications.updateMany = jest.fn().mockResolvedValue({ + id: 'example id', + }); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue({ + id: 'example id', + }); - expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); - expect(prisma.applications.update).toHaveBeenCalledWith({ - data: { - markedAsDuplicate: false, - reviewStatus: ApplicationReviewStatusEnum.valid, - }, - where: { - id: 'application id', - }, - }); - }); + expect( + await service.resolve( + { + afsId: 'example id', + status: FlaggedSetStatusEnum.resolved, + applications: [ + { + id: 'app id 1', + }, + { + id: 'app id 2', + }, + ], + }, + { + id: 'user id', + } as unknown as User, + ), + ).toEqual({ + id: 'example id', + applications: [ + { + id: 'app id 1', + }, + { + id: 'app id 2', + }, + ], + }); - it('should testApplication with no duplicates present and existing flagged set for email', async () => { - const mockCall = jest - .fn() - .mockResolvedValueOnce([ - { - id: 'found afs id', - applications: [ + expect(prisma.applicationFlaggedSet.findFirst).toHaveBeenCalledWith({ + where: { + AND: [ { - id: 'id 1', + id: 'example id', }, { - id: 'id 2', + applications: { + some: { + id: { + in: ['app id 1', 'app id 2'], + }, + }, + }, }, ], }, - ]) - .mockResolvedValueOnce([]); - prisma.applications.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.findMany = mockCall; - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); - prisma.applications.update = jest.fn().mockResolvedValue(null); - - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); + include: { + listings: true, + applications: { + where: { + id: { + in: ['app id 1', 'app id 2'], + }, + }, + }, + }, + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(1, { + data: { + reviewStatus: ApplicationReviewStatusEnum.valid, + markedAsDuplicate: false, }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + where: { + id: { + in: ['app id 1', 'app id 2'], + }, }, - }, - }); + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); + expect(prisma.applications.updateMany).toHaveBeenNthCalledWith(2, { + data: { + reviewStatus: ApplicationReviewStatusEnum.duplicate, + markedAsDuplicate: true, + }, + where: { + applicationFlaggedSet: { + some: { + id: 'example id', + }, + }, + id: { + notIn: ['app id 1', 'app id 2'], + }, + }, + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.update).toHaveBeenCalledWith({ + where: { + id: 'example id', + }, + data: { + resolvedTime: expect.anything(), + status: FlaggedSetStatusEnum.resolved, + showConfirmationAlert: true, + userAccounts: { + connect: { + id: 'user id', + }, }, }, - rule: RuleEnum.email, - }, + }); }); + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + describe('Test testApplication', () => { + it('should testApplication with no duplicates present and no existing flagged set', async () => { + prisma.applications.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', }, }, - rule: RuleEnum.nameAndDOB, - }, - }); + }); - expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ - where: { - id: 'found afs id', - }, - }); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledTimes(1); + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.email, + }, + }); - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.nameAndDOB, + }, + }); - expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); + expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); - expect(prisma.applications.update).toHaveBeenCalledWith({ - data: { - markedAsDuplicate: false, - reviewStatus: ApplicationReviewStatusEnum.valid, - }, - where: { - id: 'application id', - }, - }); - }); + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - it('should testApplication with duplicates present for email and no existing flagged set', async () => { - const mockCall = jest.fn().mockResolvedValueOnce([ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', - }, - ]); - prisma.applications.findMany = mockCall; - prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ - id: 'new afs id', + expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); }); - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); - - expect(prisma.applications.findMany).toHaveBeenCalledTimes(2); - - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', - }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', - }, - }, - }); - - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledTimes(3); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + it('should testApplication with no duplicates present and existing flagged set for nameAndDOB', async () => { + const mockCall = jest + .fn() + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([ + { + id: 'found afs id', + applications: [ + { + id: 'id 1', + }, + { + id: 'id 2', + }, + ], + }, + ]); + prisma.applications.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.findMany = mockCall; + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); + prisma.applications.update = jest.fn().mockResolvedValue(null); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', }, }, - rule: RuleEnum.email, - }, - }); + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - where: { - listingId: 'listing id', - ruleKey: 'listing id-email-example email', - }, - }); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(3, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, }, + rule: RuleEnum.email, }, - rule: RuleEnum.nameAndDOB, - }, - }); + }); - expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); - - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - - expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ - data: { - rule: RuleEnum.email, - ruleKey: 'listing id-email-example email', - resolvedTime: null, - status: FlaggedSetStatusEnum.pending, - listings: { - connect: { - id: 'listing id', - }, + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, }, - applications: { - connect: [ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', - }, - { + where: { + listingId: 'listing id', + applications: { + some: { id: 'application id', }, - ], + }, + rule: RuleEnum.nameAndDOB, }, - }, - }); - }); + }); - it('should testApplication with duplicates present for email and existing flagged set is correct', async () => { - const mockCall = jest.fn().mockResolvedValueOnce([ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', - }, - ]); - prisma.applications.findMany = mockCall; - const mockFindManyCall = jest - .fn() - .mockResolvedValueOnce([ - { + expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ + where: { id: 'found afs id', - ruleKey: 'listing id-email-example email', }, - ]) - .mockResolvedValueOnce([]); - prisma.applicationFlaggedSet.findMany = mockFindManyCall; - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); - - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); + }); - expect(prisma.applications.findMany).toHaveBeenCalledTimes(2); + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); + expect(prisma.applications.update).toHaveBeenCalledWith({ + data: { + markedAsDuplicate: false, + reviewStatus: ApplicationReviewStatusEnum.valid, }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + where: { + id: 'application id', }, - }, + }); }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); + it('should testApplication with no duplicates present and existing flagged set for email', async () => { + const mockCall = jest + .fn() + .mockResolvedValueOnce([ + { + id: 'found afs id', + applications: [ + { + id: 'id 1', + }, + { + id: 'id 2', + }, + ], + }, + ]) + .mockResolvedValueOnce([]); + prisma.applications.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.findMany = mockCall; + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); + prisma.applications.update = jest.fn().mockResolvedValue(null); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', + }, + }, + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledTimes(2); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, }, + rule: RuleEnum.email, }, - rule: RuleEnum.email, - }, - }); + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, }, + rule: RuleEnum.nameAndDOB, }, - rule: RuleEnum.nameAndDOB, - }, - }); + }); - expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); + expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ + where: { + id: 'found afs id', + }, + }); - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledTimes(1); - expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); - }); + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - it('should testApplication with duplicates present for email and existing flagged set is incorrect', async () => { - const mockCall = jest.fn().mockResolvedValueOnce([ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', - }, - ]); + expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); - const mockFindMany = jest - .fn() - .mockResolvedValueOnce([ - { - id: 'found afs id', - ruleKey: 'a different rule key', - applications: [ - { - id: 'id 1', - }, - { - id: 'id 2', - }, - ], + expect(prisma.applications.update).toHaveBeenCalledWith({ + data: { + markedAsDuplicate: false, + reviewStatus: ApplicationReviewStatusEnum.valid, }, - ]) - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([]); - prisma.applications.findMany = mockCall; - prisma.applicationFlaggedSet.findMany = mockFindMany; - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ - id: 'new afs id', + where: { + id: 'application id', + }, + }); }); - prisma.applications.update = jest.fn().mockResolvedValue(null); - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); + it('should testApplication with duplicates present for email and no existing flagged set', async () => { + const mockCall = jest.fn().mockResolvedValueOnce([ + { + id: 'dup id 1', + }, + { + id: 'dup id 2', + }, + ]); + prisma.applications.findMany = mockCall; + prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ + id: 'new afs id', + }); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenCalledTimes(2); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', + }, + }, + }); - expect(prisma.applications.findMany).toHaveBeenCalledTimes(2); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledTimes(3); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.email, }, - }, - }); + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + where: { + listingId: 'listing id', + ruleKey: 'listing id-email-example email', + }, + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(3, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, }, + rule: RuleEnum.nameAndDOB, }, - rule: RuleEnum.email, - }, - }); + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ - where: { - listingId: 'listing id', - ruleKey: 'listing id-email-example email', - }, - }); + expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledTimes(3); + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ - where: { - id: 'found afs id', - }, + expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ + data: { + rule: RuleEnum.email, + ruleKey: 'listing id-email-example email', + resolvedTime: null, + status: FlaggedSetStatusEnum.pending, + listings: { + connect: { + id: 'listing id', + }, + }, + applications: { + connect: [ + { + id: 'dup id 1', + }, + { + id: 'dup id 2', + }, + { + id: 'application id', + }, + ], + }, + }, + }); }); - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - - expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ - data: { - rule: RuleEnum.email, - ruleKey: 'listing id-email-example email', - resolvedTime: null, - status: FlaggedSetStatusEnum.pending, - listings: { - connect: { - id: 'listing id', + it('should testApplication with duplicates present for email and existing flagged set is correct', async () => { + const mockCall = jest.fn().mockResolvedValueOnce([ + { + id: 'dup id 1', + }, + { + id: 'dup id 2', + }, + ]); + prisma.applications.findMany = mockCall; + const mockFindManyCall = jest + .fn() + .mockResolvedValueOnce([ + { + id: 'found afs id', + ruleKey: 'listing id-email-example email', + }, + ]) + .mockResolvedValueOnce([]); + prisma.applicationFlaggedSet.findMany = mockFindManyCall; + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenCalledTimes(2); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', }, }, - applications: { - connect: [ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', + }); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledTimes(2); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', }, - { + }, + rule: RuleEnum.email, + }, + }); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { id: 'application id', }, - ], + }, + rule: RuleEnum.nameAndDOB, }, - }, - }); + }); - expect(prisma.applications.update).toHaveBeenCalledWith({ - data: { - markedAsDuplicate: false, - reviewStatus: ApplicationReviewStatusEnum.valid, - }, - where: { - id: 'application id', - }, + expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); + + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + + expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); }); - }); - it('should testApplication with duplicates present for nameAndDOB and no existing flagged set', async () => { - const mockCall = jest - .fn() - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([ + it('should testApplication with duplicates present for email and existing flagged set is incorrect', async () => { + const mockCall = jest.fn().mockResolvedValueOnce([ { id: 'dup id 1', }, @@ -1971,496 +1898,639 @@ describe('Testing application flagged set service', () => { }, ]); - prisma.applications.findMany = mockCall; - prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ - id: 'new afs id', - }); + const mockFindMany = jest + .fn() + .mockResolvedValueOnce([ + { + id: 'found afs id', + ruleKey: 'a different rule key', + applications: [ + { + id: 'id 1', + }, + { + id: 'id 2', + }, + ], + }, + ]) + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([]); + prisma.applications.findMany = mockCall; + prisma.applicationFlaggedSet.findMany = mockFindMany; + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ + id: 'new afs id', + }); + prisma.applications.update = jest.fn().mockResolvedValue(null); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenCalledTimes(2); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', + }, + }, + }); - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ + include: { + applications: true, }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.email, }, - }, + }); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledWith({ + where: { + listingId: 'listing id', + ruleKey: 'listing id-email-example email', + }, + }); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenCalledTimes(3); + + expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ + where: { + id: 'found afs id', + }, + }); + + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + + expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ + data: { + rule: RuleEnum.email, + ruleKey: 'listing id-email-example email', + resolvedTime: null, + status: FlaggedSetStatusEnum.pending, + listings: { + connect: { + id: 'listing id', + }, + }, + applications: { + connect: [ + { + id: 'dup id 1', + }, + { + id: 'dup id 2', + }, + { + id: 'application id', + }, + ], + }, + }, + }); + + expect(prisma.applications.update).toHaveBeenCalledWith({ + data: { + markedAsDuplicate: false, + reviewStatus: ApplicationReviewStatusEnum.valid, + }, + where: { + id: 'application id', + }, + }); }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); + it('should testApplication with duplicates present for nameAndDOB and no existing flagged set', async () => { + const mockCall = jest + .fn() + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([ + { + id: 'dup id 1', + }, + { + id: 'dup id 2', + }, + ]); + + prisma.applications.findMany = mockCall; + prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ + id: 'new afs id', + }); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', + }, + }, + }); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.email, + }, + }); + + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.nameAndDOB, + }, + }); + + expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + + expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ + data: { + rule: RuleEnum.nameAndDOB, + ruleKey: + 'listing id-nameAndDOB-example first name-example last name-9-10-2000', + resolvedTime: null, + status: FlaggedSetStatusEnum.pending, + listings: { + connect: { + id: 'listing id', + }, + }, + applications: { + connect: [ + { + id: 'dup id 1', + }, + { + id: 'dup id 2', + }, + { + id: 'application id', + }, + ], }, }, - rule: RuleEnum.email, - }, + }); }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + it('should testApplication with duplicates present for nameAndDOB and existing flagged set is correct', async () => { + const mockCall = jest + .fn() + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([ + { + id: 'dup id 1', }, - }, - rule: RuleEnum.nameAndDOB, - }, - }); + { + id: 'dup id 2', + }, + ]); - expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); + const mockFlaggedSetCall = jest + .fn() + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([ + { + id: 'found afs id', + ruleKey: + 'listing id-nameAndDOB-example first name-example last name-9-10-2000', + }, + ]); + + prisma.applications.findMany = mockCall; + prisma.applicationFlaggedSet.findMany = mockFlaggedSetCall; + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ + id: 'new afs id', + }); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', + }, + }, + }); - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ - data: { - rule: RuleEnum.nameAndDOB, - ruleKey: - 'listing id-nameAndDOB-example first name-example last name-9-10-2000', - resolvedTime: null, - status: FlaggedSetStatusEnum.pending, - listings: { - connect: { - id: 'listing id', - }, + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, }, - applications: { - connect: [ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', - }, - { + where: { + listingId: 'listing id', + applications: { + some: { id: 'application id', }, - ], + }, + rule: RuleEnum.email, }, - }, - }); - }); + }); - it('should testApplication with duplicates present for nameAndDOB and existing flagged set is correct', async () => { - const mockCall = jest - .fn() - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, }, - ]); - - const mockFlaggedSetCall = jest - .fn() - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([ - { - id: 'found afs id', - ruleKey: - 'listing id-nameAndDOB-example first name-example last name-9-10-2000', + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.nameAndDOB, }, - ]); - - prisma.applications.findMany = mockCall; - prisma.applicationFlaggedSet.findMany = mockFlaggedSetCall; - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ - id: 'new afs id', - }); + }); - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); + expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', - }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', - }, - }, - }); + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), + expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + it('should testApplication with duplicates present for nameAndDOB and existing flagged set is incorrect', async () => { + const mockCall = jest + .fn() + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([ + { + id: 'dup id 1', }, - }, - rule: RuleEnum.email, - }, - }); + { + id: 'dup id 2', + }, + ]); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + const mockFlaggedSetCall = jest + .fn() + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([ + { + id: 'found afs id', + ruleKey: 'a different ruleKey', + applications: [ + { + id: '1', + }, + { + id: '2', + }, + ], + }, + ]) + .mockResolvedValueOnce([]); + + prisma.applications.findMany = mockCall; + prisma.applicationFlaggedSet.findMany = mockFlaggedSetCall; + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ + id: 'new afs id', + }); + prisma.applications.update = jest.fn().mockResolvedValue(null); + + await service.testApplication( + testApplicationInfo('application id'), + 'listing id', + ); + + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + select: { + id: true, + }, + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', }, }, - rule: RuleEnum.nameAndDOB, - }, - }); - - expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); + }); - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - - expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); - }); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - it('should testApplication with duplicates present for nameAndDOB and existing flagged set is incorrect', async () => { - const mockCall = jest - .fn() - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([ - { - id: 'dup id 1', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, }, - { - id: 'dup id 2', + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, + }, + rule: RuleEnum.email, }, - ]); + }); - const mockFlaggedSetCall = jest - .fn() - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([ - { - id: 'found afs id', - ruleKey: 'a different ruleKey', - applications: [ - { - id: '1', - }, - { - id: '2', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', }, - ], + }, + rule: RuleEnum.nameAndDOB, }, - ]) - .mockResolvedValueOnce([]); - - prisma.applications.findMany = mockCall; - prisma.applicationFlaggedSet.findMany = mockFlaggedSetCall; - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue({ - id: 'new afs id', - }); - prisma.applications.update = jest.fn().mockResolvedValue(null); + }); - await service.testApplication( - testApplicationInfo('application id'), - 'listing id', - ); - - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(3, { + where: { + listingId: 'listing id', + ruleKey: + 'listing id-nameAndDOB-example first name-example last name-9-10-2000', }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + }); + + expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ + where: { + id: 'found afs id', }, - }, - }); + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ + data: { + rule: RuleEnum.nameAndDOB, + ruleKey: + 'listing id-nameAndDOB-example first name-example last name-9-10-2000', + resolvedTime: null, + status: FlaggedSetStatusEnum.pending, + listings: { + connect: { + id: 'listing id', + }, }, - }, - rule: RuleEnum.email, - }, - }); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + applications: { + connect: [ + { + id: 'dup id 1', + }, + { + id: 'dup id 2', + }, + { + id: 'application id', + }, + ], }, }, - rule: RuleEnum.nameAndDOB, - }, - }); - - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(3, { - where: { - listingId: 'listing id', - ruleKey: - 'listing id-nameAndDOB-example first name-example last name-9-10-2000', - }, - }); + }); - expect(prisma.applicationFlaggedSet.delete).toHaveBeenCalledWith({ - where: { - id: 'found afs id', - }, - }); - - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - - expect(prisma.applicationFlaggedSet.create).toHaveBeenCalledWith({ - data: { - rule: RuleEnum.nameAndDOB, - ruleKey: - 'listing id-nameAndDOB-example first name-example last name-9-10-2000', - resolvedTime: null, - status: FlaggedSetStatusEnum.pending, - listings: { - connect: { - id: 'listing id', - }, + expect(prisma.applications.update).toHaveBeenCalledWith({ + data: { + markedAsDuplicate: false, + reviewStatus: ApplicationReviewStatusEnum.valid, }, - applications: { - connect: [ - { - id: 'dup id 1', - }, - { - id: 'dup id 2', - }, - { - id: 'application id', - }, - ], + where: { + id: 'application id', }, - }, - }); - - expect(prisma.applications.update).toHaveBeenCalledWith({ - data: { - markedAsDuplicate: false, - reviewStatus: ApplicationReviewStatusEnum.valid, - }, - where: { - id: 'application id', - }, + }); }); }); - it('should process listing', async () => { - const mockCall = jest - .fn() - .mockResolvedValueOnce([ + describe('Test process', () => { + it('should process listing', async () => { + const mockCall = jest + .fn() + .mockResolvedValueOnce([ + { + ...testApplicationInfo('application id'), + listingId: 'listing id', + }, + ]) + .mockResolvedValueOnce([]) + .mockResolvedValueOnce([]); + + prisma.applications.findMany = mockCall; + prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); + prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); + prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); + prisma.cronJob.findFirst = jest.fn().mockResolvedValue({ + id: 'example id', + }); + prisma.cronJob.update = jest.fn().mockResolvedValue({ + id: 'example id', + }); + prisma.listings.findMany = jest.fn().mockResolvedValue([ { - ...testApplicationInfo('application id'), - listingId: 'listing id', + id: 'listing id', + afsLastRunAt: new Date(), }, - ]) - .mockResolvedValueOnce([]) - .mockResolvedValueOnce([]); - - prisma.applications.findMany = mockCall; - prisma.applicationFlaggedSet.findMany = jest.fn().mockResolvedValue([]); - prisma.applicationFlaggedSet.delete = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.update = jest.fn().mockResolvedValue(null); - prisma.applicationFlaggedSet.create = jest.fn().mockResolvedValue(null); - prisma.cronJob.findFirst = jest.fn().mockResolvedValue({ - id: 'example id', - }); - prisma.cronJob.update = jest.fn().mockResolvedValue({ - id: 'example id', - }); - prisma.listings.findMany = jest.fn().mockResolvedValue([ - { - id: 'listing id', - afsLastRunAt: new Date(), - }, - ]); - prisma.listings.update = jest.fn().mockResolvedValue(null); + ]); + prisma.listings.update = jest.fn().mockResolvedValue(null); - await service.process(); + await service.process(); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { - where: { - listingId: 'listing id', - updatedAt: { - gte: expect.anything(), + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(1, { + where: { + listingId: 'listing id', + updatedAt: { + gte: expect.anything(), + }, }, - }, - include: { - applicant: true, - householdMember: true, - }, - }); + include: { + applicant: true, + householdMember: true, + }, + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { - select: { - id: true, - }, - where: { - id: { - not: 'application id', + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(2, { + select: { + id: true, }, - status: ApplicationStatusEnum.submitted, - listingId: 'listing id', - applicant: { - emailAddress: 'example email', + where: { + id: { + not: 'application id', + }, + status: ApplicationStatusEnum.submitted, + listingId: 'listing id', + applicant: { + emailAddress: 'example email', + }, }, - }, - }); + }); - expect(prisma.applications.findMany).toHaveBeenNthCalledWith(3, { - select: { - id: true, - }, - where: whereClauseForNameAndDOBTest('application id', 'listing id'), - }); + expect(prisma.applications.findMany).toHaveBeenNthCalledWith(3, { + select: { + id: true, + }, + where: whereClauseForNameAndDOBTest('application id', 'listing id'), + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(1, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, }, + rule: RuleEnum.email, }, - rule: RuleEnum.email, - }, - }); + }); - expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { - include: { - applications: true, - }, - where: { - listingId: 'listing id', - applications: { - some: { - id: 'application id', + expect(prisma.applicationFlaggedSet.findMany).toHaveBeenNthCalledWith(2, { + include: { + applications: true, + }, + where: { + listingId: 'listing id', + applications: { + some: { + id: 'application id', + }, }, + rule: RuleEnum.nameAndDOB, }, - rule: RuleEnum.nameAndDOB, - }, - }); + }); - expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); + expect(prisma.applicationFlaggedSet.delete).not.toHaveBeenCalled(); - expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); + expect(prisma.applicationFlaggedSet.update).not.toHaveBeenCalled(); - expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); + expect(prisma.applicationFlaggedSet.create).not.toHaveBeenCalled(); - expect(prisma.listings.findMany).toHaveBeenCalledWith({ - select: { - id: true, - afsLastRunAt: true, - }, - where: { - lastApplicationUpdateAt: { - not: null, + expect(prisma.listings.findMany).toHaveBeenCalledWith({ + select: { + id: true, + afsLastRunAt: true, }, - AND: [ - { - OR: [ - { - afsLastRunAt: { - equals: null, + where: { + lastApplicationUpdateAt: { + not: null, + }, + AND: [ + { + OR: [ + { + afsLastRunAt: { + equals: null, + }, }, - }, - { - afsLastRunAt: { - lte: prisma.listings.fields.lastApplicationUpdateAt, + { + afsLastRunAt: { + lte: prisma.listings.fields.lastApplicationUpdateAt, + }, }, - }, - ], - }, - ], - }, - }); + ], + }, + ], + }, + }); - expect(prisma.listings.update).toHaveBeenCalledWith({ - where: { - id: 'listing id', - }, - data: { - afsLastRunAt: expect.anything(), - }, + expect(prisma.listings.update).toHaveBeenCalledWith({ + where: { + id: 'listing id', + }, + data: { + afsLastRunAt: expect.anything(), + }, + }); }); }); });