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

Edit campaing application task(Admin and Organaizer) #660

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -77,3 +77,57 @@ export const mockCreatedCampaignApplication = {
ticketURL: null,
archived: false,
}

export const mockUpdateCampaignApplication = {
campaignName: 'Test Campaign',
organizerName: 'Test Organizer',
organizerEmail: '[email protected]',
organizerPhone: '123456789',
beneficiary: 'Test beneficary',
organizerBeneficiaryRel: 'Test organizerBeneficiaryRel',
goal: 'Test goal',
history: 'Test history',
amount: '1000',
description: 'Test description',
campaignGuarantee: 'Test guarantee',
otherFinanceSources: 'Test otherFinanceSources',
otherNotes: 'Test otherNotes',
category: CampaignTypeCategory.medical,
}

const mockUpdateCampaignApplicationResponceOrganizer = {
amount: '1000',
beneficiary: 'Test beneficary',
campaignGuarantee: 'Test guarantee',
campaignName: 'Test Campaign',
category: 'medical',
description: 'Test description',
goal: 'Test goal',
history: 'Test history',
organizerBeneficiaryRel: 'Test organizerBeneficiaryRel',
organizerEmail: '[email protected]',
organizerName: 'Test Organizer',
organizerPhone: '123456789',
otherFinanceSources: 'Test otherFinanceSources',
otherNotes: 'Test otherNotes',
}

