Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions api/migrations/20230805131402-create-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('events', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
title: {
type: Sequelize.STRING,
allowNull: false,
},
description: {
type: Sequelize.TEXT,
defaultValue: '',
allowNull: false,
},
examinations: {
type: Sequelize.ARRAY(Sequelize.BIGINT),
allowNull: false,
},
startDate: {
type: Sequelize.DATE,
allowNull: false,
},
endDate: {
type: Sequelize.DATE,
allowNull: false,
},
primaryPrice: {
type: Sequelize.BIGINT,
allowNull: false,
},
secondaryPrice: {
type: Sequelize.BIGINT,
allowNull: false,
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
},
});
},

async down(queryInterface, Sequelize) {
await queryInterface.dropTable('events');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
up: async (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn('orders', 'eventId', {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: 'events',
key: 'id',
},
}),
queryInterface.addColumn('examAttempts', 'eventId', {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: 'events',
key: 'id',
},
}),
]);
},

down: async (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.removeColumn('orders', 'eventId'),
queryInterface.removeColumn('examAttempts', 'eventId'),
]);
},
};
51 changes: 51 additions & 0 deletions api/models/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const JsonApiModel = require('base/jsonApiModel');
const SerializerOpts = require('serializer-opts/event');

class Event extends JsonApiModel {
static get serializerOpts() {
return SerializerOpts(this);
}

static associate() {}
}

module.exports = (sequelize, DataTypes) => {
Event.init(
{
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
defaultValue: '',
allowNull: false,
},
examinations: {
type: DataTypes.ARRAY(DataTypes.BIGINT),
allowNull: false,
},
startDate: {
type: DataTypes.DATE,
allowNull: false,
},
endDate: {
type: DataTypes.DATE,
allowNull: false,
},
primaryPrice: {
type: DataTypes.BIGINT,
allowNull: false,
},
secondaryPrice: {
type: DataTypes.BIGINT,
allowNull: false,
},
},
{
sequelize,
modelName: 'events',
},
);
return Event;
};
3 changes: 2 additions & 1 deletion api/models/exam-attempt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class ExamAttempt extends JsonApiModel {
return SerializerOpts(this);
}

static associate({ examAttempts, examinations, users }) {
static associate({ examAttempts, examinations, users, events }) {
examAttempts.belongsTo(examinations);
examAttempts.belongsTo(users);
examAttempts.belongsTo(events);
}
}

Expand Down
3 changes: 2 additions & 1 deletion api/models/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Order extends JsonApiModel {
return SerializerOpts(this);
}

static associate({ orders, users }) {
static associate({ orders, users, events }) {
orders.belongsTo(users);
users.hasMany(orders);
orders.belongsTo(events);
}
}

Expand Down
37 changes: 37 additions & 0 deletions api/routes/admin/events/controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const BaseDetailController = require('base/controllers/detailController');
const BaseCreateController = require('base/controllers/createController');
const BaseUpdateController = require('base/controllers/updateController');
const BaseDeleteController = require('base/controllers/deleteController');
const DB = require('models');
const EventSerializerOpts = require('serializer-opts/admin/event');

class EventDetailController extends BaseDetailController {
model = DB.events;
modelName = DB.events.name;
serializerOpts = EventSerializerOpts(DB.events.name);
}

class EventDeleteController extends BaseDeleteController {
model = DB.events;
modelName = DB.events.name;
serializerOpts = EventSerializerOpts(DB.events.name);
}

class EventCreateController extends BaseCreateController {
model = DB.events;
modelName = DB.events.name;
serializerOpts = EventSerializerOpts(DB.events.name);
}

class EventUpdateController extends BaseUpdateController {
model = DB.events;
modelName = DB.events.name;
serializerOpts = EventSerializerOpts(DB.events.name);
}

module.exports = {
EventDetailController,
EventDeleteController,
EventCreateController,
EventUpdateController,
};
32 changes: 32 additions & 0 deletions api/routes/admin/events/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const AdminRoleRequired = require('hooks/admin-role-required');
const Controllers = require('./controllers');

module.exports = async (app, opts) => {
app.get(
'/:id',
{
preHandler: AdminRoleRequired,
},
Controllers.EventDetailController.asHandler('get'),
);

app.post(
'/',
{ preHandler: AdminRoleRequired },
Controllers.EventCreateController.asHandler('post'),
);

app.patch(
'/:id',
{ preHandler: AdminRoleRequired },
Controllers.EventUpdateController.asHandler('patch'),
);

app.delete(
'/:id',
{ preHandler: AdminRoleRequired },
Controllers.EventDeleteController.asHandler('delete'),
);
};

