From 4c5dee24a5790c3c6e45dd8987273eed16d615a7 Mon Sep 17 00:00:00 2001 From: Krum Dachev <149360576+kdachev97@users.noreply.github.com> Date: Tue, 19 Mar 2024 07:12:01 +0200 Subject: [PATCH] add helpUsImprove field to the backend (#615) * add helpUsImprove field to the backend * add migration for the new helpUsImprove field * reverted previous migration and added new one; modified personMock.ts * add helpUsImprove in auth.service.ts * add changes to tests on backend --- .../affiliate/affiliate.controller.spec.ts | 31 ++------ apps/api/src/auth/auth.service.spec.ts | 77 ++++--------------- apps/api/src/auth/auth.service.ts | 1 + apps/api/src/auth/dto/register.dto.ts | 6 ++ .../src/campaign/campaign.controller.spec.ts | 2 +- .../generated/person/dto/create-person.dto.ts | 1 + .../generated/person/dto/update-person.dto.ts | 1 + .../person/entities/person.entity.ts | 1 + .../src/donations/__mocks__/paymentMock.ts | 2 +- .../__mock__/{peronMock.ts => personMock.ts} | 3 +- .../migration.sql | 2 + podkrepi.dbml | 1 + schema.prisma | 1 + 13 files changed, 42 insertions(+), 87 deletions(-) rename apps/api/src/person/__mock__/{peronMock.ts => personMock.ts} (91%) create mode 100644 migrations/20240318124702_help_us_improve/migration.sql diff --git a/apps/api/src/affiliate/affiliate.controller.spec.ts b/apps/api/src/affiliate/affiliate.controller.spec.ts index 55316dd9e..4bbd4eb0f 100644 --- a/apps/api/src/affiliate/affiliate.controller.spec.ts +++ b/apps/api/src/affiliate/affiliate.controller.spec.ts @@ -17,9 +17,10 @@ import { Campaign, CampaignState, PaymentStatus, - Payments, + Payment, Prisma, Vault, + Person, } from '@prisma/client' import { KeycloakTokenParsed } from '../auth/keycloak' import { BadRequestException, ConflictException, ForbiddenException } from '@nestjs/common' @@ -27,6 +28,7 @@ import { AffiliateStatusUpdateDto } from './dto/affiliate-status-update.dto' import * as afCodeGenerator from './utils/affiliateCodeGenerator' import { CreateAffiliateDonationDto } from './dto/create-affiliate-donation.dto' import { mockPayment } from '../donations/__mocks__/paymentMock' +import { personMock } from '../person/__mock__/personMock' type PersonWithPayload = Prisma.PersonGetPayload<{ include: { company: true } }> type AffiliateWithPayload = Prisma.AffiliateGetPayload<{ @@ -80,26 +82,7 @@ describe('AffiliateController', () => { newStatus: 'active', } - const mockIndividualProfile: PersonWithPayload = { - id: 'e43348aa-be33-4c12-80bf-2adfbf8736cd', - firstName: 'John', - lastName: 'Doe', - companyId: null, - keycloakId: '123', - email: 'test@podkrepi.bg', - emailConfirmed: false, - phone: null, - picture: null, - createdAt: new Date('2021-10-07T13:38:11.097Z'), - updatedAt: new Date('2021-10-07T13:38:11.097Z'), - newsletter: false, - address: null, - birthday: null, - personalNumber: null, - stripeCustomerId: null, - company: null, - profileEnabled: true, - } + const mockIndividualProfile: Person = personMock const vaultMock: Vault = { id: 'vault-id', @@ -286,7 +269,7 @@ describe('AffiliateController', () => { extCustomerId: '', extPaymentIntentId: '123456', extPaymentMethodId: '1234', - billingEmail: 'test@podkrepi.bg', + billingEmail: personMock.email, currency: 'BGN', toEntity: new CreateAffiliateDonationDto().toEntity, metadata: { @@ -314,7 +297,7 @@ describe('AffiliateController', () => { ) }) it('should cancel', async () => { - const cancelledDonationResponse: Payments = { + const cancelledDonationResponse: Payment = { ...mockGuaranteedPayment, status: PaymentStatus.cancelled, } @@ -327,7 +310,7 @@ describe('AffiliateController', () => { ).toEqual(cancelledDonationResponse) }) it('should throw error if donation status is succeeded', async () => { - const succeededDonationResponse: Payments = { + const succeededDonationResponse: Payment = { ...mockGuaranteedPayment, status: PaymentStatus.succeeded, } diff --git a/apps/api/src/auth/auth.service.spec.ts b/apps/api/src/auth/auth.service.spec.ts index 48c61e8be..b7ad7cb58 100644 --- a/apps/api/src/auth/auth.service.spec.ts +++ b/apps/api/src/auth/auth.service.spec.ts @@ -24,6 +24,7 @@ import { SendGridNotificationsProvider } from '../notifications/providers/notifi import { NotificationsProviderInterface } from '../notifications/providers/notifications.interface.providers' import { MarketingNotificationsModule } from '../notifications/notifications.module' import { PersonService } from '../person/person.service' +import { personMock } from '../person/__mock__/personMock' jest.mock('@keycloak/keycloak-admin-client') @@ -32,28 +33,10 @@ describe('AuthService', () => { let config: ConfigService let admin: KeycloakAdminClient let keycloak: KeycloakConnect.Keycloak - let marketing: NotificationsProviderInterface + let marketing: NotificationsProviderInterface let personService: PersonService - const person: Person = { - id: 'e43348aa-be33-4c12-80bf-2adfbf8736cd', - firstName: 'Admin', - lastName: 'Dev', - companyId: null, - keycloakId: '123', - email: 'test@podkrepi.bg', - emailConfirmed: false, - phone: null, - picture: null, - createdAt: new Date('2021-10-07T13:38:11.097Z'), - updatedAt: new Date('2021-10-07T13:38:11.097Z'), - newsletter: false, - address: null, - birthday: null, - personalNumber: null, - stripeCustomerId: null, - profileEnabled: false, - } + const person: Person = personMock beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -331,7 +314,8 @@ describe('AuthService', () => { //if no company has been created company.id is expected to be undefined const companyId = undefined const profileEnabled = true - const newsletter = true + const newsletter = personMock.newsletter + const helpUsImprove = personMock.helpUsImprove it('should call keycloak and prisma', async () => { const keycloakId = 'u123' @@ -381,7 +365,16 @@ describe('AuthService', () => { // Check db creation expect(prismaSpy).toHaveBeenCalledWith({ - create: { keycloakId, email, firstName, lastName, newsletter, companyId, profileEnabled }, + create: { + keycloakId, + email, + firstName, + lastName, + newsletter, + companyId, + profileEnabled, + helpUsImprove, + }, update: { keycloakId }, where: { email }, }) @@ -423,25 +416,7 @@ describe('AuthService', () => { // Add to marketing list newsletter: true, }) - const person: Person = { - id: 'e43348aa-be33-4c12-80bf-2adfbf8736cd', - firstName, - lastName, - companyId: null, - keycloakId, - email, - emailConfirmed: false, - phone: null, - picture: null, - createdAt: new Date('2021-10-07T13:38:11.097Z'), - updatedAt: new Date('2021-10-07T13:38:11.097Z'), - newsletter: true, - address: null, - birthday: null, - personalNumber: null, - stripeCustomerId: null, - profileEnabled: true, - } + jest.spyOn(prismaMock.person, 'upsert').mockResolvedValue(person) jest.spyOn(admin.users, 'create').mockResolvedValue({ id: keycloakId }) const marketingSpy = jest @@ -473,25 +448,7 @@ describe('AuthService', () => { // Don't subscribe to marketing list newsletter: false, }) - const person: Person = { - id: 'e43348aa-be33-4c12-80bf-2adfbf8736cd', - firstName, - lastName, - companyId: null, - keycloakId, - email, - emailConfirmed: false, - phone: null, - picture: null, - createdAt: new Date('2021-10-07T13:38:11.097Z'), - updatedAt: new Date('2021-10-07T13:38:11.097Z'), - newsletter: false, - address: null, - birthday: null, - personalNumber: null, - stripeCustomerId: null, - profileEnabled: true, - } + jest.spyOn(prismaMock.person, 'upsert').mockResolvedValue(person) jest.spyOn(admin.users, 'create').mockResolvedValue({ id: keycloakId }) const marketingSpy = jest diff --git a/apps/api/src/auth/auth.service.ts b/apps/api/src/auth/auth.service.ts index 90e53760e..1e605d075 100644 --- a/apps/api/src/auth/auth.service.ts +++ b/apps/api/src/auth/auth.service.ts @@ -293,6 +293,7 @@ export class AuthService { newsletter: registerDto.newsletter ? registerDto.newsletter : false, companyId: companyId, profileEnabled: companyId ? false : true, + helpUsImprove: registerDto.helpUsImprove ? registerDto.helpUsImprove : false, }, // Store keycloakId to the person with same email update: { keycloakId }, diff --git a/apps/api/src/auth/dto/register.dto.ts b/apps/api/src/auth/dto/register.dto.ts index fee8399b9..bff99ec5f 100644 --- a/apps/api/src/auth/dto/register.dto.ts +++ b/apps/api/src/auth/dto/register.dto.ts @@ -52,6 +52,12 @@ export class RegisterDto { @IsBoolean() public readonly newsletter?: boolean + @ApiProperty() + @Expose() + @IsOptional() + @IsBoolean() + public readonly helpUsImprove?: boolean + @ValidateIf((o) => o.type === ProfileType.CORPORATE) @Expose() @IsString() diff --git a/apps/api/src/campaign/campaign.controller.spec.ts b/apps/api/src/campaign/campaign.controller.spec.ts index 2fb786fad..03379b1ef 100644 --- a/apps/api/src/campaign/campaign.controller.spec.ts +++ b/apps/api/src/campaign/campaign.controller.spec.ts @@ -23,7 +23,7 @@ import { MarketingNotificationsService } from '../notifications/notifications.se import { EmailService } from '../email/email.service' import { TemplateService } from '../email/template.service' import { CampaignNewsService } from '../campaign-news/campaign-news.service' -import { personMock } from '../person/__mock__/peronMock' +import { personMock } from '../person/__mock__/personMock' describe('CampaignController', () => { let controller: CampaignController diff --git a/apps/api/src/domain/generated/person/dto/create-person.dto.ts b/apps/api/src/domain/generated/person/dto/create-person.dto.ts index d96ba291e..2e7032ad4 100644 --- a/apps/api/src/domain/generated/person/dto/create-person.dto.ts +++ b/apps/api/src/domain/generated/person/dto/create-person.dto.ts @@ -4,6 +4,7 @@ export class CreatePersonDto { email?: string phone?: string newsletter?: boolean + helpUsImprove?: boolean address?: string birthday?: Date emailConfirmed?: boolean diff --git a/apps/api/src/domain/generated/person/dto/update-person.dto.ts b/apps/api/src/domain/generated/person/dto/update-person.dto.ts index b171e7db8..75f4c44fb 100644 --- a/apps/api/src/domain/generated/person/dto/update-person.dto.ts +++ b/apps/api/src/domain/generated/person/dto/update-person.dto.ts @@ -4,6 +4,7 @@ export class UpdatePersonDto { email?: string phone?: string newsletter?: boolean + helpUsImprove?: boolean address?: string birthday?: Date emailConfirmed?: boolean diff --git a/apps/api/src/domain/generated/person/entities/person.entity.ts b/apps/api/src/domain/generated/person/entities/person.entity.ts index fa0e3872a..4ee8cc896 100644 --- a/apps/api/src/domain/generated/person/entities/person.entity.ts +++ b/apps/api/src/domain/generated/person/entities/person.entity.ts @@ -29,6 +29,7 @@ export class Person { createdAt: Date updatedAt: Date | null newsletter: boolean | null + helpUsImprove: boolean | null address: string | null birthday: Date | null emailConfirmed: boolean | null diff --git a/apps/api/src/donations/__mocks__/paymentMock.ts b/apps/api/src/donations/__mocks__/paymentMock.ts index f3a5d80fd..57a5c7e88 100644 --- a/apps/api/src/donations/__mocks__/paymentMock.ts +++ b/apps/api/src/donations/__mocks__/paymentMock.ts @@ -1,7 +1,7 @@ import { Currency, DonationType, PaymentProvider, PaymentStatus } from '@prisma/client' import { PaymentWithDonation } from '../types/donation' import { DonationWithPerson } from '../types/donation' -import { personMock } from '../../person/__mock__/peronMock' +import { personMock } from '../../person/__mock__/personMock' export const mockDonation: DonationWithPerson = { id: '1234', diff --git a/apps/api/src/person/__mock__/peronMock.ts b/apps/api/src/person/__mock__/personMock.ts similarity index 91% rename from apps/api/src/person/__mock__/peronMock.ts rename to apps/api/src/person/__mock__/personMock.ts index 21b8f148c..f9797d67e 100644 --- a/apps/api/src/person/__mock__/peronMock.ts +++ b/apps/api/src/person/__mock__/personMock.ts @@ -17,5 +17,6 @@ export const personMock: Person = { birthday: null, personalNumber: null, stripeCustomerId: null, - profileEnabled: false, + profileEnabled: true, + helpUsImprove: false, } diff --git a/migrations/20240318124702_help_us_improve/migration.sql b/migrations/20240318124702_help_us_improve/migration.sql new file mode 100644 index 000000000..ac6932da2 --- /dev/null +++ b/migrations/20240318124702_help_us_improve/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "people" ADD COLUMN "help_us_improve" BOOLEAN DEFAULT false; diff --git a/podkrepi.dbml b/podkrepi.dbml index 451d653cd..26ccf535a 100644 --- a/podkrepi.dbml +++ b/podkrepi.dbml @@ -16,6 +16,7 @@ Table people { createdAt DateTime [default: `now()`, not null] updatedAt DateTime newsletter Boolean [default: false] + helpUsImprove Boolean [default: false] address String birthday DateTime emailConfirmed Boolean [default: false] diff --git a/schema.prisma b/schema.prisma index 73826c6d0..26452610f 100644 --- a/schema.prisma +++ b/schema.prisma @@ -40,6 +40,7 @@ model Person { updatedAt DateTime? @updatedAt @map("updated_at") @db.Timestamptz(6) // Receive marketing notifications newsletter Boolean? @default(false) + helpUsImprove Boolean? @default(false) @map("help_us_improve") address String? @db.VarChar(100) birthday DateTime? @db.Timestamptz(6) emailConfirmed Boolean? @default(false) @map("email_confirmed")