Skip to content

Commit

Permalink
fix: dont send lottery emails to duplicate applications (#4346)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyjablonski authored Sep 23, 2024
1 parent a62f73b commit cba63b1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 75 deletions.
47 changes: 0 additions & 47 deletions api/src/services/listing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,53 +278,6 @@ export class ListingService implements OnModuleInit {
return { emails: userEmails, publicUrl };
}

public async getPublicUserEmailInfo(
listingId?: string,
): Promise<{ [key: string]: string[] }> {
const userResults = await this.prisma.applications.findMany({
select: {
language: true,
applicant: {
select: {
emailAddress: true,
},
},
},
where: {
listingId,
applicant: {
emailAddress: {
not: null,
},
},
},
});

const result = {};
Object.keys(LanguagesEnum).forEach((languageKey) => {
const applications = userResults
.filter((user) => user.language === languageKey)
.map((userObj) => userObj.applicant.emailAddress);
if (applications.length) {
result[languageKey] = applications;
}
});

const noLanguageIndicated = userResults
.filter((user) => !user.language)
.map((userObj) => userObj.applicant.emailAddress);

if (!result[LanguagesEnum.en])
result[LanguagesEnum.en] = noLanguageIndicated;
else
result[LanguagesEnum.en] = [
...result[LanguagesEnum.en],
...noLanguageIndicated,
];

return result;
}

public async listingApprovalNotify(params: {
user: User;
listingInfo: IdDTO;
Expand Down
52 changes: 49 additions & 3 deletions api/src/services/lottery.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { SchedulerRegistry } from '@nestjs/schedule';
import { ConfigService } from '@nestjs/config';
import {
ApplicationLotteryTotal,
LanguagesEnum,
ListingEventsTypeEnum,
ListingsStatusEnum,
LotteryStatusEnum,
Expand Down Expand Up @@ -343,6 +343,53 @@ export class LotteryService {
};
}

public async getPublicUserEmailInfo(
listingId?: string,
): Promise<{ [key: string]: string[] }> {
const userResults = await this.prisma.applications.findMany({
select: {
userAccounts: {
select: {
email: true,
},
},
language: true,
},
where: {
listingId,
markedAsDuplicate: {
not: true,
},
},
});

const emailUsers = userResults.filter((user) => !!user.userAccounts?.email);

const result = {};
Object.keys(LanguagesEnum).forEach((languageKey) => {
const applications = emailUsers
.filter((user) => user.language === languageKey)
.map((userObj) => userObj.userAccounts.email);
if (applications.length) {
result[languageKey] = applications;
}
});

const noLanguageIndicated = emailUsers
.filter((user) => !user.language)
.map((userObj) => userObj.userAccounts.email);

if (!result[LanguagesEnum.en])
result[LanguagesEnum.en] = noLanguageIndicated;
else
result[LanguagesEnum.en] = [
...result[LanguagesEnum.en],
...noLanguageIndicated,
];

return result;
}

async publishLottery(listing: Listing): Promise<SuccessDTO> {
const partnerUserEmailInfo = await this.listingService.getUserEmailInfo(
[
Expand All @@ -354,8 +401,7 @@ export class LotteryService {
listing.jurisdictions?.id,
);

const publicUserEmailInfo =
await this.listingService.getPublicUserEmailInfo(listing.id);
const publicUserEmailInfo = await this.getPublicUserEmailInfo(listing.id);

await this.updateLotteryStatus(
listing.id,
Expand Down
31 changes: 26 additions & 5 deletions api/test/integration/lottery.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,9 +1032,12 @@ describe('Lottery Controller Tests', () => {
confirmationCode: 'ABCD1234',
submissionType: ApplicationSubmissionTypeEnum.electronical,
language: LanguagesEnum.en,
applicant: {
userAccounts: {
create: {
emailAddress: '[email protected]',
email: '[email protected]',
firstName: 'first',
lastName: 'last',
passwordHash: 'abcd1234',
},
},
},
Expand All @@ -1044,9 +1047,12 @@ describe('Lottery Controller Tests', () => {
confirmationCode: 'EFGH5678',
submissionType: ApplicationSubmissionTypeEnum.electronical,
language: LanguagesEnum.es,
applicant: {
userAccounts: {
create: {
emailAddress: '[email protected]',
email: '[email protected]',
firstName: 'first',
lastName: 'last',
passwordHash: 'abcd1234',
},
},
},
Expand All @@ -1056,9 +1062,24 @@ describe('Lottery Controller Tests', () => {
confirmationCode: 'IJKL9012',
submissionType: ApplicationSubmissionTypeEnum.electronical,
language: null,
userAccounts: {
create: {
email: '[email protected]',
firstName: 'first',
lastName: 'last',
passwordHash: 'abcd1234',
},
},
},
{
preferences: [],
status: ApplicationStatusEnum.submitted,
confirmationCode: 'MNOP3456',
submissionType: ApplicationSubmissionTypeEnum.electronical,
language: null,
applicant: {
create: {
emailAddress: 'applicant3@email.com',
emailAddress: 'applicant4@email.com',
},
},
},
Expand Down
32 changes: 12 additions & 20 deletions api/test/unit/services/lottery.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,9 @@ describe('Testing lottery service', () => {
emails: ['[email protected]', '[email protected]'],
});

jest
.spyOn(listingService, 'getPublicUserEmailInfo')
.mockResolvedValueOnce({
en: ['[email protected]'],
});
jest.spyOn(service, 'getPublicUserEmailInfo').mockResolvedValueOnce({
en: ['[email protected]'],
});

await service.lotteryStatus(
{
Expand Down Expand Up @@ -671,11 +669,9 @@ describe('Testing lottery service', () => {
jest.spyOn(listingService, 'getUserEmailInfo').mockResolvedValueOnce({
emails: ['[email protected]', '[email protected]'],
});
jest
.spyOn(listingService, 'getPublicUserEmailInfo')
.mockResolvedValueOnce({
en: ['[email protected]'],
});
jest.spyOn(service, 'getPublicUserEmailInfo').mockResolvedValueOnce({
en: ['[email protected]'],
});

await expect(
async () =>
Expand Down Expand Up @@ -716,11 +712,9 @@ describe('Testing lottery service', () => {
jest.spyOn(listingService, 'getUserEmailInfo').mockResolvedValueOnce({
emails: ['[email protected]', '[email protected]'],
});
jest
.spyOn(listingService, 'getPublicUserEmailInfo')
.mockResolvedValueOnce({
en: ['[email protected]'],
});
jest.spyOn(service, 'getPublicUserEmailInfo').mockResolvedValueOnce({
en: ['[email protected]'],
});

await service.lotteryStatus(
{
Expand Down Expand Up @@ -828,11 +822,9 @@ describe('Testing lottery service', () => {
lotteryStatus: LotteryStatusEnum.ran,
});
prisma.jurisdictions.findFirst = jest.fn().mockResolvedValue(null);
jest
.spyOn(listingService, 'getPublicUserEmailInfo')
.mockResolvedValueOnce({
en: ['[email protected]'],
});
jest.spyOn(service, 'getPublicUserEmailInfo').mockResolvedValueOnce({
en: ['[email protected]'],
});

jest.spyOn(listingService, 'getUserEmailInfo').mockResolvedValueOnce({
emails: ['[email protected]', '[email protected]'],
Expand Down

0 comments on commit cba63b1

Please sign in to comment.