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

fix: campaign type and add files #670

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
@@ -1,5 +1,4 @@
import { CampaignApplicationState, CampaignTypeCategory } from '@prisma/client'
import { CreateCampaignApplicationDto } from '../dto/create-campaign-application.dto'
import { CampaignApplicationState } from '@prisma/client'

export const mockNewCampaignApplication = {
campaignName: 'Test Campaign',
Expand All @@ -15,7 +14,9 @@ export const mockNewCampaignApplication = {
campaignGuarantee: 'Test guarantee',
otherFinanceSources: 'Test otherFinanceSources',
otherNotes: 'Test otherNotes',
category: CampaignTypeCategory.medical,
campaignTypeId: 'ffdbcc41-85ec-0000-9e59-0662f3b433af',
campaignEnd: 'funds',
campaignEndDate: '2024-02-02',
}

export const mockSingleCampaignApplication = {
Expand All @@ -37,10 +38,12 @@ export const mockSingleCampaignApplication = {
otherFinanceSources: 'test otherFinanceSources1',
otherNotes: 'test otherNotes1',
state: CampaignApplicationState.review,
category: CampaignTypeCategory.medical,
campaignTypeId: 'ffdbcc41-85ec-0000-9e59-0662f3b433af',
ticketURL: 'testsodifhso1',
archived: false,
documents: [{ id: 'fileId' }],
campaignEnd: 'funds',
campaignEndDate: undefined,
}

export const mockCampaigns = [
Expand All @@ -63,7 +66,7 @@ export const mockCampaigns = [
otherFinanceSources: 'test otherFinanceSources1',
otherNotes: 'test otherNotes1',
state: CampaignApplicationState.review,
category: CampaignTypeCategory.medical,
campaignTypeId: 'ffdbcc41-85ec-0000-9e59-0662f3b433af',
ticketURL: 'testsodifhso1',
archived: false,
},
Expand All @@ -86,7 +89,7 @@ export const mockCampaigns = [
otherFinanceSources: 'test otherFinanceSources2',
otherNotes: 'test otherNotes2',
state: CampaignApplicationState.review,
category: CampaignTypeCategory.medical,
campaignTypeId: 'ffdbcc41-85ec-0000-9e59-0662f3b433af',
ticketURL: 'testsodifhso2',
archived: false,
},
Expand Down Expand Up @@ -117,42 +120,7 @@ export const mockUpdateCampaignApplication = {
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',
campaignTypeId: 'ffdbcc41-85ec-0000-9e59-0662f3b433af',
campaignEnd: 'funds',
campaignEndDate: '2024-09-09',
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Controller,
Delete,
ForbiddenException,
NotFoundException,
Get,
Logger,
NotFoundException,
Param,
Patch,
Post,
UploadedFiles,
UseInterceptors,
Delete,
} 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'
import { FilesInterceptor } from '@nestjs/platform-express'
import { ApiTags } from '@nestjs/swagger'
import { AuthenticatedUser, RoleMatchingMode, Roles } from 'nest-keycloak-connect'
import { RealmViewSupporters, ViewSupporters } from '@podkrepi-bg/podkrepi-types'
import { AuthenticatedUser } from 'nest-keycloak-connect'
import { KeycloakTokenParsed, isAdmin } from '../auth/keycloak'
import { PersonService } from '../person/person.service'
import { FilesInterceptor } from '@nestjs/platform-express'
import { validateFileType } from '../common/files'
import { PersonService } from '../person/person.service'
import { CampaignApplicationService } from './campaign-application.service'
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto'
import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto'

@ApiTags('campaign-application')
@Controller('campaign-application')
Expand Down Expand Up @@ -85,7 +84,7 @@ export class CampaignApplicationController {
}

const isAdminFlag = isAdmin(user)

return this.campaignApplicationService.findOne(id, isAdminFlag, person)
}

Expand All @@ -98,7 +97,7 @@ export class CampaignApplicationController {
}

const isAdminFlag = isAdmin(user)

return this.campaignApplicationService.deleteFile(id, isAdminFlag, person)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing'
import { CampaignApplicationService } from './campaign-application.service'
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto'
import { BadRequestException, ForbiddenException, NotFoundException } from '@nestjs/common'
import { CampaignApplicationFileRole, CampaignTypeCategory, Person } from '@prisma/client'
import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock'
import { Test, TestingModule } from '@nestjs/testing'
import { OrganizerService } from '../organizer/organizer.service'
import { personMock } from '../person/__mock__/personMock'
import { MockPrismaService, prismaMock } from '../prisma/prisma-client.mock'
import { S3Service } from '../s3/s3.service'
import {
mockCampaigns,
mockCreatedCampaignApplication,
mockNewCampaignApplication,
mockSingleCampaignApplication,
mockUpdateCampaignApplication,
} from './__mocks__/campaign-application-mocks'
import { S3Service } from '../s3/s3.service'
import {
mockCampaignApplicationFileFn,
mockCampaignApplicationFilesFn,
mockCampaignApplicationUploadFileFn,
} from './__mocks__/campaing-application-file-mocks'
import { CampaignApplicationService } from './campaign-application.service'
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto'

describe('CampaignApplicationService', () => {
let service: CampaignApplicationService
Expand Down Expand Up @@ -108,6 +106,7 @@ describe('CampaignApplicationService', () => {
transparencyTermsAccepted: true,
personalInformationProcessingAccepted: true,
toEntity: new CreateCampaignApplicationDto().toEntity,
campaignEndDate: '2024-01-01'
}

const mockOrganizerId = 'mockOrganizerId'
Expand Down Expand Up @@ -143,8 +142,10 @@ describe('CampaignApplicationService', () => {
campaignGuarantee: 'Test guarantee',
otherFinanceSources: 'Test otherFinanceSources',
otherNotes: 'Test otherNotes',
category: CampaignTypeCategory.medical,
campaignTypeId: 'ffdbcc41-85ec-0000-9e59-0662f3b433af',
organizerId: mockOrganizerId,
campaignEnd: 'funds',
campaignEndDate: new Date('2024-01-01T00:00:00.000Z'),
},
})

Expand Down Expand Up @@ -283,6 +284,7 @@ describe('CampaignApplicationService', () => {
where: { id: '1' },
data: {
...mockUpdateCampaignApplication,
campaignEndDate: new Date('2024-09-09T00:00:00.000Z')
},
})
})
Expand Down Expand Up @@ -338,6 +340,7 @@ describe('CampaignApplicationService', () => {
where: { id: '1' },
data: {
...mockUpdateCampaignApplication,
campaignEndDate: new Date('2024-09-09T00:00:00.000Z')
},
})
})
Expand Down
45 changes: 36 additions & 9 deletions apps/api/src/campaign-application/campaign-application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import { OrganizerService } from '../organizer/organizer.service'
import { CampaignApplicationFileRole, Person, Prisma } from '@prisma/client'
import { S3Service } from './../s3/s3.service'
import { CreateCampaignApplicationFileDto } from './dto/create-campaignApplication-file.dto'