const mockUpdateCampaignApplicationResponceAdmin = {
amount: '1000',
beneficiary: 'Test beneficary',
campaignGuarantee: 'Test guarantee',
campaignName: 'Test Campaign',
category: 'medical',
description: 'Test description',
goal: 'Test goal',
history: 'Test history',
organizerBeneficiaryRel: 'Test organizerBeneficiaryRel',
organizerEmail: '[email protected]',
organizerName: 'Test Organizer',
organizerPhone: '123456789',
otherFinanceSources: 'Test otherFinanceSources',
otherNotes: 'Test otherNotes',
archived: false,
state: 'active',
ticketURL: 'http://test.com/ticket',
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ import { KeycloakTokenParsed } from '../auth/keycloak'
import { ForbiddenException, NotFoundException } from '@nestjs/common'
import { PersonService } from '../person/person.service'
import { mockUser, mockUserAdmin } from './../auth/__mocks__'
import { mockNewCampaignApplication } from './__mocks__/campaign-application-mocks'
import {
mockNewCampaignApplication,
mockUpdateCampaignApplication,
} from './__mocks__/campaign-application-mocks'
import { mockCampaignApplicationFilesFn } from './__mocks__/campaing-application-file-mocks'
import { personMock } from '../person/__mock__/personMock'

describe('CampaignApplicationController', () => {
let controller: CampaignApplicationController
let service: CampaignApplicationService
let personService: PersonService

const mockPerson = {
...personMock,
company: null,
beneficiaries: [],
organizer: { id: 'personOrganaizerId' },
}

const mockCreateNewCampaignApplication = {
...mockNewCampaignApplication,
acceptTermsAndConditions: true,
Expand Down Expand Up @@ -46,31 +57,45 @@ describe('CampaignApplicationController', () => {
// Arrange
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(mockUser)

// Act
await controller.create(mockCreateNewCampaignApplication, mockUser)

// Assert
expect(service.create).toHaveBeenCalledWith(mockCreateNewCampaignApplication, mockUser)
})

it('when create called with wrong user it should throw NotFoundException', async () => {
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(null)

// Act & Assert
await expect(controller.create(mockCreateNewCampaignApplication, mockUser)).rejects.toThrow(
NotFoundException,
)
})

it('when uploadFile/:id called it should delegate to the service uploadFiles', async () => {
// Arrange
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(mockUser)
const mockCampaignApplicationFiles = mockCampaignApplicationFilesFn()

// Act
await controller.create(
mockCampaignApplicationFiles,
mockCreateNewCampaignApplication,
mockUser,
)
await controller.uploadFiles(mockCampaignApplicationFiles, 'newCampaignApplicationId', mockUser)

// Assert
expect(service.create).toHaveBeenCalledWith(
mockCreateNewCampaignApplication,
expect(service.uploadFiles).toHaveBeenCalledWith(
'newCampaignApplicationId',
mockUser,
mockCampaignApplicationFiles,
)
})

it('when create called with wrong user it should throw NotFoundException', async () => {
it('when uploadFile/:id called with wrong user it should throw NotFoundException', async () => {
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(null)

const mockCampaignApplicationFiles = mockCampaignApplicationFilesFn()

// Act & Assert
await expect(
controller.create(mockCampaignApplicationFiles, mockCreateNewCampaignApplication, mockUser),
controller.uploadFiles(mockCampaignApplicationFiles, 'newCampaignApplicationId', mockUser),
).rejects.toThrow(NotFoundException)
})

Expand Down Expand Up @@ -106,11 +131,36 @@ describe('CampaignApplicationController', () => {
expect(service.findOne).toHaveBeenCalledWith('id')
})

it('when update called it should delegate to the service update', () => {
it('when update called by an user it should delegate to the service update', async () => {
// Arrange
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(mockPerson)

// Act
controller.update('1', {}, { sub: 'test', 'allowed-origins': ['test'] })
await controller.update('campaignApplicationId', mockUpdateCampaignApplication, mockUser)

// Assert
expect(service.update).toHaveBeenCalledWith('1', {})
expect(service.updateCampaignApplication).toHaveBeenCalledWith(
'campaignApplicationId',
mockUpdateCampaignApplication,
false,
'personOrganaizerId',
)
})

it('when update called by an admin it should delegate to the service update with isAdminFlag true', async () => {
// Arrange
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(mockPerson)
jest.spyOn(service, 'updateCampaignApplication').mockImplementation(async () => {})

// Act
await controller.update('campaignApplicationId', mockUpdateCampaignApplication, mockUserAdmin)

// Assert
expect(service.updateCampaignApplication).toHaveBeenCalledWith(
'campaignApplicationId',
mockUpdateCampaignApplication,
true,
'ADMIN',
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export class CampaignApplicationController {
) {}

@Post('create')
async create(
@Body() createCampaignApplicationDto: CreateCampaignApplicationDto,
@AuthenticatedUser() user: KeycloakTokenParsed,
) {
const person = await this.personService.findOneByKeycloakId(user.sub)
if (!person) {
Logger.error('No person found in database')
throw new NotFoundException('No person found in database')
}

return this.campaignApplicationService.create(createCampaignApplicationDto, person)
}

@Post('uploadFile/:id')
@UseInterceptors(
FilesInterceptor('file', 10, {
limits: { fileSize: 1024 * 1024 * 30 },
Expand All @@ -39,9 +53,9 @@ export class CampaignApplicationController {
},
}),
)
async create(
async uploadFiles(
@UploadedFiles() files: Express.Multer.File[],
@Body() createCampaignApplicationDto: CreateCampaignApplicationDto,
@Param('id') id: string,
@AuthenticatedUser() user: KeycloakTokenParsed,
) {
const person = await this.personService.findOneByKeycloakId(user.sub)
Expand All @@ -50,7 +64,7 @@ export class CampaignApplicationController {
throw new NotFoundException('No person found in database')
}

return this.campaignApplicationService.create(createCampaignApplicationDto, person, files)
return this.campaignApplicationService.uploadFiles(id, person, files)
}

@Get('list')
Expand All @@ -67,15 +81,33 @@ export class CampaignApplicationController {
}

@Patch(':id')
@Roles({
roles: [RealmViewSupporters.role, ViewSupporters.role],
mode: RoleMatchingMode.ANY,
})
async update(
@Param('id') id: string,
@Body() updateCampaignApplicationDto: UpdateCampaignApplicationDto,
@AuthenticatedUser() user: KeycloakTokenParsed,
) {
return this.campaignApplicationService.update(id, updateCampaignApplicationDto)
const person = await this.personService.findOneByKeycloakId(user.sub)
if (!person) throw new NotFoundException('User is not found')

let isAdminFlag
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need this defined before the ifs since you are not using it after that.
Try to define variables in the scope they are used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

using person.id

Copy link
Contributor

Choose a reason for hiding this comment

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

I was referring to the isAdminFlag, but it is not a big deal.


if (isAdmin(user)) {
isAdminFlag = true
return this.campaignApplicationService.updateCampaignApplication(
id,
updateCampaignApplicationDto,
isAdminFlag,
'ADMIN',
)
} else {
if (!person.organizer) throw new NotFoundException('User has no campaigns')
isAdminFlag = false
return this.campaignApplicationService.updateCampaignApplication(
id,
updateCampaignApplicationDto,
isAdminFlag,
person.organizer.id,
)
}
}
}
Loading
Loading