diff --git a/backend/.env.development b/backend/.env.development index d6d7ae8..2e82eb0 100644 --- a/backend/.env.development +++ b/backend/.env.development @@ -9,3 +9,5 @@ DB_PORT=5432 DB_USER=postgres DB_DATABASE=postgres DB_PASSWORD=postgres +USOS_CONSUMER_KEY=key +USOS_CONSUMER_SECRET=secret \ No newline at end of file diff --git a/backend/app/controllers/session_controller.ts b/backend/app/controllers/session_controller.ts index 9575bca..c396a15 100644 --- a/backend/app/controllers/session_controller.ts +++ b/backend/app/controllers/session_controller.ts @@ -1,26 +1,27 @@ -import User from '#models/user' import { HttpContext } from '@adonisjs/core/http' +import { createClient } from '../usos/usos_client.js' export default class SessionController { - async store({ request, auth, response }: HttpContext) { + async store({ request, response }: HttpContext) { /** * Step 1: Get credentials from the request body */ - const { id, password } = request.only(['id', 'password']) + const { accessToken, accessSecret } = request.only(['accessToken', 'accessSecret']) + try { + const usosClient = createClient({ + token: accessToken, + secret: accessSecret, + }) + const profile: any = await usosClient.get( + 'users/user?fields=id|student_number|first_name|last_name', + { + shouldCache: false, // Nie używamy cache dla tego zapytania + } + ) + return { message: 'User logged in.', firstName: profile.first_name } + } catch (error) { + return response.unauthorized({ accessToken, accessSecret }) + } - /** - * Step 2: Verify credentials - */ - const user = await User.verifyCredentials(id, password) - - /** - * Step 3: Login user - */ - await auth.use('web').login(user) - - /** - * Step 4: Send them to a protected route - */ - console.log(response) } } diff --git a/backend/app/usos/usos_client.ts b/backend/app/usos/usos_client.ts new file mode 100644 index 0000000..34914a6 --- /dev/null +++ b/backend/app/usos/usos_client.ts @@ -0,0 +1,103 @@ +import crypto from 'node:crypto' +import OAuth from 'oauth-1.0a' +import { LRUCache } from 'lru-cache' +import env from '#start/env' +import fetch from 'node-fetch' +const baseUrl = `https://apps.usos.pwr.edu.pl/services` + +const cache = new LRUCache({ + ttl: 60 * 60 * 1000, + ttlAutopurge: false, + max: 10000, +}) +const oauth = new OAuth({ + consumer: { key: env.get('USOS_CONSUMER_KEY'), secret: env.get('USOS_CONSUMER_SECRET') }, + signature_method: 'HMAC-SHA1', + hash_function(base_string, key) { + return crypto.createHmac('sha1', key).update(base_string).digest('base64') + }, +}) +export const createClient = ({ token, secret }: { token?: string; secret?: string }) => { + if (typeof token !== 'string' || typeof secret !== 'string') { + throw new Error('No token or secret provided') + } + return { + async get( + endpoint: string, + { shouldCache }: { shouldCache: boolean } = { shouldCache: true } + ): Promise { + const url = `${baseUrl}/${endpoint}` + + if (cache.has(url) && shouldCache) { + return cache.get(url) as R + } + + const data = oauth.authorize( + { + url, + method: 'GET', + }, + { + key: token, + secret, + } + ) + const response = await fetch(url, { + method: 'GET', + headers: { + Authorization: oauth.toHeader(data).Authorization, + }, + }) + + if (response.status === 401) { + throw new Error('Unauthorized') + } + + if (!response.ok) { + // eslint-disable-next-line no-console + console.log('Not ok', await response.text()) + throw new Error('Unauthorized') + } + + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const json: any = await response.json() + + if (shouldCache) { + cache.set(url, json) + } + + return json as Promise + }, + async post(endpoint: string, body: unknown): Promise { + const url = `${baseUrl}/${endpoint}` + + const data = oauth.authorize( + { + url, + method: 'POST', + data: body, + includeBodyHash: true, + }, + { + key: token, + secret, + } + ) + const response = await fetch(url, { + method: 'POST', + headers: { + Authorization: oauth.toHeader(data).Authorization, + }, + body: JSON.stringify(body), + }) + + if (!response.ok) { + throw new Error(response.statusText) + } + + return response.json() as Promise + }, + } +} + +export type UsosClient = ReturnType diff --git a/backend/database/migrations/1732121670545_create_users_table.ts b/backend/database/migrations/1732121670545_create_users_table.ts new file mode 100644 index 0000000..d6a6798 --- /dev/null +++ b/backend/database/migrations/1732121670545_create_users_table.ts @@ -0,0 +1,21 @@ +import { BaseSchema } from '@adonisjs/lucid/schema' + +export default class extends BaseSchema { + protected tableName = 'users' + + async up() { + this.schema.createTable(this.tableName, (table) => { + table.increments('id').notNullable() + table.string('full_name').nullable() + table.string('email', 254).notNullable().unique() + table.string('password').notNullable() + + table.timestamp('created_at').notNullable() + table.timestamp('updated_at').nullable() + }) + } + + async down() { + this.schema.dropTable(this.tableName) + } +} \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index b4e2895..b0c6cf5 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -9,15 +9,18 @@ "version": "0.0.0", "license": "AGPL-3.0-only", "dependencies": { - "@adonisjs/auth": "^9.2.3", + "@adonisjs/auth": "^9.2.4", "@adonisjs/core": "^6.12.1", "@adonisjs/cors": "^2.2.1", "@adonisjs/lucid": "^21.2.0", "@adonisjs/session": "^7.5.0", + "@maximemrf/adonisjs-jwt": "^0.2.2", "@vinejs/vine": "^2.1.0", "adonisjs-scheduler": "^1.0.5", "cheerio": "^1.0.0", "luxon": "^3.5.0", + "node-fetch": "^3.3.2", + "oauth-1.0a": "^2.2.6", "pg": "^8.13.0", "reflect-metadata": "^0.2.2" }, @@ -115,12 +118,13 @@ } }, "node_modules/@adonisjs/auth": { - "version": "9.2.3", - "resolved": "https://registry.npmjs.org/@adonisjs/auth/-/auth-9.2.3.tgz", - "integrity": "sha512-my/dqQJo1LQRhT3bW0RuK/1BhDTWcY3tVrGZmVMedCRBfYGt91OAreMy0muukJcWr8AkXBtbBsju2D+myaDa4Q==", + "version": "9.2.4", + "resolved": "https://registry.npmjs.org/@adonisjs/auth/-/auth-9.2.4.tgz", + "integrity": "sha512-n6YLy1U0ClKCNFRwC3t+TQBXlJjKwGmRQEQdJnuSgK1L38R8KhU9LbsFTkMH+iKSOigMRQlILtd4Sgf0ekoC6w==", "license": "MIT", "dependencies": { "@adonisjs/presets": "^2.5.1", + "@poppinss/utils": "^6.8.3", "basic-auth": "^2.0.1" }, "engines": { @@ -1275,6 +1279,22 @@ "node": ">=8" } }, + "node_modules/@maximemrf/adonisjs-jwt": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@maximemrf/adonisjs-jwt/-/adonisjs-jwt-0.2.2.tgz", + "integrity": "sha512-rYwMpuzSD0Iviojwm8paqSwO5giqutIvM1c5hrEBWXmJ1K3Rb8KFJhiJgwh2m5xYdSvoat9dVCRhHWJKHEgAIw==", + "license": "MIT", + "dependencies": { + "@types/jsonwebtoken": "^9.0.6", + "jsonwebtoken": "^9.0.2" + }, + "engines": { + "node": ">=20.6.0" + }, + "peerDependencies": { + "@adonisjs/core": "^6.2.0" + } + }, "node_modules/@noble/hashes": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.5.0.tgz", @@ -1892,6 +1912,15 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/jsonwebtoken": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.7.tgz", + "integrity": "sha512-ugo316mmTYBl2g81zDFnZ7cfxlut3o+/EQdaP7J8QN2kY6lJ22hmQYCK5EHcJHbrW+dkCGSCPgbG8JtYj6qSrg==", + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/luxon": { "version": "3.4.2", "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz", @@ -1910,7 +1939,6 @@ "version": "22.5.5", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.5.5.tgz", "integrity": "sha512-Xjs4y5UPO/CLdzpgR6GirZJx36yScjh73+2NlLlkFRSoQN8B0DpfXPdZGnvVmLRLOsqDpOfTNv7D9trgGhmOIA==", - "devOptional": true, "license": "MIT", "dependencies": { "undici-types": "~6.19.2" @@ -2764,6 +2792,12 @@ "ieee754": "^1.2.1" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==", + "license": "BSD-3-Clause" + }, "node_modules/builtin-modules": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz", @@ -3794,6 +3828,15 @@ "url": "https://dotenvx.com" } }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/edgejs-parser": { "version": "0.2.10", "resolved": "https://registry.npmjs.org/edgejs-parser/-/edgejs-parser-0.2.10.tgz", @@ -4567,6 +4610,29 @@ "reusify": "^1.0.4" } }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/figures": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/figures/-/figures-6.1.0.tgz", @@ -4729,6 +4795,18 @@ "node": ">= 6" } }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "license": "MIT", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/formidable": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.2.tgz", @@ -6053,6 +6131,28 @@ "node": "*" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "license": "MIT", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, "node_modules/junk": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/junk/-/junk-4.0.1.tgz", @@ -6066,6 +6166,27 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "license": "MIT", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "license": "MIT", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -6255,6 +6376,18 @@ "dev": true, "license": "MIT" }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==", + "license": "MIT" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==", + "license": "MIT" + }, "node_modules/lodash.isequal": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", @@ -6262,6 +6395,30 @@ "dev": true, "license": "MIT" }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==", + "license": "MIT" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==", + "license": "MIT" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==", + "license": "MIT" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==", + "license": "MIT" + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -6269,6 +6426,12 @@ "dev": true, "license": "MIT" }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==", + "license": "MIT" + }, "node_modules/lodash.set": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", @@ -6639,6 +6802,52 @@ "node": ">=10" } }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "license": "MIT", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, + "node_modules/node-fetch/node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, "node_modules/node-releases": { "version": "2.0.18", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz", @@ -6738,6 +6947,12 @@ "url": "https://github.com/fb55/nth-check?sponsor=1" } }, + "node_modules/oauth-1.0a": { + "version": "2.2.6", + "resolved": "https://registry.npmjs.org/oauth-1.0a/-/oauth-1.0a-2.2.6.tgz", + "integrity": "sha512-6bkxv3N4Gu5lty4viIcIAnq5GbxECviMBeKR3WX/q87SPQ8E8aursPZUtsXDnxCs787af09WPRBLqYrf/lwoYQ==", + "license": "MIT" + }, "node_modules/object-inspect": { "version": "1.13.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", @@ -8203,7 +8418,6 @@ "version": "7.6.3", "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", - "devOptional": true, "license": "ISC", "bin": { "semver": "bin/semver.js" @@ -9278,7 +9492,6 @@ "version": "6.19.8", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", - "devOptional": true, "license": "MIT" }, "node_modules/unicorn-magic": { @@ -9405,6 +9618,15 @@ "defaults": "^1.0.3" } }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "license": "MIT", + "engines": { + "node": ">= 8" + } + }, "node_modules/whatwg-encoding": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-3.1.1.tgz", diff --git a/backend/package.json b/backend/package.json index 78f59c2..f1c262d 100644 --- a/backend/package.json +++ b/backend/package.json @@ -51,15 +51,18 @@ "typescript": "~5.5" }, "dependencies": { - "@adonisjs/auth": "^9.2.3", + "@adonisjs/auth": "^9.2.4", "@adonisjs/core": "^6.12.1", "@adonisjs/cors": "^2.2.1", "@adonisjs/lucid": "^21.2.0", "@adonisjs/session": "^7.5.0", + "@maximemrf/adonisjs-jwt": "^0.2.2", "@vinejs/vine": "^2.1.0", "adonisjs-scheduler": "^1.0.5", "cheerio": "^1.0.0", "luxon": "^3.5.0", + "node-fetch": "^3.3.2", + "oauth-1.0a": "^2.2.6", "pg": "^8.13.0", "reflect-metadata": "^0.2.2" }, diff --git a/backend/start/env.ts b/backend/start/env.ts index b4d1525..d6b22e4 100644 --- a/backend/start/env.ts +++ b/backend/start/env.ts @@ -28,6 +28,8 @@ export default await Env.create(new URL('../', import.meta.url), { DB_USER: Env.schema.string(), DB_PASSWORD: Env.schema.string.optional(), DB_DATABASE: Env.schema.string(), + USOS_CONSUMER_KEY: Env.schema.string(), + USOS_CONSUMER_SECRET: Env.schema.string(), /* |---------------------------------------------------------- diff --git a/backend/start/scheduler.ts b/backend/start/scheduler.ts index c25e0b2..9ca9c4b 100644 --- a/backend/start/scheduler.ts +++ b/backend/start/scheduler.ts @@ -11,6 +11,7 @@ import Department from '#models/department' import Registration from '#models/registration' import Course from '#models/course' import Group from '#models/group' +import env from './env.js' function extractLastStringInBrackets(input: string): string | null { const regex = /\[([^\]]+)\]/g @@ -25,6 +26,9 @@ function extractLastStringInBrackets(input: string): string | null { } const scrapData = async () => { + if (env.get('NODE_ENV') !== 'production') { + return + } console.log('Scraping departments') const departments = await scrapDepartments() if (!departments) return @@ -126,5 +130,6 @@ const scrapData = async () => { console.log('Groups details scraped') } -// scrapData() -// scheduler.call(scrapData).everyTwoHours() +scrapData() +scheduler.call(scrapData).everySixHours() +