module.exports.autoPrefix = '/admin/events';
16 changes: 16 additions & 0 deletions api/routes/events/controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const BaseListController = require('base/controllers/listController');
const BaseDetailController = require('base/controllers/detailController');
const DB = require('models');

class EventListController extends BaseListController {
model = DB.events;
}

class EventDetailController extends BaseDetailController {
model = DB.events;
}

module.exports = {
EventListController,
EventDetailController,
};
16 changes: 16 additions & 0 deletions api/routes/events/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Controllers = require('./controllers');
const LoginRequired = require('hooks/login-required');

module.exports = async (app, opts) => {
app.get('/', Controllers.EventListController.asHandler('get'));

app.get(
'/:id',
{
preHandler: [LoginRequired],
},
Controllers.EventDetailController.asHandler('get'),
);
};

module.exports.autoPrefix = '/events';
14 changes: 11 additions & 3 deletions api/routes/orders/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ class OrdersDetailController extends BaseDetailController {

async post() {
const _ordersService = this.app.getService('orders');
const { examinationIds } = this.request.body;
const { examinationIds, eventId } = this.request.body;

const order = await _ordersService.createOrder(examinationIds, this.request.user.id);
const order = await _ordersService.createOrder({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would need to check if event has expired or not based on endDate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has been already taken care inside api/services/orders/index.js

examinationIds,
userId: this.request.user.id,
eventId,
});

return this.serialize(order);
}
Expand Down Expand Up @@ -54,7 +58,11 @@ class OrdersDetailController extends BaseDetailController {
// fetch updated object
order = await this.getObject();

enrollInExaminations(order.examinations, this.request.user.id);
enrollInExaminations({
examinationsIds: order.examinations,
userId: this.request.user.id,
eventId: order.eventId,
});

return this.serialize(order);
} catch (err) {
Expand Down
5 changes: 4 additions & 1 deletion api/routes/orders/schema.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const postSchema = {
body: {
type: 'object',
required: ['examinationIds'],
required: ['examinationIds', 'eventId'],
properties: {
examinationIds: {
type: 'array',
maxItems: 2,
items: { type: 'integer' },
},
eventId: {
type: 'integer',
},
},
},
};
Expand Down
3 changes: 2 additions & 1 deletion api/routes/orders/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DB = require('models');

const enrollInExaminations = async (examinationsIds, userId) => {
const enrollInExaminations = async ({ examinationsIds, userId, eventId }) => {
const result = await DB.sequelize.transaction(async (transaction) => {
await Promise.all(
examinationsIds.map(async (examinationId) => {
Expand All @@ -11,6 +11,7 @@ const enrollInExaminations = async (examinationsIds, userId) => {
start: exam.start,
userId,
examinationId,
eventId,
},
{ transaction },
);
Expand Down
12 changes: 12 additions & 0 deletions api/serializer-opts/admin/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = () => ({
attributes: [
'id',
'title',
'description',
'examinations',
'startDate',
'endDate',
'primaryPrice',
'secondaryPrice',
],
});
5 changes: 5 additions & 0 deletions api/serializer-opts/admin/exam-attempts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const ExaminationSerializerOpts = require('./examination');
const UserSerializerOpts = require('./user');
const EventSerializerOpts = require('./event');

module.exports = () => ({
attributes: ['id', 'start', 'isSubmitted', 'submittedAt', 'result', 'examination', 'user'],
examination: {
ref: 'id',
...ExaminationSerializerOpts(),
},
event: {
ref: 'id',
...EventSerializerOpts(),
},
user: {
ref: 'id',
...UserSerializerOpts(),
Expand Down
15 changes: 15 additions & 0 deletions api/serializer-opts/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = () => ({
attributes: [
'id',
'title',
'description',
'examinations',
'startDate',
'endDate',
'primaryPrice',
'secondaryPrice',
],
meta: {
pagination: (records) => records.pagination,
},
});
5 changes: 5 additions & 0 deletions api/serializer-opts/exam-attempts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ExaminationSerializerOpts = require('./examination');
const UserSerializerOpts = require('./user');
const EventSerializerOpts = require('./event');

module.exports = () => ({
attributes: [
Expand All @@ -16,6 +17,10 @@ module.exports = () => ({
ref: 'id',
...ExaminationSerializerOpts(),
},
event: {
ref: 'id',
...EventSerializerOpts(),
},
user: {
ref: 'id',
...UserSerializerOpts(),
Expand Down
Loading