Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add findAll(GET) method in the campaign-application.service(second try) #652

Merged
merged 14 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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],
})
Expand Down
128 changes: 124 additions & 4 deletions apps/api/src/campaign-application/campaign-application.service.spec.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove commented out code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I have missed it.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,57 @@ 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: [CampaignApplicationService],
}).compile()
providers: [
{
// Use the interface as token
provide: NotificationsProviderInterface,
// But actually provide the service that implements the interface
useClass: SendGridNotificationsProvider,
},
CampaignService,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all these providers? CampaignService? Vault Service.... and all the rest?
It looks like CampaignApplicationService is the only requirement for this PR's scope

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind that all of these would need to be instantiated with all of their dependencies for each test!!!

Instead use mocking (see other tests or look for autoSpy usage)

MockPrismaService,
NotificationService,
VaultService,
MarketingNotificationsService,
ConfigService,
PersonService,
EmailService,
NotificationGateway,
TemplateService,
CampaignApplicationService,
],
})
.overrideProvider(EmailService)
.useValue({
sendFromTemplate: jest.fn(() => {
return true
}),
})
.compile()

service = module.get<CampaignApplicationService>(CampaignApplicationService)
})
Expand All @@ -28,7 +71,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 = {
Expand Down Expand Up @@ -80,4 +123,81 @@ 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 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: '[email protected]',
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: '[email protected]',
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)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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<UpdateCampaignApplicationDto> {
throw new Error('Method not implemented.')
}
Expand All @@ -20,7 +22,7 @@ export class CampaignApplicationService {
}

findAll() {
return `This action returns all campaignApplication`
return this.prisma.campaignApplication.findMany()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The controller would need to be updated to check for authenticated user and allow getting only for admin/operator roles.

}

findOne(id: string) {
Expand Down
Loading