Skip to content

Commit

Permalink
control level/lesson (#576)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernardo Vieira <bernardo@impactmarket.com>
  • Loading branch information
Joao Pedro da Silva and Bernardo Vieira authored May 16, 2023
1 parent 93c4d7c commit 0596305
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 192 deletions.
32 changes: 8 additions & 24 deletions packages/api/src/controllers/v2/learnAndEarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,7 @@ class LearnAndEarnController {
req: RequestWithUser<never, never, never, ListLevelsRequestType>,
res: Response
) => {
if (req.user === undefined) {
standardResponse(res, 401, false, '', {
error: {
name: 'USER_NOT_FOUND',
message: 'User not identified!',
},
});
return;
}

let { category, status, level, limit, offset } = req.query;
let { category, status, level, limit, offset, language } = req.query;

if (offset === undefined || typeof offset !== 'string') {
offset = config.defaultOffset.toString();
Expand All @@ -53,12 +43,12 @@ class LearnAndEarnController {

services.learnAndEarn
.listLevels(
req.user.userId,
parseInt(offset, 10),
parseInt(limit, 10),
req.user?.userId,
status,
category,
level
level,
language,
)
.then((r) => standardResponse(res, 200, true, r))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
Expand Down Expand Up @@ -104,18 +94,12 @@ class LearnAndEarnController {
};

listLessons = (req: RequestWithUser<{ id: string }>, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 401, false, '', {
error: {
name: 'USER_NOT_FOUND',
message: 'User not identified!',
},
});
return;
}
let { language } = req.query;

const id = parseInt(req.params.id, 10);

services.learnAndEarn
.listLessons(req.user.userId, parseInt(req.params.id, 10))
.listLessons(id ? id : req.params.id, req.user?.userId, language as string)
.then((r) => standardResponse(res, 200, true, r))
.catch((e) => standardResponse(res, 400, false, '', { error: e }));
};
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/routes/v2/learnAndEarn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from 'express';

import LearnAndEarnController from '../../controllers/v2/learnAndEarn';
import { authenticateToken } from '../../middlewares';
import { authenticateToken, optionalAuthentication } from '../../middlewares';
import LearnAndEarnValidator from '../../validators/learnAndEarn';

export default (app: Router): void => {
Expand Down Expand Up @@ -47,7 +47,7 @@ export default (app: Router): void => {
*/
route.get(
'/levels',
authenticateToken,
optionalAuthentication,
learnAndEarnValidator.listLevels,
learnAndEarnController.listLevels
);
Expand Down Expand Up @@ -125,7 +125,7 @@ export default (app: Router): void => {
*/
route.get(
'/levels/:id',
authenticateToken,
optionalAuthentication,
learnAndEarnController.listLessons
);

Expand Down
6 changes: 4 additions & 2 deletions packages/api/src/validators/learnAndEarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ export type AnswerRequestType = {
answers: number[];
};
export type StartLessonRequestType = {
lesson: number;
lesson: string;
};
export type ListLevelsRequestType = {
status: string;
category: string;
level: string;
limit?: string;
offset?: string;
language?: string;
};
export type RegisterClaimRewardsRequestType = {
transactionHash: string;
Expand All @@ -36,7 +37,7 @@ class LearnAndEarnValidator {

startLesson = celebrate({
body: defaultSchema.object({
lesson: Joi.number().required(),
lesson: Joi.string().required(),
}),
});

Expand All @@ -47,6 +48,7 @@ class LearnAndEarnValidator {
.valid('available', 'started', 'completed'),
category: Joi.string().optional(),
level: Joi.string().optional(),
language: Joi.string().optional(),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ module.exports = {
allowNull: false,
},
lessonId: {
type: Sequelize.INTEGER,
allowNull: false,
},
levelId: {
type: Sequelize.INTEGER,
references: {
model: 'learn_and_earn_lesson',
model: 'learn_and_earn_level',
key: 'id',
},
onDelete: 'CASCADE',
allowNull: false,
allowNull: true,
},
status: {
type: Sequelize.ENUM('available', 'started', 'completed'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

// eslint-disable-next-line no-undef
module.exports = {
async up(queryInterface, Sequelize) {
if (process.env.NODE_ENV === 'test') {
return;
}

// add new columns
await queryInterface.addColumn('learn_and_earn_user_lesson', 'levelId', {
type: Sequelize.INTEGER,
references: {
model: 'learn_and_earn_level',
key: 'id',
},
onDelete: 'CASCADE',
allowNull: true,
});

await queryInterface.removeConstraint('learn_and_earn_user_lesson', 'learn_and_earn_user_lesson_lessonId_fkey');

// set old columns as optional
await queryInterface.changeColumn('learn_and_earn_user_lesson', 'lessonId', {
type: Sequelize.INTEGER,
allowNull: false,
});
},
down: (queryInterface) => {},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const prismic = require('@prismicio/client');

const endpoint = prismic.getEndpoint(process.env.PRISMIC_REPO);
const accessToken = process.env.PRISMIC_ACCESS_TOKEN;

const client = prismic.createClient(endpoint, { accessToken });

// eslint-disable-next-line no-undef
module.exports = {
async up(queryInterface, Sequelize) {
if (process.env.NODE_ENV === 'test') {
return;
}

// get all user lesson
const userLessons = await queryInterface.sequelize.query(
`select learn_and_earn_user_lesson.id as id, learn_and_earn_lesson."prismicId" as "prismicId", learn_and_earn_lesson."levelId" as "levelId"
from learn_and_earn_user_lesson inner join learn_and_earn_lesson on learn_and_earn_user_lesson."lessonId" = learn_and_earn_lesson.id
where learn_and_earn_user_lesson."levelId" is null`,
{ type: Sequelize.QueryTypes.SELECT }
);

// get prismic id
const prismicIds = userLessons.map((userLesson) => userLesson.prismicId);

// get prismic document
try {
const prismicData = await client.getAllByIDs(prismicIds);

userLessons.forEach(async (userLesson) => {
const prismic = prismicData.find(
(el) => el.id === userLesson.prismicId
);
const levelId = userLesson.levelId;
const lessonId = prismic.data.id;

if (!!levelId && !!lessonId) {
await queryInterface.sequelize.query(
`update learn_and_earn_user_lesson set "levelId" = ${levelId}, "lessonId" = ${lessonId} where id = ${userLesson.id}`
);
}
});

// throw new Error();

} catch (error) {
console.log(error);
throw new Error();

}
},
down: (queryInterface) => {},
};
27 changes: 27 additions & 0 deletions packages/core/src/database/models/associations/learnAndEarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ export function learnAndEarnAssociation(sequelize: Sequelize) {
}
);

sequelize.models.LearnAndEarnPrismicLevelModel.hasMany(
sequelize.models.LearnAndEarnUserLevelModel,
{
foreignKey: 'levelId',
sourceKey: 'levelId',
as: 'userLevel',
}
);

sequelize.models.LearnAndEarnPrismicLevelModel.hasMany(
sequelize.models.LearnAndEarnPrismicLessonModel,
{
foreignKey: 'levelId',
sourceKey: 'levelId',
as: 'lesson',
}
);

sequelize.models.LearnAndEarnPrismicLessonModel.hasMany(
sequelize.models.LearnAndEarnUserLessonModel,
{
foreignKey: 'levelId',
sourceKey: 'levelId',
as: 'userLesson',
}
);

sequelize.models.LearnAndEarnLessonModel.hasMany(
sequelize.models.LearnAndEarnUserLessonModel,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class LearnAndEarnUserLessonModel extends Model<
public id!: number;
public userId!: number;
public lessonId!: number;
public levelId!: number;
public status!: 'available' | 'started' | 'completed';
public completionDate!: Date;
public attempts!: number;
Expand Down Expand Up @@ -40,11 +41,10 @@ export function initializeLearnAndEarnUserLesson(
},
lessonId: {
type: DataTypes.INTEGER,
references: {
model: 'learn_and_earn_lesson',
key: 'id',
},
onDelete: 'CASCADE',
allowNull: false,
},
levelId: {
type: DataTypes.INTEGER,
allowNull: false,
},
status: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LearnAndEarnUserLessonModel } from '../../database/models/learnAndEarn/learnAndEarnUserLesson';

/**
* @swagger
* components:
Expand Down Expand Up @@ -29,6 +31,8 @@ export interface LearnAndEarnPrismicLesson {
lessonId: number;
language: string;
isLive: boolean;

userLesson?: LearnAndEarnUserLessonModel[];
}

export interface LearnAndEarnPrismicLessonCreation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* type: integer
* lessonId:
* type: integer
* levelId:
* type: integer
* status:
* type: string
* completionDate:
Expand All @@ -32,6 +34,7 @@ export interface LearnAndEarnUserLesson {
id: number;
userId: number;
lessonId: number;
levelId: number;
status: 'available' | 'started' | 'completed';
completionDate: Date;
attempts: number;
Expand All @@ -41,6 +44,7 @@ export interface LearnAndEarnUserLesson {
export interface LearnAndEarnUserLessonCreation {
userId: number;
lessonId: number;
levelId: number;
status: 'available' | 'started' | 'completed';
completionDate?: Date;
attempts?: number;
Expand Down
Loading

0 comments on commit 0596305

Please sign in to comment.