import 'reflect-metadata'; //import and if case for @nestjs/schedule Cron jobs import * as nodeCrypto from 'crypto'; if (!(globalThis as any).crypto) { (globalThis as any).crypto = nodeCrypto; } import { ensureAttachmentsTmpDir } from './config/attachment.fs'; import { resolveAttachmentsRoot } from './config/attachment.config';// log to be removed post dev import { ATT_TMP_DIR } from './config/attachment.config'; // log to be removed post dev import { ModuleRef, NestFactory, Reflector } from '@nestjs/core'; import { AppModule } from './app.module'; // import { JwtAuthGuard } from './modules/authentication/guards/jwt-auth.guard'; import { RolesGuard } from './common/guards/roles.guard'; import { OwnershipGuard } from './common/guards/ownership.guard'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { writeFileSync } from 'fs'; import * as session from 'express-session'; import * as passport from 'passport'; import { extractOldShifts } from 'scripts/migrate-shifts'; import { extractOldTimesheets } from 'scripts/migrate-timesheets'; import { extractOldExpenses } from 'scripts/migrate-expenses'; const SESSION_TOKEN_DURATION_MINUTES = 180 async function bootstrap() { const app = await NestFactory.create(AppModule); const reflector = app.get(Reflector); //setup Reflector for Roles() app.useGlobalGuards( // new JwtAuthGuard(reflector), //Authentification JWT new RolesGuard(reflector), //deny-by-default and Role-based Access Control new OwnershipGuard(reflector, app.get(ModuleRef)), //Global use of OwnershipGuard, not implemented yet ); // Authentication and session app.use(session({ secret: 'This is a super secret dev secret that you cant share with anyone', resave: false, saveUninitialized: false, rolling: true, cookie: { maxAge: SESSION_TOKEN_DURATION_MINUTES * 60 * 1000, // property maxAge requires milliseconds httpOnly: true, } })) app.use(passport.initialize()); app.use(passport.session()); // Enable CORS app.enableCors({ origin: ['http://10.100.251.2:9011', 'http://10.100.251.2:9012', 'http://10.100.251.2:9013', 'http://localhost:9000'], credentials: true, }); //swagger config const config = new DocumentBuilder() .setTitle('Targo_Backend') .setDescription('Documentation de l`API REST pour Targo (NestJS + Prisma)') .setVersion('1.0') .addBearerAuth({ type: 'http', scheme: 'bearer', bearerFormat: 'JWT', name: 'Authorization', description: 'Invalid JWT token', in: 'header', }, 'access-token') .addTag('Users') .addTag('Employees') .addTag('Customers') .addTag('Timesheets') .addTag('Shifts') .addTag('Leave Requests') .addTag('Shift Codes') .addTag('OAuth Access Tokens') .addTag('Authorization') .build(); //document builder for swagger docs const documentFactory = () => SwaggerModule.createDocument(app, config); const document = documentFactory() SwaggerModule.setup('api/docs', app, document); writeFileSync('./docs/swagger/swagger-spec.json', JSON.stringify(document, null, 2)); // logs to be removed post dev console.log('[ENV.ATTACHMENTS_ROOT]', process.env.ATTACHMENTS_ROOT); console.log('[resolveAttachmentsRoot()]', resolveAttachmentsRoot()); console.log('[ATT_TMP_DIR()]', ATT_TMP_DIR()); await ensureAttachmentsTmpDir(); await app.listen(process.env.PORT ?? 3000); // migration function calls // await extractOldTimesheets(); // await extractOldShifts(); await extractOldExpenses(); } bootstrap();