Skip to content

Commit

Permalink
optional parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Szotkowski committed Sep 11, 2023
1 parent bbd97ae commit 84d000b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/controllers/instruction-step.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class InstructionStepController {
@repository(InstructionRepository)
public instructionRepository: InstructionRepository,
@repository(StepRepository) public stepRepository: StepRepository,
) {}
) { }

@authenticate('jwt')
@post('/users/{id}/instructions/{instructionId}/steps/{stepId}', {
Expand Down Expand Up @@ -416,6 +416,11 @@ export class InstructionStepController {
if (!instruction) {
throw new HttpErrors.NotFound('Instruction not found');
}
if (!instruction.premiumUserIds) {
throw new HttpErrors.Forbidden(
'You are not authorized to this instruction',
);
}
if (!instruction.premiumUserIds.includes(this.user.id)) {
throw new HttpErrors.Forbidden(
'You are not authorized to this instruction',
Expand Down
10 changes: 4 additions & 6 deletions src/controllers/user-instruction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class UserInstructionController {
@repository(InstructionRepository)
protected instructionRepository: InstructionRepository,
@repository(StepRepository) public stepRepository: StepRepository,
) {}
) { }

@authenticate('jwt')
@post('/users/{id}/instructions/{instructionId}', {
Expand Down Expand Up @@ -663,12 +663,10 @@ export class UserInstructionController {
if (!keyMatch) {
throw new HttpErrors.Unauthorized('Invalid password');
}
const instruction =
await this.instructionRepository.findById(instructionId);
const instruction = await this.instructionRepository.findById(instructionId);
instruction.premiumUserIds = instruction.premiumUserIds ?? [];
if (instruction.premiumUserIds.includes(userId)) {
instruction.premiumUserIds = instruction.premiumUserIds.filter(
id => id !== userId,
);
instruction.premiumUserIds = instruction.premiumUserIds.filter(id => id !== userId);
} else {
instruction.premiumUserIds.push(userId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/instruction.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class Instruction extends Entity {
},
default: () => [],
})
premiumUserIds: number[];
premiumUserIds?: number[];

@property({
type: 'number',
Expand Down

0 comments on commit 84d000b

Please sign in to comment.