From c88134cbdf1ac2816d7ff2581226607ac8c7763b Mon Sep 17 00:00:00 2001 From: Akalanka Date: Mon, 14 Aug 2023 01:01:42 +0530 Subject: [PATCH] Feat: added initial migration script Closes #80 --- src/app.js | 2 +- .../20230812121853-create_initial_settings.js | 16 ++++++++++++++++ src/routes/index.routes.js | 4 ++-- .../{settings.routes.js => setting.routes.js} | 0 src/services/auth.js | 4 ++-- src/services/user.js | 2 +- src/validations/settings.js | 2 +- 7 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 src/database/migrations/20230812121853-create_initial_settings.js rename src/routes/{settings.routes.js => setting.routes.js} (100%) diff --git a/src/app.js b/src/app.js index 55e4f98..27e52ac 100644 --- a/src/app.js +++ b/src/app.js @@ -1,4 +1,3 @@ -import crypto from 'crypto'; import express from 'express'; import context from 'express-http-context'; import rateLimit from 'express-rate-limit'; @@ -6,6 +5,7 @@ import httpLogger from '@sliit-foss/http-logger'; import { moduleLogger } from '@sliit-foss/module-logger'; import compression from 'compression'; import cors from 'cors'; +import crypto from 'crypto'; import helmet from 'helmet'; import { pick } from 'lodash'; import { default as connectDB } from '@/database'; diff --git a/src/database/migrations/20230812121853-create_initial_settings.js b/src/database/migrations/20230812121853-create_initial_settings.js new file mode 100644 index 0000000..3f9bc81 --- /dev/null +++ b/src/database/migrations/20230812121853-create_initial_settings.js @@ -0,0 +1,16 @@ +module.exports = { + async up(db) { + await db.collection('settings').deleteMany({}); + await db.collection('settings').insertOne({ + submission_deadline: new Date('2023-09-21T12:00:00.000Z'), + registration_deadline: new Date('2023-09-14T12:00:00.000Z'), + created_at: new Date(), + updated_at: new Date(), + __v: 0 + }); + }, + + async down(db) { + await db.collection('settings').deleteMany({}); + } +}; diff --git a/src/routes/index.routes.js b/src/routes/index.routes.js index e414e73..d24928d 100644 --- a/src/routes/index.routes.js +++ b/src/routes/index.routes.js @@ -3,7 +3,7 @@ import authRouter from './auth.routes'; import dashboardRouter from './dashboard.routes'; import leaderboardRouter from './leaderboard.routes'; import questionRouter from './question.routes'; -import settingsRouter from './settings.routes'; +import settingRouter from './setting.routes'; import submissionRouter from './submission.routes'; import userRouter from './user.routes'; import { adminProtect, protect } from '@/middleware/auth'; @@ -16,6 +16,6 @@ router.use('/users', protect, userRouter); router.use('/questions', protect, questionRouter); router.use('/dashboard', protect, adminProtect, dashboardRouter); router.use('/leaderboard', leaderboardRouter); -router.use('/settings', protect, adminProtect, settingsRouter); +router.use('/settings', protect, adminProtect, settingRouter); export default router; diff --git a/src/routes/settings.routes.js b/src/routes/setting.routes.js similarity index 100% rename from src/routes/settings.routes.js rename to src/routes/setting.routes.js diff --git a/src/services/auth.js b/src/services/auth.js index 7f42c33..976b818 100644 --- a/src/services/auth.js +++ b/src/services/auth.js @@ -1,9 +1,9 @@ -import crypto from 'crypto'; import bcrypt from 'bcryptjs'; +import crypto from 'crypto'; import createError from 'http-errors'; -import { sendMail } from './email'; import { createUser, findOneAndUpdateUser, getOneUser } from '@/repository/user'; import { decodeJwtToken } from '@/utils'; +import { sendMail } from './email'; export const authRegister = async ({ name, email, password, university, members }) => { const encryptedPassword = await new Promise((resolve, reject) => { diff --git a/src/services/user.js b/src/services/user.js index d920950..353c46a 100644 --- a/src/services/user.js +++ b/src/services/user.js @@ -1,7 +1,7 @@ import bcrypt from 'bcryptjs'; import createError from 'http-errors'; -import { sendMail } from './email'; import { createUser, findOneAndRemoveUser, findOneAndUpdateUser, getAllUsers, getOneUser } from '@/repository/user'; +import { sendMail } from './email'; export const getUsers = (query) => { return getAllUsers(query); diff --git a/src/validations/settings.js b/src/validations/settings.js index 2d431f8..a5bdf72 100644 --- a/src/validations/settings.js +++ b/src/validations/settings.js @@ -1,6 +1,6 @@ import { Joi } from 'celebrate'; -export const updateSettingsSchema = Joi.object.keys({ +export const updateSettingsSchema = Joi.object({ submission_deadline: Joi.date().allow(null), registration_deadline: Joi.date().allow(null) });