Skip to content

Commit

Permalink
Merge pull request #33 from Selecro/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
Szotkowski authored Sep 22, 2023
2 parents 901f7ff + 7d89bc1 commit 835bb94
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 140 deletions.
28 changes: 14 additions & 14 deletions 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 All @@ -48,8 +48,8 @@ export class InstructionStepController {
},
},
})
async create(
@param.path.number('id') instructionId: number,
async createStep(
@param.path.number('instructionId') instructionId: number,
@requestBody({
content: {
'application/json': {
Expand All @@ -76,7 +76,7 @@ export class InstructionStepController {
},
},
})
step: Omit<Step, 'id' | 'instructionId'>,
step: Omit<Step, 'id' | 'link' | 'deleteHash' | 'instructionId'>,
): Promise<Step> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
Expand Down Expand Up @@ -110,9 +110,9 @@ export class InstructionStepController {
},
},
})
async patch(
@param.path.number('stepId') stepId: number,
async patchStep(
@param.path.number('instructionId') instructionId: number,
@param.path.number('stepId') stepId: number,
@requestBody({
content: {
'application/json': {
Expand Down Expand Up @@ -160,7 +160,7 @@ export class InstructionStepController {
}

@authenticate('jwt')
@del('/users/{id}/instructions/{instructionId}/steps/{stepId}', {
@del('/users/{id}/instructions/{instructionId}/steps/${stepId}', {
responses: {
'200': {
description: 'Delete Step',
Expand All @@ -174,9 +174,9 @@ export class InstructionStepController {
},
},
})
async delete(
@param.path.number('stepId') stepId: number,
async deleteStep(
@param.path.number('instructionId') instructionId: number,
@param.path.number('stepId') stepId: number,
): Promise<boolean> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
Expand Down Expand Up @@ -239,7 +239,7 @@ export class InstructionStepController {
},
},
})
async getPublicInstructions(
async getSteps(
@param.path.number('instructionId') instructionId: number,
): Promise<Omit<Step, 'deleteHash'>[]> {
const user = await this.userRepository.findById(this.user.id);
Expand Down Expand Up @@ -274,9 +274,9 @@ export class InstructionStepController {
},
},
})
async uploadPicture(
@param.path.number('stepId') stepId: number,
async uploadStepsPicture(
@param.path.number('instructionId') instructionId: number,
@param.path.number('stepId') stepId: number,
@requestBody({
content: {
'multipart/form-data': {
Expand Down Expand Up @@ -335,9 +335,9 @@ export class InstructionStepController {
},
},
})
async deleteImage(
@param.path.number('stepId') stepId: number,
async deleteStepPicture(
@param.path.number('instructionId') instructionId: number,
@param.path.number('stepId') stepId: number,
): Promise<boolean> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
Expand Down
158 changes: 88 additions & 70 deletions src/controllers/user-instruction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class UserInstructionController {
@repository(StepRepository) public stepRepository: StepRepository,
@repository(UserRepository)
protected progressRepository: ProgressRepository,
) {}
) { }

@authenticate('jwt')
@post('/users/{id}/instructions/{instructionId}', {
Expand All @@ -58,7 +58,7 @@ export class UserInstructionController {
},
},
})
async create(
async createInstruction(
@requestBody({
content: {
'application/json': {
Expand Down Expand Up @@ -124,7 +124,7 @@ export class UserInstructionController {
},
},
})
async patch(
async patchInstruction(
@param.path.number('instructionId') instructionId: number,
@requestBody({
content: {
Expand Down Expand Up @@ -181,8 +181,8 @@ export class UserInstructionController {
},
},
})
async delete(
@param.query.number('instructionId') instructionId: number,
async deleteInstruction(
@param.path.number('instructionId') instructionId: number,
): Promise<boolean> {
const userOriginal = await this.userRepository.findById(this.user.id);
if (!userOriginal) {
Expand Down Expand Up @@ -280,14 +280,14 @@ export class UserInstructionController {
},
})
async getUsersInstructions(): Promise<{
instreuctions: Omit<Instruction, 'deleteHash' | 'premiumUserIds'>[];
instructions: Omit<Instruction, 'deleteHash' | 'premiumUserIds'>[];
progress: (Progress & ProgressRelations)[];
}> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
throw new HttpErrors.NotFound('User not found');
}
const instreuctions = await this.instructionRepository.find({
const instructions = await this.instructionRepository.find({
where: {
userId: this.user.id,
},
Expand All @@ -311,7 +311,7 @@ export class UserInstructionController {
userId: this.user.id,
},
});
return {instreuctions, progress};
return {instructions, progress};
}

@authenticate('jwt')
Expand All @@ -329,7 +329,7 @@ export class UserInstructionController {
},
},
})
async uploadPicture(
async uploadInstructionPicture(
@param.path.number('instructionId') instructionId: number,
@requestBody({
content: {
Expand Down Expand Up @@ -384,7 +384,7 @@ export class UserInstructionController {
},
},
})
async deleteImage(
async deleteInstructionPicture(
@param.path.number('instructionId') instructionId: number,
): Promise<boolean> {
const user = await this.userRepository.findById(this.user.id);
Expand Down Expand Up @@ -423,12 +423,25 @@ export class UserInstructionController {
},
},
})
async setPremium(
async setPremiumInstruction(
@param.path.number('instructionId') instructionId: number,
@requestBody()
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
key: {type: 'string'},
premium: {type: 'boolean'},
},
required: ['key', 'premium'],
},
},
},
})
request: {
premium: boolean;
key: string;
premium: boolean;
},
): Promise<boolean> {
const user = await this.userRepository.findById(this.user.id);
Expand Down Expand Up @@ -466,6 +479,68 @@ export class UserInstructionController {
return true;
}

@authenticate('jwt')
@patch('/authorizate-for-premium-instruction/{instructionId}/{userId}', {
responses: {
'200': {
description: 'Authorize user for premium instructions',
content: {
'application/json': {
schema: {
type: 'boolean',
},
},
},
},
},
})
async authorizeUserToPremiumInstruction(
@param.path.number('instructionId') instructionId: number,
@param.path.number('userId') userId: number,
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
key: {type: 'string'},
},
required: ['key'],
},
},
},
})
request: {
key: string;
},
): Promise<boolean> {
const user = await this.userRepository.findById(this.user.id);
if (!user) {
throw new HttpErrors.NotFound('User not found');
}
const instructionKey =
process.env.INSTRUCTION_KEY_PREMIUM_PERMISSIONS ?? '';
const keyMatch = await this.hasher.comparePassword(
request.key,
instructionKey,
);
if (!keyMatch) {
throw new HttpErrors.Unauthorized('Invalid password');
}
const instruction =
await this.instructionRepository.findById(instructionId);
instruction.premiumUserIds = instruction.premiumUserIds ?? [];
if (instruction.premiumUserIds.includes(userId)) {
instruction.premiumUserIds = instruction.premiumUserIds.filter(
id => id !== userId,
);
} else {
instruction.premiumUserIds.push(userId);
}
await this.instructionRepository.updateById(instructionId, instruction);
return true;
}

@get('/public-instructions', {
responses: {
'200': {
Expand Down Expand Up @@ -630,63 +705,6 @@ export class UserInstructionController {
return data;
}

@patch('/authorizate-for-premium-instruction/{instructionId}/{userId}', {
responses: {
'200': {
description: 'Authorize user for premium instructions',
content: {
'application/json': {
schema: {
type: 'boolean',
},
},
},
},
},
})
async authorizeUserToPremiumInstruction(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
key: {type: 'string'},
},
required: ['key'],
},
},
},
})
request: {
key: string;
},
@param.query.number('instructionId') instructionId: number,
@param.query.number('userId') userId: number,
): Promise<boolean> {
const instructionKey =
process.env.INSTRUCTION_KEY_PREMIUM_PERMISSIONS ?? '';
const keyMatch = await this.hasher.comparePassword(
request.key,
instructionKey,
);
if (!keyMatch) {
throw new HttpErrors.Unauthorized('Invalid password');
}
const instruction =
await this.instructionRepository.findById(instructionId);
instruction.premiumUserIds = instruction.premiumUserIds ?? [];
if (instruction.premiumUserIds.includes(userId)) {
instruction.premiumUserIds = instruction.premiumUserIds.filter(
id => id !== userId,
);
} else {
instruction.premiumUserIds.push(userId);
}
await this.instructionRepository.updateById(instructionId, instruction);
return true;
}

private validateInstructionOwnership(instruction: Instruction): void {
if (Number(instruction.userId) !== Number(this.user.id)) {
throw new HttpErrors.Forbidden(
Expand Down
Loading

0 comments on commit 835bb94

Please sign in to comment.