-
Notifications
You must be signed in to change notification settings - Fork 46
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 and findOne for admin only usage #661
Changes from 4 commits
6bb688d
91a8222
a7f135a
dfbcd45
a45c587
87f5e41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,12 +86,66 @@ export class CampaignApplicationService { | |
} | ||
} | ||
|
||
findAll() { | ||
return this.prisma.campaignApplication.findMany() | ||
async findAll() { | ||
try { | ||
const campaignApplications = await this.prisma.campaignApplication.findMany() | ||
return campaignApplications | ||
} catch (error) { | ||
Logger.error('Error in findAll():', error) | ||
throw error | ||
} | ||
} | ||
|
||
async findOne(id: string, isAdminFlag: boolean, person: Person) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit ugly but set the type of the person argument to be Prisma.PersonGetPayload<{ include: { organizer: {select:{id:true}}}}> instead of |
||
try { | ||
const singleCampaignApplication = await this.prisma.campaignApplication.findUnique({ | ||
where: { id }, | ||
}) | ||
if (!singleCampaignApplication) { | ||
throw new NotFoundException('Campaign application doesnt exist') | ||
} | ||
|
||
if (isAdminFlag === false && singleCampaignApplication.organizerId !== person.organizer.id) { | ||
throw new ForbiddenException('User is not admin or organizer of the campaignApplication') | ||
} | ||
|
||
return singleCampaignApplication | ||
} catch (error) { | ||
Logger.error('Error in findOne():', error) | ||
throw error | ||
} | ||
} | ||
|
||
findOne(id: string) { | ||
return `This action returns a #${id} campaignApplication` | ||
async deleteFile(id: string, isAdminFlag: boolean, person: Person) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above. |
||
try { | ||
const campaignApplication = await this.prisma.campaignApplication.findFirst({ | ||
where: { | ||
documents: { | ||
some: { | ||
id: id, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (!campaignApplication) { | ||
throw new NotFoundException('File does not exist') | ||
} | ||
|
||
if (isAdminFlag === false && campaignApplication.organizerId !== person.organizer.id) { | ||
throw new ForbiddenException('User is not admin or organizer of the campaignApplication') | ||
} | ||
|
||
await this.prisma.campaignApplicationFile.delete({ | ||
where: { id }, | ||
}) | ||
|
||
await this.s3.deleteObject(this.bucketName, id) | ||
} catch (error) { | ||
Logger.error('Error in deleteFile():', error) | ||
throw error | ||
} | ||
return 'Successfully deleted file' | ||
} | ||
|
||
async updateCampaignApplication( | ||
|
@@ -182,20 +236,25 @@ export class CampaignApplicationService { | |
role: CampaignApplicationFileRole.document, | ||
} | ||
|
||
const createFileInDb = await this.prisma.campaignApplicationFile.create({ | ||
data: fileDto, | ||
}) | ||
try { | ||
const createFileInDb = await this.prisma.campaignApplicationFile.create({ | ||
data: fileDto, | ||
}) | ||
|
||
await this.s3.uploadObject( | ||
this.bucketName, | ||
createFileInDb.id, | ||
file.originalname, | ||
file.mimetype, | ||
file.buffer, | ||
'CampaignApplicationFile', | ||
campaignApplicationId, | ||
personId, | ||
) | ||
return createFileInDb | ||
await this.s3.uploadObject( | ||
this.bucketName, | ||
createFileInDb.id, | ||
file.originalname, | ||
file.mimetype, | ||
file.buffer, | ||
'CampaignApplicationFile', | ||
campaignApplicationId, | ||
personId, | ||
) | ||
return createFileInDb | ||
} catch (error) { | ||
Logger.error('Error in campaignApplicationFilesCreate():', error) | ||
throw error | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the ES6 import
import {isAdmin} from
../auth/keycloak`, over the require() statement