-
-
Notifications
You must be signed in to change notification settings - Fork 573
feat: add option to Decline request with a Reason #4205
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
Open
samohtxotom
wants to merge
2
commits into
sct:develop
Choose a base branch
from
samohtxotom:decline-reasons
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,141
−77
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { | ||
| Column, | ||
| CreateDateColumn, | ||
| Entity, | ||
| PrimaryGeneratedColumn, | ||
| UpdateDateColumn, | ||
| } from 'typeorm'; | ||
|
|
||
| @Entity() | ||
| export class DeclineReason { | ||
| @PrimaryGeneratedColumn() | ||
| public id: number; | ||
|
|
||
| @Column({ type: 'text' }) | ||
| public reason: string; | ||
|
|
||
| @CreateDateColumn() | ||
| public createdAt: Date; | ||
|
|
||
| @UpdateDateColumn() | ||
| public updatedAt: Date; | ||
|
|
||
| constructor(init?: Partial<DeclineReason>) { | ||
| Object.assign(this, init); | ||
| } | ||
| } | ||
|
|
||
| export default DeclineReason; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
server/migration/1740717744279-AddDeclineReasonToMediaRequest.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class AddDeclineReasonToMediaRequest1740717744279 | ||
| implements MigrationInterface | ||
| { | ||
| name = 'AddDeclineReasonToMediaRequest1740717744279'; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "media_request" ADD "declineReason" text` | ||
| ); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `ALTER TABLE "media_request" DROP COLUMN "declineReason"` | ||
| ); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
server/migration/1740717744280-CreateDeclineReasonTable.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
|
||
| export class CreateDeclineReasonTable1740717744280 | ||
| implements MigrationInterface | ||
| { | ||
| name = 'CreateDeclineReasonTable1740717744280'; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query( | ||
| `CREATE TABLE "decline_reason" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "reason" text NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')))` | ||
| ); | ||
|
|
||
| // Insert default decline reasons | ||
| await queryRunner.query(` | ||
| INSERT INTO "decline_reason" ("reason") VALUES | ||
| ('Inappropriate content'), | ||
| ('Low quality content'), | ||
| ('Not available - too niche'), | ||
| ('Please request only a few seasons at a time'), | ||
| ('Available on YouTube'), | ||
| ('No reality TV sorry') | ||
| `); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`DROP TABLE "decline_reason"`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { getRepository } from '@server/datasource'; | ||
| import DeclineReason from '@server/entity/DeclineReason'; | ||
| import { Permission } from '@server/lib/permissions'; | ||
| import logger from '@server/logger'; | ||
| import { isAuthenticated } from '@server/middleware/auth'; | ||
| import { Router } from 'express'; | ||
|
|
||
| const declineReasonsRoutes = Router(); | ||
|
|
||
| // Get all custom decline reasons | ||
| declineReasonsRoutes.get('/', async (_req, res, next) => { | ||
| try { | ||
| const declineReasonRepository = getRepository(DeclineReason); | ||
| const reasons = await declineReasonRepository.find({ | ||
| order: { createdAt: 'ASC' }, | ||
| }); | ||
|
|
||
| return res.status(200).json(reasons); | ||
| } catch (e) { | ||
| logger.error('Something went wrong retrieving decline reasons', { | ||
| label: 'API', | ||
| errorMessage: e.message, | ||
| }); | ||
| next({ status: 500, message: 'Unable to retrieve decline reasons.' }); | ||
| } | ||
| }); | ||
|
|
||
| // Create a new custom decline reason | ||
| declineReasonsRoutes.post<never, DeclineReason, { reason: string }>( | ||
| '/', | ||
| isAuthenticated(Permission.ADMIN), | ||
| async (req, res, next) => { | ||
| try { | ||
| const { reason } = req.body; | ||
|
|
||
| if (!reason || !reason.trim()) { | ||
| return next({ status: 400, message: 'Reason is required.' }); | ||
| } | ||
|
|
||
| const declineReasonRepository = getRepository(DeclineReason); | ||
|
|
||
| // Check if reason already exists | ||
| const existingReason = await declineReasonRepository.findOne({ | ||
| where: { reason: reason.trim() }, | ||
| }); | ||
|
|
||
| if (existingReason) { | ||
| return next({ | ||
| status: 409, | ||
| message: 'This decline reason already exists.', | ||
| }); | ||
| } | ||
|
|
||
| const newReason = new DeclineReason({ | ||
| reason: reason.trim(), | ||
| }); | ||
|
|
||
| await declineReasonRepository.save(newReason); | ||
|
|
||
| return res.status(201).json(newReason); | ||
| } catch (e) { | ||
| logger.error('Something went wrong creating decline reason', { | ||
| label: 'API', | ||
| errorMessage: e.message, | ||
| }); | ||
| next({ status: 500, message: 'Unable to create decline reason.' }); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| // Delete a custom decline reason | ||
| declineReasonsRoutes.delete<{ reasonId: string }>( | ||
| '/:reasonId', | ||
| isAuthenticated(Permission.ADMIN), | ||
| async (req, res, next) => { | ||
| try { | ||
| const declineReasonRepository = getRepository(DeclineReason); | ||
| const reasonId = Number(req.params.reasonId); | ||
|
|
||
| const reason = await declineReasonRepository.findOne({ | ||
| where: { id: reasonId }, | ||
| }); | ||
|
|
||
| if (!reason) { | ||
| return next({ status: 404, message: 'Decline reason not found.' }); | ||
| } | ||
|
|
||
| await declineReasonRepository.remove(reason); | ||
|
|
||
| return res.status(204).send(); | ||
| } catch (e) { | ||
| logger.error('Something went wrong deleting decline reason', { | ||
| label: 'API', | ||
| errorMessage: e.message, | ||
| }); | ||
| next({ status: 500, message: 'Unable to delete decline reason.' }); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| export default declineReasonsRoutes; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need this Entity? Can we not just add this directly to the MediaRequest entity