From 8f0525a0375c7fcb5efe0e4ba52ce5b07ac607a2 Mon Sep 17 00:00:00 2001 From: Martbul Date: Tue, 25 Jun 2024 11:11:11 +0300 Subject: [PATCH 01/12] add create campaign agreements validation if any of the 3 agreements for creating campaign are not checked -> throw a HttpException with status 405 --- .../campaign-application.service.spec.ts | 65 +++++++++++++++++++ .../campaign-application.service.ts | 9 ++- .../dto/create-campaign-application.dto.ts | 6 +- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 42c9cb5e..469332e0 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -1,5 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing' import { CampaignApplicationService } from './campaign-application.service' +import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' +import { HttpException, HttpStatus } from '@nestjs/common' describe('CampaignApplicationService', () => { let service: CampaignApplicationService @@ -15,4 +17,67 @@ describe('CampaignApplicationService', () => { it('should be defined', () => { expect(service).toBeDefined() }) + + describe('createNewApplication', () => { + const baseDto = { + campaignName: 'Test Campaign', + organizerName: 'Test Organizer', + organizerEmail: 'organizer@example.com', + organizerPhone: '123456789', + beneficiary: 'Test Beneficiary', + organizerBeneficiaryRel: 'Test Relation', + goal: 'Test Goal', + amount: '1000', + toEntity: jest.fn(), // Mock implementation + } + it('should throw an error if acceptTermsAndConditions are not accepted', () => { + const dto: CreateCampaignApplicationDto = { + ...baseDto, + acceptTermsAndConditions: false, + transparencyTermsAccepted: true, + personalInformationProcessingAccepted: true, + } + + expect(() => service.create(dto)).toThrow( + new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED), + ) + }) + + it('should throw an error if transparencyTermsAccepted are not accepted', () => { + const dto: CreateCampaignApplicationDto = { + ...baseDto, + acceptTermsAndConditions: true, + transparencyTermsAccepted: false, + personalInformationProcessingAccepted: true, + } + + expect(() => service.create(dto)).toThrow( + new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED), + ) + }) + + it('should throw an error if personalInformationProcessingAccepted is not accepted', () => { + const dto: CreateCampaignApplicationDto = { + ...baseDto, + acceptTermsAndConditions: true, + transparencyTermsAccepted: true, + personalInformationProcessingAccepted: false, + } + + expect(() => service.create(dto)).toThrow( + new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED), + ) + }) + + it('should add a new campaign application if all agreements are accepted', () => { + const dto: CreateCampaignApplicationDto = { + ...baseDto, + acceptTermsAndConditions: true, + transparencyTermsAccepted: true, + personalInformationProcessingAccepted: true, + } + + expect(service.create(dto)).toBe('This action adds a new campaignApplication') + }) + }) }) diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index 296c03a8..5b22f3c5 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common' +import { HttpException, HttpStatus, Injectable } from '@nestjs/common' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' @@ -9,6 +9,13 @@ export class CampaignApplicationService { } create(createCampaignApplicationDto: CreateCampaignApplicationDto) { + if ( + !createCampaignApplicationDto.acceptTermsAndConditions || + !createCampaignApplicationDto.transparencyTermsAccepted || + !createCampaignApplicationDto.personalInformationProcessingAccepted + ) { + throw new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED) + } return 'This action adds a new campaignApplication' } diff --git a/apps/api/src/campaign-application/dto/create-campaign-application.dto.ts b/apps/api/src/campaign-application/dto/create-campaign-application.dto.ts index 69ad1ebc..43ad8f02 100644 --- a/apps/api/src/campaign-application/dto/create-campaign-application.dto.ts +++ b/apps/api/src/campaign-application/dto/create-campaign-application.dto.ts @@ -17,19 +17,19 @@ export class CreateCampaignApplicationDto { @ApiProperty() @Expose() @IsBoolean() - acceptTermsAndConditions: true + acceptTermsAndConditions: boolean /** user needs to agree to this as a prerequisite to creating a campaign application */ @ApiProperty() @Expose() @IsBoolean() - transparencyTermsAccepted: true + transparencyTermsAccepted: boolean /** user needs to agree to this as a prerequisite to creating a campaign application */ @ApiProperty() @Expose() @IsBoolean() - personalInformationProcessingAccepted: true + personalInformationProcessingAccepted: boolean /** Who is organizing this campaign */ @ApiProperty() From 77c9df81dc653935860e5556ed2740ac045a0c58 Mon Sep 17 00:00:00 2001 From: Martbul Date: Tue, 25 Jun 2024 21:38:46 +0300 Subject: [PATCH 02/12] fix http error status code in campaign-application.service http error code is now 400 in validation for all 3 agreements in create method --- .../campaign-application.service.spec.ts | 6 +++--- .../campaign-application/campaign-application.service.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 469332e0..584e3caa 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -39,7 +39,7 @@ describe('CampaignApplicationService', () => { } expect(() => service.create(dto)).toThrow( - new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED), + new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST), ) }) @@ -52,7 +52,7 @@ describe('CampaignApplicationService', () => { } expect(() => service.create(dto)).toThrow( - new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED), + new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST), ) }) @@ -65,7 +65,7 @@ describe('CampaignApplicationService', () => { } expect(() => service.create(dto)).toThrow( - new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED), + new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST), ) }) diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index 5b22f3c5..f84cd749 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -14,7 +14,7 @@ export class CampaignApplicationService { !createCampaignApplicationDto.transparencyTermsAccepted || !createCampaignApplicationDto.personalInformationProcessingAccepted ) { - throw new HttpException('All agreements must be checked', HttpStatus.METHOD_NOT_ALLOWED) + throw new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST) } return 'This action adds a new campaignApplication' } From 139a6a926cf46457ff83ab7b6019802ced963df5 Mon Sep 17 00:00:00 2001 From: Martbul Date: Tue, 25 Jun 2024 21:57:03 +0300 Subject: [PATCH 03/12] fix throw new BadRequestException changed the error from HttpException to BadRequestException for the agreements validation in create method in campaign-application.service.ts --- .../campaign-application.service.spec.ts | 8 ++++---- .../campaign-application/campaign-application.service.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 584e3caa..b48a5fb9 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -1,7 +1,7 @@ import { Test, TestingModule } from '@nestjs/testing' import { CampaignApplicationService } from './campaign-application.service' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' -import { HttpException, HttpStatus } from '@nestjs/common' +import { BadRequestException, HttpStatus } from '@nestjs/common' describe('CampaignApplicationService', () => { let service: CampaignApplicationService @@ -39,7 +39,7 @@ describe('CampaignApplicationService', () => { } expect(() => service.create(dto)).toThrow( - new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST), + new BadRequestException('All agreements must be checked'), ) }) @@ -52,7 +52,7 @@ describe('CampaignApplicationService', () => { } expect(() => service.create(dto)).toThrow( - new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST), + new BadRequestException('All agreements must be checked'), ) }) @@ -65,7 +65,7 @@ describe('CampaignApplicationService', () => { } expect(() => service.create(dto)).toThrow( - new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST), + new BadRequestException('All agreements must be checked'), ) }) diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index f84cd749..532bbba1 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -1,4 +1,4 @@ -import { HttpException, HttpStatus, Injectable } from '@nestjs/common' +import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nestjs/common' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' @@ -14,7 +14,7 @@ export class CampaignApplicationService { !createCampaignApplicationDto.transparencyTermsAccepted || !createCampaignApplicationDto.personalInformationProcessingAccepted ) { - throw new HttpException('All agreements must be checked', HttpStatus.BAD_REQUEST) + throw new BadRequestException('All agreements must be checked') } return 'This action adds a new campaignApplication' } From 15ddce4b88a4f2aa9df4fee3fb8355f467f08f37 Mon Sep 17 00:00:00 2001 From: Martbul Date: Wed, 26 Jun 2024 09:56:53 +0300 Subject: [PATCH 04/12] injected prisma service --- .../src/campaign-application/campaign-application.module.ts | 2 ++ .../campaign-application/campaign-application.service.spec.ts | 2 +- .../src/campaign-application/campaign-application.service.ts | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/api/src/campaign-application/campaign-application.module.ts b/apps/api/src/campaign-application/campaign-application.module.ts index e574e3f7..bbefbc71 100644 --- a/apps/api/src/campaign-application/campaign-application.module.ts +++ b/apps/api/src/campaign-application/campaign-application.module.ts @@ -1,8 +1,10 @@ import { Module } from '@nestjs/common' import { CampaignApplicationService } from './campaign-application.service' import { CampaignApplicationController } from './campaign-application.controller' +import { PrismaModule } from '../prisma/prisma.module' @Module({ + imports: [PrismaModule], controllers: [CampaignApplicationController], providers: [CampaignApplicationService], }) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index b48a5fb9..7e417ece 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -28,7 +28,7 @@ describe('CampaignApplicationService', () => { organizerBeneficiaryRel: 'Test Relation', goal: 'Test Goal', amount: '1000', - toEntity: jest.fn(), // Mock implementation + toEntity: jest.fn(), } it('should throw an error if acceptTermsAndConditions are not accepted', () => { const dto: CreateCampaignApplicationDto = { diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index 532bbba1..bba3d625 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -1,9 +1,12 @@ import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nestjs/common' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' +import { PrismaService } from '../prisma/prisma.service'; @Injectable() export class CampaignApplicationService { + constructor(private prisma: PrismaService) {} + async getCampaignByIdWithPersonIds(id: string): Promise { throw new Error('Method not implemented.') } From 7d4333547f2aeead7094b469b86eb70501cc91d3 Mon Sep 17 00:00:00 2001 From: Martbul Date: Wed, 26 Jun 2024 20:53:34 +0300 Subject: [PATCH 05/12] add findAll camapings method created findAll functionality in campaign-application.service and add testing for the method --- .../campaign-application.service.spec.ts | 149 +++++++++++++++++- .../campaign-application.service.ts | 4 +- 2 files changed, 147 insertions(+), 6 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 7e417ece..844c3a11 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -2,14 +2,73 @@ import { Test, TestingModule } from '@nestjs/testing' import { CampaignApplicationService } from './campaign-application.service' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { BadRequestException, HttpStatus } from '@nestjs/common' - +import { CampaignState, Currency } from '@prisma/client' +import { CreateCampaignDto } from '../campaign/dto/create-campaign.dto' +import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock' +import { CampaignService } from '../campaign/campaign.service' +import { NotificationService } from '../sockets/notifications/notification.service' +import { VaultService } from '../vault/vault.service' +import { ConfigService } from '@nestjs/config' +import { MarketingNotificationsService } from '../notifications/notifications.service' +import { PersonService } from '../person/person.service' +import { EmailService } from '../email/email.service' +import { NotificationsProviderInterface } from '../notifications/providers/notifications.interface.providers' +import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider' +import { NotificationGateway } from '../sockets/notifications/gateway' +import { TemplateService } from '../email/template.service' describe('CampaignApplicationService', () => { let service: CampaignApplicationService + const mockCreateCampaign = { + slug: 'test-slug', + title: 'Test name', + description: 'Test description', + essence: 'test', + coordinatorId: 'testCoordinatorId', + beneficiaryId: 'testBeneficiaryId', + organizerId: 'testOrganizerId', + companyId: 'testCompanyId', + campaignTypeId: 'testCampaignTypeId', + targetAmount: 1000, + reachedAmount: 0, + + startDate: new Date('2021-04-08T06:36:33.661Z'), + endDate: new Date('2023-04-08T06:36:33.661Z'), + currency: Currency.BGN, + // donationWish: undefined, + allowDonationOnComplete: true, + toEntity: new CreateCampaignDto().toEntity, + } as CreateCampaignDto + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [CampaignApplicationService], - }).compile() + providers: [ + { + // Use the interface as token + provide: NotificationsProviderInterface, + // But actually provide the service that implements the interface + useClass: SendGridNotificationsProvider, + }, + CampaignService, + MockPrismaService, + NotificationService, + VaultService, + MarketingNotificationsService, + ConfigService, + PersonService, + EmailService, + NotificationGateway, + TemplateService, + CampaignApplicationService, + ], + }) + .overrideProvider(EmailService) + .useValue({ + sendFromTemplate: jest.fn(() => { + return true + }), + }) + .compile() service = module.get(CampaignApplicationService) }) @@ -28,7 +87,7 @@ describe('CampaignApplicationService', () => { organizerBeneficiaryRel: 'Test Relation', goal: 'Test Goal', amount: '1000', - toEntity: jest.fn(), + toEntity: jest.fn(), } it('should throw an error if acceptTermsAndConditions are not accepted', () => { const dto: CreateCampaignApplicationDto = { @@ -80,4 +139,86 @@ describe('CampaignApplicationService', () => { expect(service.create(dto)).toBe('This action adds a new campaignApplication') }) }) + + describe('find all(GET) campains', () => { + it('should return an array of campaign applications', async () => { + const paymentReferenceMock = 'NY5P-KVO4-DNBZ' + const mockCampaigns = [ + { + ...mockCreateCampaign, + ...{ + id: 'testId', + state: CampaignState.draft, + createdAt: new Date('2022-04-08T06:36:33.661Z'), + updatedAt: new Date('2022-04-08T06:36:33.662Z'), + deletedAt: null, + approvedById: null, + beneficiary: { person: { keycloakId: 'some-id' } }, + coordinator: { person: { keycloakId: 'some-id' } }, + organizer: { person: { keycloakId: 'some-id' } }, + paymentReference: paymentReferenceMock, + campaignFiles: [], + donationWish: [], + irregularities: [], + outgoingTransfers: [], + incomingTransfers: [], + vaults: [], + slugArchive: [], + withdrawals: [], + notificationLists: [], + campaignNews: [], + }, + }, + { + ...mockCreateCampaign, + ...{ + id: 'testId', + state: CampaignState.draft, + createdAt: new Date('2022-04-08T06:36:33.661Z'), + updatedAt: new Date('2022-04-08T06:36:33.662Z'), + deletedAt: null, + approvedById: null, + beneficiary: { person: { keycloakId: 'some-id2' } }, + coordinator: { person: { keycloakId: 'some-id2' } }, + organizer: { person: { keycloakId: 'some-id2' } }, + paymentReference: paymentReferenceMock, + campaignFiles: [], + donationWish: [], + irregularities: [], + outgoingTransfers: [], + incomingTransfers: [], + withdrawals: [], + slugArchive: [], + campaignNews: [], + vaults: [], + notificationLists: [], + }, + }, + ] + + prismaMock.campaignApplication.findMany.mockResolvedValue(mockCampaigns) + + const result = await service.findAll() + + expect(result).toEqual(mockCampaigns) + expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) + }) + + it('should return an empty array if no campaigns are found', async () => { + prismaMock.campaignApplication.findMany.mockResolvedValue([]) + + const result = await service.findAll() + + expect(result).toEqual([]) + expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) + }) + + it('should handle errors and throw an exception', async () => { + const errorMessage = 'Database error' + prismaMock.campaignApplication.findMany.mockRejectedValue(new Error(errorMessage)) + + await expect(service.findAll()).rejects.toThrow(errorMessage) + expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) + }) + }) }) diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index bba3d625..036a77ba 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -1,7 +1,7 @@ import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nestjs/common' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' -import { PrismaService } from '../prisma/prisma.service'; +import { PrismaService } from '../prisma/prisma.service' @Injectable() export class CampaignApplicationService { @@ -23,7 +23,7 @@ export class CampaignApplicationService { } findAll() { - return `This action returns all campaignApplication` + return this.prisma.campaignApplication.findMany() } findOne(id: string) { From 1280e2b1ca2fb8eac26a312b86dc1649927f1908 Mon Sep 17 00:00:00 2001 From: Martbul Date: Wed, 26 Jun 2024 21:00:56 +0300 Subject: [PATCH 06/12] reverted commit --- apps/api/src/campaign-application/campaign-application.module.ts | 1 - .../api/src/campaign-application/campaign-application.service.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.module.ts b/apps/api/src/campaign-application/campaign-application.module.ts index bbefbc71..0c421d19 100644 --- a/apps/api/src/campaign-application/campaign-application.module.ts +++ b/apps/api/src/campaign-application/campaign-application.module.ts @@ -2,7 +2,6 @@ import { Module } from '@nestjs/common' import { CampaignApplicationService } from './campaign-application.service' import { CampaignApplicationController } from './campaign-application.controller' import { PrismaModule } from '../prisma/prisma.module' - @Module({ imports: [PrismaModule], controllers: [CampaignApplicationController], diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index 036a77ba..9b4a0bcc 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -6,7 +6,6 @@ import { PrismaService } from '../prisma/prisma.service' @Injectable() export class CampaignApplicationService { constructor(private prisma: PrismaService) {} - async getCampaignByIdWithPersonIds(id: string): Promise { throw new Error('Method not implemented.') } From cb14fdfcb198ea42b89fb3e095300a4341477633 Mon Sep 17 00:00:00 2001 From: Martbul Date: Thu, 27 Jun 2024 08:59:57 +0300 Subject: [PATCH 07/12] fix test for findAll (campaign-applications) method new the mocked campaign-applications in the tests have the right schema(schema of the campaign-application) --- .../campaign-application.service.spec.ts | 117 +++++++----------- 1 file changed, 48 insertions(+), 69 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 844c3a11..5018f075 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -2,7 +2,12 @@ import { Test, TestingModule } from '@nestjs/testing' import { CampaignApplicationService } from './campaign-application.service' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { BadRequestException, HttpStatus } from '@nestjs/common' -import { CampaignState, Currency } from '@prisma/client' +import { + CampaignApplicationState, + CampaignState, + CampaignTypeCategory, + Currency, +} from '@prisma/client' import { CreateCampaignDto } from '../campaign/dto/create-campaign.dto' import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock' import { CampaignService } from '../campaign/campaign.service' @@ -19,27 +24,6 @@ import { TemplateService } from '../email/template.service' describe('CampaignApplicationService', () => { let service: CampaignApplicationService - const mockCreateCampaign = { - slug: 'test-slug', - title: 'Test name', - description: 'Test description', - essence: 'test', - coordinatorId: 'testCoordinatorId', - beneficiaryId: 'testBeneficiaryId', - organizerId: 'testOrganizerId', - companyId: 'testCompanyId', - campaignTypeId: 'testCampaignTypeId', - targetAmount: 1000, - reachedAmount: 0, - - startDate: new Date('2021-04-08T06:36:33.661Z'), - endDate: new Date('2023-04-08T06:36:33.661Z'), - currency: Currency.BGN, - // donationWish: undefined, - allowDonationOnComplete: true, - toEntity: new CreateCampaignDto().toEntity, - } as CreateCampaignDto - beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ @@ -142,57 +126,52 @@ describe('CampaignApplicationService', () => { describe('find all(GET) campains', () => { it('should return an array of campaign applications', async () => { - const paymentReferenceMock = 'NY5P-KVO4-DNBZ' const mockCampaigns = [ { - ...mockCreateCampaign, - ...{ - id: 'testId', - state: CampaignState.draft, - createdAt: new Date('2022-04-08T06:36:33.661Z'), - updatedAt: new Date('2022-04-08T06:36:33.662Z'), - deletedAt: null, - approvedById: null, - beneficiary: { person: { keycloakId: 'some-id' } }, - coordinator: { person: { keycloakId: 'some-id' } }, - organizer: { person: { keycloakId: 'some-id' } }, - paymentReference: paymentReferenceMock, - campaignFiles: [], - donationWish: [], - irregularities: [], - outgoingTransfers: [], - incomingTransfers: [], - vaults: [], - slugArchive: [], - withdrawals: [], - notificationLists: [], - campaignNews: [], - }, + id: 'testId', + createdAt: new Date('2022-04-08T06:36:33.661Z'), + updatedAt: new Date('2022-04-08T06:36:33.662Z'), + description: 'Test description', + organizerId: 'testOrganizerId1', + organizerName: 'Test Organizer1', + organizerEmail: 'organizer@example.com', + beneficiary: 'test beneficary', + organizerPhone: '123456789', + organizerBeneficiaryRel: 'Test Relation', + campaignName: 'Test Campaign', + goal: 'Test Goal', + history: 'test history', + amount: '1000', + campaignGuarantee: 'test campaignGuarantee', + otherFinanceSources: 'test otherFinanceSources', + otherNotes: 'test otherNotes', + state: CampaignApplicationState.review, + category: CampaignTypeCategory.medical, + ticketURL: 'testsodifhso', + archived: false, }, { - ...mockCreateCampaign, - ...{ - id: 'testId', - state: CampaignState.draft, - createdAt: new Date('2022-04-08T06:36:33.661Z'), - updatedAt: new Date('2022-04-08T06:36:33.662Z'), - deletedAt: null, - approvedById: null, - beneficiary: { person: { keycloakId: 'some-id2' } }, - coordinator: { person: { keycloakId: 'some-id2' } }, - organizer: { person: { keycloakId: 'some-id2' } }, - paymentReference: paymentReferenceMock, - campaignFiles: [], - donationWish: [], - irregularities: [], - outgoingTransfers: [], - incomingTransfers: [], - withdrawals: [], - slugArchive: [], - campaignNews: [], - vaults: [], - notificationLists: [], - }, + id: 'testId', + createdAt: new Date('2022-04-08T06:36:33.661Z'), + updatedAt: new Date('2022-04-08T06:36:33.662Z'), + description: 'Test description', + organizerId: 'testOrganizerId1', + organizerName: 'Test Organizer1', + organizerEmail: 'organizer@example.com', + beneficiary: 'test beneficary', + organizerPhone: '123456789', + organizerBeneficiaryRel: 'Test Relation', + campaignName: 'Test Campaign', + goal: 'Test Goal', + history: 'test history', + amount: '1000', + campaignGuarantee: 'test campaignGuarantee', + otherFinanceSources: 'test otherFinanceSources', + otherNotes: 'test otherNotes', + state: CampaignApplicationState.review, + category: CampaignTypeCategory.medical, + ticketURL: 'testsodifhso', + archived: false, }, ] From 3fdbb752113d4f82ffb69433a683e5c11fbef412 Mon Sep 17 00:00:00 2001 From: Martbul Date: Thu, 27 Jun 2024 19:55:11 +0300 Subject: [PATCH 08/12] working on "cleaning my branches" --- .../campaign-application.module.ts | 8 ++- .../campaign-application.service.spec.ts | 5 ++ .../campaign-application.service.ts | 55 ++++++++++++++++++- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.module.ts b/apps/api/src/campaign-application/campaign-application.module.ts index 0c421d19..9ca4ace4 100644 --- a/apps/api/src/campaign-application/campaign-application.module.ts +++ b/apps/api/src/campaign-application/campaign-application.module.ts @@ -1,10 +1,16 @@ import { Module } from '@nestjs/common' import { CampaignApplicationService } from './campaign-application.service' import { CampaignApplicationController } from './campaign-application.controller' + import { PrismaModule } from '../prisma/prisma.module' +import { OrganizerService } from '../organizer/organizer.service' +import { PersonService } from '../person/person.service' @Module({ imports: [PrismaModule], controllers: [CampaignApplicationController], - providers: [CampaignApplicationService], + providers: [CampaignApplicationService, OrganizerService, PersonService], }) export class CampaignApplicationModule {} + + + diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 5018f075..d54a7179 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -21,6 +21,9 @@ import { NotificationsProviderInterface } from '../notifications/providers/notif import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider' import { NotificationGateway } from '../sockets/notifications/gateway' import { TemplateService } from '../email/template.service' +import { OrganizerModule } from '../organizer/organizer.module' // import OrganizerModule +import { OrganizerService } from '../organizer/organizer.service' + describe('CampaignApplicationService', () => { let service: CampaignApplicationService @@ -44,7 +47,9 @@ describe('CampaignApplicationService', () => { NotificationGateway, TemplateService, CampaignApplicationService, + OrganizerService, // Add OrganizerService here ], + imports: [OrganizerModule], // }) .overrideProvider(EmailService) .useValue({ diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index 9b4a0bcc..b4a01a05 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -2,15 +2,22 @@ import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nes import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' import { PrismaService } from '../prisma/prisma.service' +import { OrganizerService } from '../organizer/organizer.service' +import { PersonService } from '../person/person.service' +import { log } from 'console' @Injectable() export class CampaignApplicationService { - constructor(private prisma: PrismaService) {} + constructor( + private prisma: PrismaService, + private organizerService: OrganizerService, + private personService: PersonService, + ) {} async getCampaignByIdWithPersonIds(id: string): Promise { throw new Error('Method not implemented.') } - create(createCampaignApplicationDto: CreateCampaignApplicationDto) { + async create(createCampaignApplicationDto: CreateCampaignApplicationDto) { if ( !createCampaignApplicationDto.acceptTermsAndConditions || !createCampaignApplicationDto.transparencyTermsAccepted || @@ -18,7 +25,49 @@ export class CampaignApplicationService { ) { throw new BadRequestException('All agreements must be checked') } - return 'This action adds a new campaignApplication' + + //! debugging purposes - delete when pushing + const users = await this.personService.findAll() + const organaisers = await this.organizerService.findAll() + console.log(users) + console.log(organaisers) + + //! not sure if it is my job to create organizer + let organizer = await this.prisma.organizer.findUnique({ + //this is just Id(NOT keycloackId) + where: { id: 'f8563be6-384c-4f92-a1bb-304408ee2eb9' }, + }) + + if (!organizer) { + //this is just Id(NOT keycloackId) + organizer = await this.organizerService.create({ + personId: 'f8563be6-384c-4f92-a1bb-304408ee2eb9', + }) + } + + //! task - [API] [Organizer] Create a CampaignApplication - persist to db + const newCampaignApplication = this.prisma.campaignApplication.create({ + data: { + campaignName: createCampaignApplicationDto.campaignName, + organizerName: createCampaignApplicationDto.organizerName, + organizerEmail: createCampaignApplicationDto.organizerEmail, + organizerPhone: createCampaignApplicationDto.organizerPhone, + beneficiary: createCampaignApplicationDto.beneficiary, + organizerBeneficiaryRel: createCampaignApplicationDto.organizerBeneficiaryRel, + goal: createCampaignApplicationDto.goal, + history: createCampaignApplicationDto.history, + amount: createCampaignApplicationDto.amount, + description: createCampaignApplicationDto.description, + campaignGuarantee: createCampaignApplicationDto.campaignGuarantee, + otherFinanceSources: createCampaignApplicationDto.otherFinanceSources, + otherNotes: createCampaignApplicationDto.otherNotes, + category: createCampaignApplicationDto.category, + organizerId: 'f8563be6-384c-4f92-a1bb-304408ee2eb9', + // organizerId: organizer, + }, + }) + + return newCampaignApplication } findAll() { From 6199f1d91305964c2803bdc6dae3bafdf4af2126 Mon Sep 17 00:00:00 2001 From: Martbul Date: Thu, 27 Jun 2024 20:23:58 +0300 Subject: [PATCH 09/12] Revert "fix test for findAll (campaign-applications) method" This reverts commit cb14fdfcb198ea42b89fb3e095300a4341477633. --- .../campaign-application.service.spec.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index b48a5fb9..7e261529 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -6,6 +6,27 @@ import { BadRequestException, HttpStatus } from '@nestjs/common' describe('CampaignApplicationService', () => { let service: CampaignApplicationService + const mockCreateCampaign = { + slug: 'test-slug', + title: 'Test name', + description: 'Test description', + essence: 'test', + coordinatorId: 'testCoordinatorId', + beneficiaryId: 'testBeneficiaryId', + organizerId: 'testOrganizerId', + companyId: 'testCompanyId', + campaignTypeId: 'testCampaignTypeId', + targetAmount: 1000, + reachedAmount: 0, + + startDate: new Date('2021-04-08T06:36:33.661Z'), + endDate: new Date('2023-04-08T06:36:33.661Z'), + currency: Currency.BGN, + // donationWish: undefined, + allowDonationOnComplete: true, + toEntity: new CreateCampaignDto().toEntity, + } as CreateCampaignDto + beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [CampaignApplicationService], From a6d35fb31cca0fca38fb18b9dfcbb4aa04a50342 Mon Sep 17 00:00:00 2001 From: Martbul Date: Thu, 27 Jun 2024 22:03:58 +0300 Subject: [PATCH 10/12] add findAll(GET) method in the campaign-application.service --- .../campaign-application.module.ts | 8 +-- .../campaign-application.service.spec.ts | 26 --------- .../campaign-application.service.ts | 55 +------------------ 3 files changed, 4 insertions(+), 85 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.module.ts b/apps/api/src/campaign-application/campaign-application.module.ts index 9ca4ace4..0c421d19 100644 --- a/apps/api/src/campaign-application/campaign-application.module.ts +++ b/apps/api/src/campaign-application/campaign-application.module.ts @@ -1,16 +1,10 @@ import { Module } from '@nestjs/common' import { CampaignApplicationService } from './campaign-application.service' import { CampaignApplicationController } from './campaign-application.controller' - import { PrismaModule } from '../prisma/prisma.module' -import { OrganizerService } from '../organizer/organizer.service' -import { PersonService } from '../person/person.service' @Module({ imports: [PrismaModule], controllers: [CampaignApplicationController], - providers: [CampaignApplicationService, OrganizerService, PersonService], + providers: [CampaignApplicationService], }) export class CampaignApplicationModule {} - - - diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 1317d59f..5018f075 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -21,33 +21,9 @@ import { NotificationsProviderInterface } from '../notifications/providers/notif import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider' import { NotificationGateway } from '../sockets/notifications/gateway' import { TemplateService } from '../email/template.service' -import { OrganizerModule } from '../organizer/organizer.module' // import OrganizerModule -import { OrganizerService } from '../organizer/organizer.service' - describe('CampaignApplicationService', () => { let service: CampaignApplicationService - const mockCreateCampaign = { - slug: 'test-slug', - title: 'Test name', - description: 'Test description', - essence: 'test', - coordinatorId: 'testCoordinatorId', - beneficiaryId: 'testBeneficiaryId', - organizerId: 'testOrganizerId', - companyId: 'testCompanyId', - campaignTypeId: 'testCampaignTypeId', - targetAmount: 1000, - reachedAmount: 0, - - startDate: new Date('2021-04-08T06:36:33.661Z'), - endDate: new Date('2023-04-08T06:36:33.661Z'), - currency: Currency.BGN, - // donationWish: undefined, - allowDonationOnComplete: true, - toEntity: new CreateCampaignDto().toEntity, - } as CreateCampaignDto - beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ @@ -68,9 +44,7 @@ describe('CampaignApplicationService', () => { NotificationGateway, TemplateService, CampaignApplicationService, - OrganizerService, // Add OrganizerService here ], - imports: [OrganizerModule], // }) .overrideProvider(EmailService) .useValue({ diff --git a/apps/api/src/campaign-application/campaign-application.service.ts b/apps/api/src/campaign-application/campaign-application.service.ts index b4a01a05..9b4a0bcc 100644 --- a/apps/api/src/campaign-application/campaign-application.service.ts +++ b/apps/api/src/campaign-application/campaign-application.service.ts @@ -2,22 +2,15 @@ import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nes import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' import { PrismaService } from '../prisma/prisma.service' -import { OrganizerService } from '../organizer/organizer.service' -import { PersonService } from '../person/person.service' -import { log } from 'console' @Injectable() export class CampaignApplicationService { - constructor( - private prisma: PrismaService, - private organizerService: OrganizerService, - private personService: PersonService, - ) {} + constructor(private prisma: PrismaService) {} async getCampaignByIdWithPersonIds(id: string): Promise { throw new Error('Method not implemented.') } - async create(createCampaignApplicationDto: CreateCampaignApplicationDto) { + create(createCampaignApplicationDto: CreateCampaignApplicationDto) { if ( !createCampaignApplicationDto.acceptTermsAndConditions || !createCampaignApplicationDto.transparencyTermsAccepted || @@ -25,49 +18,7 @@ export class CampaignApplicationService { ) { throw new BadRequestException('All agreements must be checked') } - - //! debugging purposes - delete when pushing - const users = await this.personService.findAll() - const organaisers = await this.organizerService.findAll() - console.log(users) - console.log(organaisers) - - //! not sure if it is my job to create organizer - let organizer = await this.prisma.organizer.findUnique({ - //this is just Id(NOT keycloackId) - where: { id: 'f8563be6-384c-4f92-a1bb-304408ee2eb9' }, - }) - - if (!organizer) { - //this is just Id(NOT keycloackId) - organizer = await this.organizerService.create({ - personId: 'f8563be6-384c-4f92-a1bb-304408ee2eb9', - }) - } - - //! task - [API] [Organizer] Create a CampaignApplication - persist to db - const newCampaignApplication = this.prisma.campaignApplication.create({ - data: { - campaignName: createCampaignApplicationDto.campaignName, - organizerName: createCampaignApplicationDto.organizerName, - organizerEmail: createCampaignApplicationDto.organizerEmail, - organizerPhone: createCampaignApplicationDto.organizerPhone, - beneficiary: createCampaignApplicationDto.beneficiary, - organizerBeneficiaryRel: createCampaignApplicationDto.organizerBeneficiaryRel, - goal: createCampaignApplicationDto.goal, - history: createCampaignApplicationDto.history, - amount: createCampaignApplicationDto.amount, - description: createCampaignApplicationDto.description, - campaignGuarantee: createCampaignApplicationDto.campaignGuarantee, - otherFinanceSources: createCampaignApplicationDto.otherFinanceSources, - otherNotes: createCampaignApplicationDto.otherNotes, - category: createCampaignApplicationDto.category, - organizerId: 'f8563be6-384c-4f92-a1bb-304408ee2eb9', - // organizerId: organizer, - }, - }) - - return newCampaignApplication + return 'This action adds a new campaignApplication' } findAll() { From 480d492c3e1578251ef8ebcd0e2feff7fe291368 Mon Sep 17 00:00:00 2001 From: Martbul Date: Fri, 28 Jun 2024 20:19:34 +0300 Subject: [PATCH 11/12] fix campaign-application controller and services remove unused providers in campaign-application.spec.ts add validation if user is admin in findAll method in campaign-application.controller.ts add tests for findAll method --- .../campaign-application.controller.spec.ts | 21 +- .../campaign-application.controller.ts | 7 +- .../campaign-application.service.spec.ts | 300 ++++++++++++++---- 3 files changed, 261 insertions(+), 67 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.controller.spec.ts b/apps/api/src/campaign-application/campaign-application.controller.spec.ts index 8f0be134..21244b12 100644 --- a/apps/api/src/campaign-application/campaign-application.controller.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.controller.spec.ts @@ -2,6 +2,10 @@ import { Test, TestingModule } from '@nestjs/testing' import { CampaignApplicationController } from './campaign-application.controller' import { CampaignApplicationService } from './campaign-application.service' import { SpyOf, autoSpy } from '@podkrepi-bg/testing' +import { ForbiddenException } from '@nestjs/common' +import { KeycloakTokenParsed, isAdmin } from '../auth/keycloak' + +jest.mock('../auth/keycloak') describe('CampaignApplicationController', () => { let controller: CampaignApplicationController @@ -43,14 +47,27 @@ describe('CampaignApplicationController', () => { }) }) - it('when findAll called it should delegate to the service findAll', () => { + it('when findAll called by a non-admin user it should throw a ForbiddenException', () => { // arrange + const user = { sub: 'non-admin', 'allowed-origins': ['test'] } as KeycloakTokenParsed + ;(isAdmin as jest.Mock).mockReturnValue(false) + + // act & assert + expect(() => controller.findAll(user)).toThrow(ForbiddenException) + }) + + it('when findAll called by an admin user it should delegate to the service findAll', () => { + // arrange + const user = { sub: 'admin', 'allowed-origins': ['test'] } as KeycloakTokenParsed + ;(isAdmin as jest.Mock).mockReturnValue(true) + // act - controller.findAll() + controller.findAll(user) // assert expect(service.findAll).toHaveBeenCalledWith() }) + it('when findOne called it should delegate to the service findOne', () => { // arrange // act diff --git a/apps/api/src/campaign-application/campaign-application.controller.ts b/apps/api/src/campaign-application/campaign-application.controller.ts index 7f13109f..8a215172 100644 --- a/apps/api/src/campaign-application/campaign-application.controller.ts +++ b/apps/api/src/campaign-application/campaign-application.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body, Patch, Param } from '@nestjs/common' +import { Controller, Get, Post, Body, Patch, Param, ForbiddenException } from '@nestjs/common' import { CampaignApplicationService } from './campaign-application.service' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto' @@ -19,7 +19,10 @@ export class CampaignApplicationController { } @Get('list') - findAll() { + findAll(@AuthenticatedUser() user: KeycloakTokenParsed) { + if (!isAdmin(user)) { + throw new ForbiddenException('Must be admin to get all campaign-applications') + } return this.campaignApplicationService.findAll() } diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 5018f075..32f07ffc 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -1,58 +1,27 @@ import { Test, TestingModule } from '@nestjs/testing' import { CampaignApplicationService } from './campaign-application.service' import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' -import { BadRequestException, HttpStatus } from '@nestjs/common' -import { - CampaignApplicationState, - CampaignState, - CampaignTypeCategory, - Currency, -} from '@prisma/client' -import { CreateCampaignDto } from '../campaign/dto/create-campaign.dto' +import { BadRequestException } from '@nestjs/common' +import { CampaignApplicationState, CampaignTypeCategory } from '@prisma/client' import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock' -import { CampaignService } from '../campaign/campaign.service' -import { NotificationService } from '../sockets/notifications/notification.service' -import { VaultService } from '../vault/vault.service' -import { ConfigService } from '@nestjs/config' -import { MarketingNotificationsService } from '../notifications/notifications.service' -import { PersonService } from '../person/person.service' import { EmailService } from '../email/email.service' -import { NotificationsProviderInterface } from '../notifications/providers/notifications.interface.providers' -import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider' -import { NotificationGateway } from '../sockets/notifications/gateway' -import { TemplateService } from '../email/template.service' + describe('CampaignApplicationService', () => { let service: CampaignApplicationService beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ + CampaignApplicationService, + MockPrismaService, { - // Use the interface as token - provide: NotificationsProviderInterface, - // But actually provide the service that implements the interface - useClass: SendGridNotificationsProvider, + provide: EmailService, + useValue: { + sendFromTemplate: jest.fn(() => true), + }, }, - CampaignService, - MockPrismaService, - NotificationService, - VaultService, - MarketingNotificationsService, - ConfigService, - PersonService, - EmailService, - NotificationGateway, - TemplateService, - CampaignApplicationService, ], - }) - .overrideProvider(EmailService) - .useValue({ - sendFromTemplate: jest.fn(() => { - return true - }), - }) - .compile() + }).compile() service = module.get(CampaignApplicationService) }) @@ -73,7 +42,8 @@ describe('CampaignApplicationService', () => { amount: '1000', toEntity: jest.fn(), } - it('should throw an error if acceptTermsAndConditions are not accepted', () => { + + it('should throw an error if acceptTermsAndConditions is false', () => { const dto: CreateCampaignApplicationDto = { ...baseDto, acceptTermsAndConditions: false, @@ -86,7 +56,7 @@ describe('CampaignApplicationService', () => { ) }) - it('should throw an error if transparencyTermsAccepted are not accepted', () => { + it('should throw an error if transparencyTermsAccepted is false', () => { const dto: CreateCampaignApplicationDto = { ...baseDto, acceptTermsAndConditions: true, @@ -99,7 +69,7 @@ describe('CampaignApplicationService', () => { ) }) - it('should throw an error if personalInformationProcessingAccepted is not accepted', () => { + it('should throw an error if personalInformationProcessingAccepted is false', () => { const dto: CreateCampaignApplicationDto = { ...baseDto, acceptTermsAndConditions: true, @@ -112,7 +82,7 @@ describe('CampaignApplicationService', () => { ) }) - it('should add a new campaign application if all agreements are accepted', () => { + it('should add a new campaign-application if all agreements are true', () => { const dto: CreateCampaignApplicationDto = { ...baseDto, acceptTermsAndConditions: true, @@ -124,8 +94,8 @@ describe('CampaignApplicationService', () => { }) }) - describe('find all(GET) campains', () => { - it('should return an array of campaign applications', async () => { + describe('findAll', () => { + it('should return an array of campaign-applications', async () => { const mockCampaigns = [ { id: 'testId', @@ -151,26 +121,26 @@ describe('CampaignApplicationService', () => { archived: false, }, { - id: 'testId', + id: 'testId2', createdAt: new Date('2022-04-08T06:36:33.661Z'), updatedAt: new Date('2022-04-08T06:36:33.662Z'), description: 'Test description', - organizerId: 'testOrganizerId1', - organizerName: 'Test Organizer1', - organizerEmail: 'organizer@example.com', - beneficiary: 'test beneficary', + organizerId: 'testOrganizerId2', + organizerName: 'Test Organizer2', + organizerEmail: 'organizer2@example.com', + beneficiary: 'test beneficary2', organizerPhone: '123456789', - organizerBeneficiaryRel: 'Test Relation', - campaignName: 'Test Campaign', - goal: 'Test Goal', - history: 'test history', - amount: '1000', - campaignGuarantee: 'test campaignGuarantee', - otherFinanceSources: 'test otherFinanceSources', - otherNotes: 'test otherNotes', + organizerBeneficiaryRel: 'Test Relation2', + campaignName: 'Test Campaign2', + goal: 'Test Goal2', + history: 'test history2', + amount: '2000', + campaignGuarantee: 'test campaignGuarantee2', + otherFinanceSources: 'test otherFinanceSources2', + otherNotes: 'test otherNotes2', state: CampaignApplicationState.review, category: CampaignTypeCategory.medical, - ticketURL: 'testsodifhso', + ticketURL: 'testsodifhso2', archived: false, }, ] @@ -183,7 +153,7 @@ describe('CampaignApplicationService', () => { expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) }) - it('should return an empty array if no campaigns are found', async () => { + it('should return an empty array if no campaign-applications are found', async () => { prismaMock.campaignApplication.findMany.mockResolvedValue([]) const result = await service.findAll() @@ -193,7 +163,7 @@ describe('CampaignApplicationService', () => { }) it('should handle errors and throw an exception', async () => { - const errorMessage = 'Database error' + const errorMessage = 'error' prismaMock.campaignApplication.findMany.mockRejectedValue(new Error(errorMessage)) await expect(service.findAll()).rejects.toThrow(errorMessage) @@ -201,3 +171,207 @@ describe('CampaignApplicationService', () => { }) }) }) + +// import { Test, TestingModule } from '@nestjs/testing' +// import { CampaignApplicationService } from './campaign-application.service' +// import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' +// import { BadRequestException, HttpStatus } from '@nestjs/common' +// import { +// CampaignApplicationState, +// CampaignState, +// CampaignTypeCategory, +// Currency, +// } from '@prisma/client' +// import { CreateCampaignDto } from '../campaign/dto/create-campaign.dto' +// import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock' +// import { CampaignService } from '../campaign/campaign.service' +// import { NotificationService } from '../sockets/notifications/notification.service' +// import { VaultService } from '../vault/vault.service' +// import { ConfigService } from '@nestjs/config' +// import { MarketingNotificationsService } from '../notifications/notifications.service' +// import { PersonService } from '../person/person.service' +// import { EmailService } from '../email/email.service' +// import { NotificationsProviderInterface } from '../notifications/providers/notifications.interface.providers' +// import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider' +// import { NotificationGateway } from '../sockets/notifications/gateway' +// import { TemplateService } from '../email/template.service' +// describe('CampaignApplicationService', () => { +// let service: CampaignApplicationService + +// beforeEach(async () => { +// const module: TestingModule = await Test.createTestingModule({ +// providers: [ +// { +// // Use the interface as token +// provide: NotificationsProviderInterface, +// // But actually provide the service that implements the interface +// useClass: SendGridNotificationsProvider, +// }, +// CampaignService, +// MockPrismaService, +// NotificationService, +// VaultService, +// MarketingNotificationsService, +// ConfigService, +// PersonService, +// EmailService, +// NotificationGateway, +// TemplateService, +// CampaignApplicationService, +// ], +// }) +// .overrideProvider(EmailService) +// .useValue({ +// sendFromTemplate: jest.fn(() => { +// return true +// }), +// }) +// .compile() + +// service = module.get(CampaignApplicationService) +// }) + +// it('should be defined', () => { +// expect(service).toBeDefined() +// }) + +// describe('createNewApplication', () => { +// const baseDto = { +// campaignName: 'Test Campaign', +// organizerName: 'Test Organizer', +// organizerEmail: 'organizer@example.com', +// organizerPhone: '123456789', +// beneficiary: 'Test Beneficiary', +// organizerBeneficiaryRel: 'Test Relation', +// goal: 'Test Goal', +// amount: '1000', +// toEntity: jest.fn(), +// } +// it('should throw an error if acceptTermsAndConditions are not accepted', () => { +// const dto: CreateCampaignApplicationDto = { +// ...baseDto, +// acceptTermsAndConditions: false, +// transparencyTermsAccepted: true, +// personalInformationProcessingAccepted: true, +// } + +// expect(() => service.create(dto)).toThrow( +// new BadRequestException('All agreements must be checked'), +// ) +// }) + +// it('should throw an error if transparencyTermsAccepted are not accepted', () => { +// const dto: CreateCampaignApplicationDto = { +// ...baseDto, +// acceptTermsAndConditions: true, +// transparencyTermsAccepted: false, +// personalInformationProcessingAccepted: true, +// } + +// expect(() => service.create(dto)).toThrow( +// new BadRequestException('All agreements must be checked'), +// ) +// }) + +// it('should throw an error if personalInformationProcessingAccepted is not accepted', () => { +// const dto: CreateCampaignApplicationDto = { +// ...baseDto, +// acceptTermsAndConditions: true, +// transparencyTermsAccepted: true, +// personalInformationProcessingAccepted: false, +// } + +// expect(() => service.create(dto)).toThrow( +// new BadRequestException('All agreements must be checked'), +// ) +// }) + +// it('should add a new campaign application if all agreements are accepted', () => { +// const dto: CreateCampaignApplicationDto = { +// ...baseDto, +// acceptTermsAndConditions: true, +// transparencyTermsAccepted: true, +// personalInformationProcessingAccepted: true, +// } + +// expect(service.create(dto)).toBe('This action adds a new campaignApplication') +// }) +// }) + +// describe('find all(GET) campains', () => { +// it('should return an array of campaign applications', async () => { +// const mockCampaigns = [ +// { +// id: 'testId', +// createdAt: new Date('2022-04-08T06:36:33.661Z'), +// updatedAt: new Date('2022-04-08T06:36:33.662Z'), +// description: 'Test description', +// organizerId: 'testOrganizerId1', +// organizerName: 'Test Organizer1', +// organizerEmail: 'organizer@example.com', +// beneficiary: 'test beneficary', +// organizerPhone: '123456789', +// organizerBeneficiaryRel: 'Test Relation', +// campaignName: 'Test Campaign', +// goal: 'Test Goal', +// history: 'test history', +// amount: '1000', +// campaignGuarantee: 'test campaignGuarantee', +// otherFinanceSources: 'test otherFinanceSources', +// otherNotes: 'test otherNotes', +// state: CampaignApplicationState.review, +// category: CampaignTypeCategory.medical, +// ticketURL: 'testsodifhso', +// archived: false, +// }, +// { +// id: 'testId', +// createdAt: new Date('2022-04-08T06:36:33.661Z'), +// updatedAt: new Date('2022-04-08T06:36:33.662Z'), +// description: 'Test description', +// organizerId: 'testOrganizerId1', +// organizerName: 'Test Organizer1', +// organizerEmail: 'organizer@example.com', +// beneficiary: 'test beneficary', +// organizerPhone: '123456789', +// organizerBeneficiaryRel: 'Test Relation', +// campaignName: 'Test Campaign', +// goal: 'Test Goal', +// history: 'test history', +// amount: '1000', +// campaignGuarantee: 'test campaignGuarantee', +// otherFinanceSources: 'test otherFinanceSources', +// otherNotes: 'test otherNotes', +// state: CampaignApplicationState.review, +// category: CampaignTypeCategory.medical, +// ticketURL: 'testsodifhso', +// archived: false, +// }, +// ] + +// prismaMock.campaignApplication.findMany.mockResolvedValue(mockCampaigns) + +// const result = await service.findAll() + +// expect(result).toEqual(mockCampaigns) +// expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) +// }) + +// it('should return an empty array if no campaigns are found', async () => { +// prismaMock.campaignApplication.findMany.mockResolvedValue([]) + +// const result = await service.findAll() + +// expect(result).toEqual([]) +// expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) +// }) + +// it('should handle errors and throw an exception', async () => { +// const errorMessage = 'Database error' +// prismaMock.campaignApplication.findMany.mockRejectedValue(new Error(errorMessage)) + +// await expect(service.findAll()).rejects.toThrow(errorMessage) +// expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) +// }) +// }) +// }) From e5488d3f0c26211674eba7f578fb57aefc7ae3b6 Mon Sep 17 00:00:00 2001 From: Martbul Date: Sat, 29 Jun 2024 15:13:13 +0300 Subject: [PATCH 12/12] remove commented code --- .../campaign-application.service.spec.ts | 206 +----------------- 1 file changed, 1 insertion(+), 205 deletions(-) diff --git a/apps/api/src/campaign-application/campaign-application.service.spec.ts b/apps/api/src/campaign-application/campaign-application.service.spec.ts index 32f07ffc..11948680 100644 --- a/apps/api/src/campaign-application/campaign-application.service.spec.ts +++ b/apps/api/src/campaign-application/campaign-application.service.spec.ts @@ -170,208 +170,4 @@ describe('CampaignApplicationService', () => { expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) }) }) -}) - -// import { Test, TestingModule } from '@nestjs/testing' -// import { CampaignApplicationService } from './campaign-application.service' -// import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' -// import { BadRequestException, HttpStatus } from '@nestjs/common' -// import { -// CampaignApplicationState, -// CampaignState, -// CampaignTypeCategory, -// Currency, -// } from '@prisma/client' -// import { CreateCampaignDto } from '../campaign/dto/create-campaign.dto' -// import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock' -// import { CampaignService } from '../campaign/campaign.service' -// import { NotificationService } from '../sockets/notifications/notification.service' -// import { VaultService } from '../vault/vault.service' -// import { ConfigService } from '@nestjs/config' -// import { MarketingNotificationsService } from '../notifications/notifications.service' -// import { PersonService } from '../person/person.service' -// import { EmailService } from '../email/email.service' -// import { NotificationsProviderInterface } from '../notifications/providers/notifications.interface.providers' -// import { SendGridNotificationsProvider } from '../notifications/providers/notifications.sendgrid.provider' -// import { NotificationGateway } from '../sockets/notifications/gateway' -// import { TemplateService } from '../email/template.service' -// describe('CampaignApplicationService', () => { -// let service: CampaignApplicationService - -// beforeEach(async () => { -// const module: TestingModule = await Test.createTestingModule({ -// providers: [ -// { -// // Use the interface as token -// provide: NotificationsProviderInterface, -// // But actually provide the service that implements the interface -// useClass: SendGridNotificationsProvider, -// }, -// CampaignService, -// MockPrismaService, -// NotificationService, -// VaultService, -// MarketingNotificationsService, -// ConfigService, -// PersonService, -// EmailService, -// NotificationGateway, -// TemplateService, -// CampaignApplicationService, -// ], -// }) -// .overrideProvider(EmailService) -// .useValue({ -// sendFromTemplate: jest.fn(() => { -// return true -// }), -// }) -// .compile() - -// service = module.get(CampaignApplicationService) -// }) - -// it('should be defined', () => { -// expect(service).toBeDefined() -// }) - -// describe('createNewApplication', () => { -// const baseDto = { -// campaignName: 'Test Campaign', -// organizerName: 'Test Organizer', -// organizerEmail: 'organizer@example.com', -// organizerPhone: '123456789', -// beneficiary: 'Test Beneficiary', -// organizerBeneficiaryRel: 'Test Relation', -// goal: 'Test Goal', -// amount: '1000', -// toEntity: jest.fn(), -// } -// it('should throw an error if acceptTermsAndConditions are not accepted', () => { -// const dto: CreateCampaignApplicationDto = { -// ...baseDto, -// acceptTermsAndConditions: false, -// transparencyTermsAccepted: true, -// personalInformationProcessingAccepted: true, -// } - -// expect(() => service.create(dto)).toThrow( -// new BadRequestException('All agreements must be checked'), -// ) -// }) - -// it('should throw an error if transparencyTermsAccepted are not accepted', () => { -// const dto: CreateCampaignApplicationDto = { -// ...baseDto, -// acceptTermsAndConditions: true, -// transparencyTermsAccepted: false, -// personalInformationProcessingAccepted: true, -// } - -// expect(() => service.create(dto)).toThrow( -// new BadRequestException('All agreements must be checked'), -// ) -// }) - -// it('should throw an error if personalInformationProcessingAccepted is not accepted', () => { -// const dto: CreateCampaignApplicationDto = { -// ...baseDto, -// acceptTermsAndConditions: true, -// transparencyTermsAccepted: true, -// personalInformationProcessingAccepted: false, -// } - -// expect(() => service.create(dto)).toThrow( -// new BadRequestException('All agreements must be checked'), -// ) -// }) - -// it('should add a new campaign application if all agreements are accepted', () => { -// const dto: CreateCampaignApplicationDto = { -// ...baseDto, -// acceptTermsAndConditions: true, -// transparencyTermsAccepted: true, -// personalInformationProcessingAccepted: true, -// } - -// expect(service.create(dto)).toBe('This action adds a new campaignApplication') -// }) -// }) - -// describe('find all(GET) campains', () => { -// it('should return an array of campaign applications', async () => { -// const mockCampaigns = [ -// { -// id: 'testId', -// createdAt: new Date('2022-04-08T06:36:33.661Z'), -// updatedAt: new Date('2022-04-08T06:36:33.662Z'), -// description: 'Test description', -// organizerId: 'testOrganizerId1', -// organizerName: 'Test Organizer1', -// organizerEmail: 'organizer@example.com', -// beneficiary: 'test beneficary', -// organizerPhone: '123456789', -// organizerBeneficiaryRel: 'Test Relation', -// campaignName: 'Test Campaign', -// goal: 'Test Goal', -// history: 'test history', -// amount: '1000', -// campaignGuarantee: 'test campaignGuarantee', -// otherFinanceSources: 'test otherFinanceSources', -// otherNotes: 'test otherNotes', -// state: CampaignApplicationState.review, -// category: CampaignTypeCategory.medical, -// ticketURL: 'testsodifhso', -// archived: false, -// }, -// { -// id: 'testId', -// createdAt: new Date('2022-04-08T06:36:33.661Z'), -// updatedAt: new Date('2022-04-08T06:36:33.662Z'), -// description: 'Test description', -// organizerId: 'testOrganizerId1', -// organizerName: 'Test Organizer1', -// organizerEmail: 'organizer@example.com', -// beneficiary: 'test beneficary', -// organizerPhone: '123456789', -// organizerBeneficiaryRel: 'Test Relation', -// campaignName: 'Test Campaign', -// goal: 'Test Goal', -// history: 'test history', -// amount: '1000', -// campaignGuarantee: 'test campaignGuarantee', -// otherFinanceSources: 'test otherFinanceSources', -// otherNotes: 'test otherNotes', -// state: CampaignApplicationState.review, -// category: CampaignTypeCategory.medical, -// ticketURL: 'testsodifhso', -// archived: false, -// }, -// ] - -// prismaMock.campaignApplication.findMany.mockResolvedValue(mockCampaigns) - -// const result = await service.findAll() - -// expect(result).toEqual(mockCampaigns) -// expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) -// }) - -// it('should return an empty array if no campaigns are found', async () => { -// prismaMock.campaignApplication.findMany.mockResolvedValue([]) - -// const result = await service.findAll() - -// expect(result).toEqual([]) -// expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) -// }) - -// it('should handle errors and throw an exception', async () => { -// const errorMessage = 'Database error' -// prismaMock.campaignApplication.findMany.mockRejectedValue(new Error(errorMessage)) - -// await expect(service.findAll()).rejects.toThrow(errorMessage) -// expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1) -// }) -// }) -// }) +}) \ No newline at end of file