Skip to content

Commit

Permalink
Merge pull request #31 from Selecro/progess
Browse files Browse the repository at this point in the history
Progess
  • Loading branch information
Szotkowski authored Sep 20, 2023
2 parents c95e92c + 881ffbe commit 42400be
Show file tree
Hide file tree
Showing 22 changed files with 736 additions and 119 deletions.
61 changes: 36 additions & 25 deletions .github/workflows/continuous-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ jobs:
- name: Set deployment variables
run: |
if [ "${{ github.ref }}" = "refs/heads/dev" ]; then
echo "EXTPORT=3002" >> $GITHUB_ENV
echo "IMAGE=backend-dev" >> $GITHUB_ENV
echo "VAULT_PORT=8202" >> $GITHUB_ENV
echo "SQLDATABASE=selecro_dev" >> $GITHUB_ENV
echo "DEFAULT_PORT=${{ secrets.DEFAULT_PORT_DEV }}" >> $GITHUB_ENV
echo "SQL_DATABASE=${{ secrets.SQL_DATABASE_DEV }}" >> $GITHUB_ENV
echo "VAULT_PORT=${{ secrets.VAULT_PORT_DEV }}" >> $GITHUB_ENV
echo "UNSEAL_KEY_1=${{ secrets.UNSEAL_KEY_1_DEV }}" >> $GITHUB_ENV
echo "UNSEAL_KEY_2=${{ secrets.UNSEAL_KEY_2_DEV }}" >> $GITHUB_ENV
echo "UNSEAL_KEY_3=${{ secrets.UNSEAL_KEY_3_DEV }}" >> $GITHUB_ENV
echo "ROOT_VAULT_TOKEN=${{ secrets.ROOT_VAULT_TOKEN_DEV }}" >> $GITHUB_ENV
elif [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "EXTPORT=3001" >> $GITHUB_ENV
echo "IMAGE=backend-main" >> $GITHUB_ENV
echo "VAULT_PORT=8201" >> $GITHUB_ENV
echo "SQLDATABASE=selecro_main" >> $GITHUB_ENV
echo "DEFAULT_PORT=${{ secrets.DEFAULT_PORT_MAIN }}" >> $GITHUB_ENV
echo "SQL_DATABASE=${{ secrets.SQL_DATABASE_MAIN }}" >> $GITHUB_ENV
echo "VAULT_PORT=${{ secrets.VAULT_PORT_MAIN }}" >> $GITHUB_ENV
echo "UNSEAL_KEY_1=${{ secrets.UNSEAL_KEY_1_MAIN }}" >> $GITHUB_ENV
echo "UNSEAL_KEY_2=${{ secrets.UNSEAL_KEY_2_MAIN }}" >> $GITHUB_ENV
echo "UNSEAL_KEY_3=${{ secrets.UNSEAL_KEY_3_MAIN }}" >> $GITHUB_ENV
echo "ROOT_VAULT_TOKEN=${{ secrets.ROOT_VAULT_TOKEN_MAIN }}" >> $GITHUB_ENV
else
echo "Invalid branch for deployment" && exit 1
fi
Expand Down Expand Up @@ -78,27 +86,30 @@ jobs:
docker ps -a | grep ${{ env.IMAGE }} && docker stop ${{ env.IMAGE }} || true && \
docker ps -a | grep ${{ env.IMAGE }} && docker rm ${{ env.IMAGE }} || true && \
docker run \
-e HOST="${{ secrets.HOST }}" \
-e SQLHOST="${{ secrets.SQLHOST }}" \
-e SQLPORT="${{ secrets.SQLPORT }}" \
-e SQLUSER="${{ secrets.SQLUSER }}" \
-e SQLPASSWORD="${{ secrets.SQLPASSWORD }}" \
-e SQLDATABASE="${{ env.SQLDATABASE }}" \
-e TOKEN="${{ secrets.TOKEN }}" \
-e EXTPORT="${{ env.EXTPORT }}" \
-e EMAILHOST="${{ secrets.EMAILHOST }}" \
-e EMAILPORT="${{ secrets.EMAILPORT }}" \
-e EMAILUSER="${{ secrets.EMAILUSER }}" \
-e EMAILPASSWORD="${{ secrets.EMAILPASSWORD }}" \
-e JWT_SECRET="$${{ secrets.JWT_SECRET }}" \
-e UNSEAL_KEY_1="${{ secrets.UNSEAL_KEY_1 }}" \
-e UNSEAL_KEY_2="${{ secrets.UNSEAL_KEY_2 }}" \
-e DEFAULT_HOST="${{ secrets.DEFAULT_HOST }}" \
-e DEFAULT_PORT="${{ env.DEFAULT_PORT }}" \
-e JWT_SECRET="${{ secrets.JWT_SECRET }}" \
-e JWT_SECRET_EMAIL="${{ secrets.JWT_SECRET_EMAIL }}" \
-e JWT_SECRET_SIGNUP="${{ secrets.JWT_SECRET_SIGNUP }}" \
-e SQL_HOST="${{ secrets.SQL_HOST }}" \
-e SQL_PORT="${{ secrets.SQL_PORT }}" \
-e SQL_USER="${{ secrets.SQL_USER }}" \
-e SQL_PASSWORD="${{ secrets.SQL_PASSWORD }}" \
-e SQL_DATABASE="${{ env.SQL_DATABASE }}" \
-e EMAIL_HOST="${{ secrets.EMAIL_HOST }}" \
-e EMAIL_PORT="${{ secrets.EMAIL_PORT }}" \
-e EMAIL_USER="${{ secrets.EMAIL_USER }}" \
-e EMAIL_PASSWORD="${{ secrets.EMAIL_PASSWORD }}" \
-e VAULT_URL="${{ secrets.VAULT_URL }}" \
-e VAULT_URL="${{ secrets.VAULT_URL }}" \
-e VAULT_PORT="${{ env.VAULT_PORT }}" \
-e CLIENT_ID="${{ secrets.CLIENT_ID }}" \
-e ROOT_VAULT="${{ secrets.ROOT_VAULT }}" \
-e INSTRUCTION_KEY="${{ secrets.INSTRUCTION_KEY }}" \
-e INSTRUCTION_KEY_PERMISSIONS="${{ secrets.INSTRUCTION_KEY_PERMISSIONS }}" \
-e UNSEAL_KEY_1="${{ env.UNSEAL_KEY_1 }}" \
-e UNSEAL_KEY_2="${{ env.UNSEAL_KEY_2 }}" \
-e UNSEAL_KEY_3="${{ env.UNSEAL_KEY_3 }}" \
-e ROOT_VAULT_TOKEN="${{ env.ROOT_VAULT_TOKEN }}" \
-e IMGUR_CLIENT_ID="${{ secrets.IMGUR_CLIENT_ID }}" \
-e INSTRUCTION_KEY_PREMIUM="${{ secrets.INSTRUCTION_KEY_PREMIUM }}" \
-e INSTRUCTION_KEY_PREMIUM_PERMISSIONS="${{ secrets.INSTRUCTION_KEY_PREMIUM_PERMISSIONS }}" \
--name ${{ env.IMAGE }} -dp ${{ env.EXTPORT }}:${{ env.EXTPORT }} \
selecro/${{ env.IMAGE }}:${{ github.ref_name }}-${{ env.SHORT_SHA }} && \
docker update --restart unless-stopped ${{ env.IMAGE }} && exit
Expand Down
18 changes: 15 additions & 3 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ import {
import {ServiceMixin} from '@loopback/service-proxy';
import * as dotenv from 'dotenv';
import path from 'path';
import {PingController, UserController} from './controllers';
import {
InstructionStepController,
PingController,
UserController,
UserInstructionController,
UserLinkController,
UserProgressController,
} from './controllers';
import {DbDataSource} from './datasources';
import {
InstructionRepository,
ProgressRepository,
StepRepository,
UserLinkRepository,
UserRepository,
Expand Down Expand Up @@ -49,16 +57,20 @@ export class SelecroBackendApplication extends BootMixin(
this.component(JWTAuthenticationComponent);
this.controller(PingController);
this.controller(UserController);
this.controller(StepRepository);
this.controller(UserProgressController);
this.controller(UserLinkController);
this.controller(UserInstructionController);
this.controller(InstructionStepController);
this.repository(UserRepository);
this.repository(InstructionRepository);
this.repository(StepRepository);
this.repository(UserLinkRepository);
this.repository(ProgressRepository);
this.dataSource(DbDataSource);

this.bind('services.jwt.service').toClass(JWTService);
this.bind('authentication.jwt.expiresIn').to('32d');
this.bind('authentication.jwt.secret').to(process.env.TOKEN);
this.bind('authentication.jwt.secret').to(process.env.JWT_SECRET);
this.bind('services.hasher').toClass(BcryptHasher);
this.bind('services.hasher.rounds').to(10);
this.bind('services.user.service').toClass(MyUserService);
Expand Down
1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './ping.controller';
export * from './user-instruction.controller';
export * from './user.controller';
export * from './user-link.controller';
export * from './user-progress.controller';
2 changes: 1 addition & 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
37 changes: 25 additions & 12 deletions src/controllers/user-instruction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import {
} from '@loopback/rest';
import {SecurityBindings, UserProfile} from '@loopback/security';
import * as dotenv from 'dotenv';
import {Difficulty, Instruction} from '../models';
import {Difficulty, Instruction, Progress, ProgressRelations} from '../models';
import {
InstructionRepository,
ProgressRepository,
StepRepository,
UserRepository,
} from '../repositories';
Expand All @@ -38,7 +39,9 @@ export class UserInstructionController {
@repository(InstructionRepository)
protected instructionRepository: InstructionRepository,
@repository(StepRepository) public stepRepository: StepRepository,
) { }
@repository(UserRepository)
protected progressRepository: ProgressRepository,
) {}

@authenticate('jwt')
@post('/users/{id}/instructions/{instructionId}', {
Expand Down Expand Up @@ -93,7 +96,7 @@ export class UserInstructionController {
if (!key) {
throw new HttpErrors.Unauthorized('Key not providen');
}
const instructionKey = process.env.INSTRUCTION_KEY ?? '';
const instructionKey = process.env.INSTRUCTION_KEY_PREMIUM ?? '';
const keyMatch = await this.hasher.comparePassword(key, instructionKey);
if (!keyMatch) {
throw new HttpErrors.Unauthorized('Invalid password');
Expand Down Expand Up @@ -276,14 +279,15 @@ export class UserInstructionController {
},
},
})
async getUsersInstructions(): Promise<
Omit<Instruction, 'deleteHash' | 'premiumUserIds'>[]
> {
async getUsersInstructions(): Promise<{
instreuctions: 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 data = await this.instructionRepository.find({
const instreuctions = await this.instructionRepository.find({
where: {
userId: this.user.id,
},
Expand All @@ -302,7 +306,12 @@ export class UserInstructionController {
premiumUserIds: false,
},
});
return data;
const progress = await this.progressRepository.find({
where: {
userId: this.user.id,
},
});
return {instreuctions, progress};
}

@authenticate('jwt')
Expand Down Expand Up @@ -431,7 +440,7 @@ export class UserInstructionController {
if (!instruction) {
throw new HttpErrors.NotFound('Instruction not found');
}
const instructionKey = process.env.INSTRUCTION_KEY ?? '';
const instructionKey = process.env.INSTRUCTION_KEY_PREMIUM ?? '';
const keyMatch = await this.hasher.comparePassword(
request.key,
instructionKey,
Expand Down Expand Up @@ -655,18 +664,22 @@ export class UserInstructionController {
@param.query.number('instructionId') instructionId: number,
@param.query.number('userId') userId: number,
): Promise<boolean> {
const instructionKey = process.env.INSTRUCTION_KEY_PERMISSIONS ?? '';
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);
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
Loading

0 comments on commit 42400be

Please sign in to comment.