Skip to content

Commit

Permalink
Edit campaing application task(Admin and Organaizer) (#660)
Browse files Browse the repository at this point in the history
* working on edit

* add edit campaignApplication

* admin and orgganizer update campaign application

* update max file upload and change file upload on update campaignApplication

* fix file upload

add new endpoint for uploading files to campaignApplication
remove upload functionality on create and on update campaignApplication
  • Loading branch information
Martbul committed Aug 7, 2024
1 parent ff951b3 commit 2e68561
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 91 deletions.
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: 'testemail@gmail.com',
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: 'testemail@gmail.com',
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: 'testemail@gmail.com',
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

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

0 comments on commit 2e68561

Please sign in to comment.