Skip to content

Commit

Permalink
add helpUsImprove field to the backend (#615)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kdachev97 authored Mar 19, 2024
1 parent 8b800c6 commit 4c5dee2
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 87 deletions.
31 changes: 7 additions & 24 deletions apps/api/src/affiliate/affiliate.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ import {
Campaign,
CampaignState,
PaymentStatus,
Payments,
Payment,
Prisma,
Vault,
Person,
} from '@prisma/client'
import { KeycloakTokenParsed } from '../auth/keycloak'
import { BadRequestException, ConflictException, ForbiddenException } from '@nestjs/common'
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 } }>

Check warning on line 33 in apps/api/src/affiliate/affiliate.controller.spec.ts

View workflow job for this annotation

GitHub Actions / Run API tests

'PersonWithPayload' is defined but never used
type AffiliateWithPayload = Prisma.AffiliateGetPayload<{
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -314,7 +297,7 @@ describe('AffiliateController', () => {
)
})
it('should cancel', async () => {
const cancelledDonationResponse: Payments = {
const cancelledDonationResponse: Payment = {
...mockGuaranteedPayment,
status: PaymentStatus.cancelled,
}
Expand All @@ -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,
}
Expand Down
77 changes: 17 additions & 60 deletions apps/api/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -32,28 +33,10 @@ describe('AuthService', () => {
let config: ConfigService
let admin: KeycloakAdminClient
let keycloak: KeycloakConnect.Keycloak
let marketing: NotificationsProviderInterface<unknown>
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({
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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 },
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/auth/dto/register.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/campaign/campaign.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class CreatePersonDto {
email?: string
phone?: string
newsletter?: boolean
helpUsImprove?: boolean
address?: string
birthday?: Date
emailConfirmed?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class UpdatePersonDto {
email?: string
phone?: string
newsletter?: boolean
helpUsImprove?: boolean
address?: string
birthday?: Date
emailConfirmed?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/donations/__mocks__/paymentMock.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export const personMock: Person = {
birthday: null,
personalNumber: null,
stripeCustomerId: null,
profileEnabled: false,
profileEnabled: true,
helpUsImprove: false,
}
2 changes: 2 additions & 0 deletions migrations/20240318124702_help_us_improve/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "people" ADD COLUMN "help_us_improve" BOOLEAN DEFAULT false;
1 change: 1 addition & 0 deletions podkrepi.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4c5dee2

Please sign in to comment.