function dateMaybe (d?: string) {
return d != null &&
typeof d === 'string' &&
new Date(d).toString() != new Date('----invalid date ---').toString()
? new Date(d)
: undefined
}

@Injectable()
export class CampaignApplicationService {
private readonly bucketName: string = 'campaignapplication-files'
Expand All @@ -21,10 +30,6 @@ export class CampaignApplicationService {
private s3: S3Service,
) {}

async getCampaignByIdWithPersonIds(id: string): Promise<UpdateCampaignApplicationDto> {
throw new Error('Method not implemented.')
}

async create(createCampaignApplicationDto: CreateCampaignApplicationDto, person: Person) {
try {
if (
Expand Down Expand Up @@ -59,8 +64,10 @@ export class CampaignApplicationService {
campaignGuarantee: createCampaignApplicationDto.campaignGuarantee,
otherFinanceSources: createCampaignApplicationDto.otherFinanceSources,
otherNotes: createCampaignApplicationDto.otherNotes,
category: createCampaignApplicationDto.category,
campaignTypeId: createCampaignApplicationDto.campaignTypeId,
organizerId: organizer.id,
campaignEnd: createCampaignApplicationDto.campaignEnd,
campaignEndDate: dateMaybe(createCampaignApplicationDto.campaignEndDate)
}

const newCampaignApplication = await this.prisma.campaignApplication.create({
Expand Down Expand Up @@ -96,10 +103,22 @@ export class CampaignApplicationService {
}
}

async findOne(id: string, isAdminFlag: boolean, person: Prisma.PersonGetPayload<{ include: { organizer: {select:{id:true}}}}>) {
async findOne(
id: string,
isAdminFlag: boolean,
person: Prisma.PersonGetPayload<{ include: { organizer: { select: { id: true } } } }>,
) {
try {
const singleCampaignApplication = await this.prisma.campaignApplication.findUnique({
where: { id },
include: {
documents: {
select: {
id: true,
filename: true,
},
},
},
})
if (!singleCampaignApplication) {
throw new NotFoundException('Campaign application doesnt exist')
Expand All @@ -116,7 +135,11 @@ export class CampaignApplicationService {
}
}

async deleteFile(id: string, isAdminFlag: boolean, person: Prisma.PersonGetPayload<{ include: { organizer: {select:{id:true}}}}>) {
async deleteFile(
id: string,
isAdminFlag: boolean,
person: Prisma.PersonGetPayload<{ include: { organizer: { select: { id: true } } } }>,
) {
try {
const campaignApplication = await this.prisma.campaignApplication.findFirst({
where: {
Expand Down Expand Up @@ -184,7 +207,9 @@ export class CampaignApplicationService {
campaignGuarantee: updateCampaignApplicationDto?.campaignGuarantee,
otherFinanceSources: updateCampaignApplicationDto?.otherFinanceSources,
otherNotes: updateCampaignApplicationDto?.otherNotes,
category: updateCampaignApplicationDto?.category,
campaignTypeId: updateCampaignApplicationDto?.campaignTypeId,
campaignEnd: updateCampaignApplicationDto.campaignEnd,
campaignEndDate: dateMaybe(updateCampaignApplicationDto.campaignEndDate),
},
})

Expand All @@ -208,10 +233,12 @@ export class CampaignApplicationService {
campaignGuarantee: updateCampaignApplicationDto?.campaignGuarantee,
otherFinanceSources: updateCampaignApplicationDto?.otherFinanceSources,
otherNotes: updateCampaignApplicationDto?.otherNotes,
category: updateCampaignApplicationDto?.category,
campaignTypeId: updateCampaignApplicationDto?.campaignTypeId,
state: updateCampaignApplicationDto?.state,
ticketURL: updateCampaignApplicationDto?.ticketURL,
archived: updateCampaignApplicationDto?.archived,
campaignEnd: updateCampaignApplicationDto.campaignEnd,
campaignEndDate: dateMaybe(updateCampaignApplicationDto.campaignEndDate),
},
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
import { CampaignTypeCategory, Prisma } from '@prisma/client'
import { Prisma } from '@prisma/client'
import { Expose } from 'class-transformer'
import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator'
import { IsBoolean, IsDateString, IsNotEmpty, IsOptional, IsString } from 'class-validator'

@Expose()
export class CreateCampaignApplicationDto {
Expand Down Expand Up @@ -116,10 +116,23 @@ export class CreateCampaignApplicationDto {
@IsOptional()
otherNotes?: string

@ApiProperty({ enum: CampaignTypeCategory })
@ApiProperty()
@Expose()
@IsString()
@IsOptional()
campaignTypeId?: string

@ApiProperty()
@Expose()
@IsString()
@IsOptional()
campaignEnd?: string

@ApiProperty()
@Expose()
@IsDateString()
@IsOptional()
category?: CampaignTypeCategory
campaignEndDate?: string

public toEntity(): Prisma.CampaignApplicationCreateInput {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { CampaignTypeCategory } from '@prisma/client'
import { ApiProperty } from '@nestjs/swagger'

export class CreateCampaignApplicationDto {
organizerName: string
organizerEmail?: string
Expand All @@ -15,8 +12,9 @@ export class CreateCampaignApplicationDto {
campaignGuarantee?: string
otherFinanceSources?: string
otherNotes?: string
@ApiProperty({ enum: CampaignTypeCategory })
category?: CampaignTypeCategory
campaignTypeId?: string
ticketURL?: string
archived?: boolean
campaignEnd?: string
campaignEndDate?: Date
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { CampaignTypeCategory } from '@prisma/client'
import { ApiProperty } from '@nestjs/swagger'

export class UpdateCampaignApplicationDto {
organizerName?: string
organizerEmail?: string
Expand All @@ -15,8 +12,9 @@ export class UpdateCampaignApplicationDto {
campaignGuarantee?: string
otherFinanceSources?: string
otherNotes?: string
@ApiProperty({ enum: CampaignTypeCategory })
category?: CampaignTypeCategory
campaignTypeId?: string
ticketURL?: string
archived?: boolean
campaignEnd?: string
campaignEndDate?: Date
}
Loading
Loading