UNPKG

express-typeorm-rest-boilerplate

Version:

Boilerplate code to get started with building RESTful API Services

117 lines 5.71 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = require("express"); const typedi_1 = require("typedi"); const celebrate_1 = require("celebrate"); const JobApplicationService_1 = __importDefault(require("../services/JobApplicationService")); const JobApplication_1 = require("../entities/JobApplication"); const middlewares_1 = require("../middlewares"); const CompanyService_1 = __importDefault(require("../services/CompanyService")); const route = express_1.Router(); route.get('/', middlewares_1.isAuth, (req, res, next) => __awaiter(void 0, void 0, void 0, function* () { const logger = typedi_1.Container.get('logger'); logger.debug('Calling GET to /jobApplication endpoint'); try { const jobApplicationServiceInstance = typedi_1.Container.get(JobApplicationService_1.default); const jobApplications = yield jobApplicationServiceInstance.find(); return res.status(200).json(jobApplications); } catch (e) { return next(e); } })); route.get('/:id', middlewares_1.isAuth, middlewares_1.attachUser, (req, res, next) => __awaiter(void 0, void 0, void 0, function* () { const logger = typedi_1.Container.get('logger'); logger.debug('Calling GET to /jobApplication/:id endpoint with id: %s', req.params.id); try { const jobApplicationServiceInstance = typedi_1.Container.get(JobApplicationService_1.default); const jobApplication = yield jobApplicationServiceInstance.findOne(req.params.id); const jobUser = jobApplication.user; if (!jobUser.id.equals(req.currentUser.id)) return res.sendStatus(403); return res.status(200).json(jobApplication); } catch (e) { return next(e); } })); route.delete('/:id', middlewares_1.isAuth, middlewares_1.attachUser, (req, res, next) => __awaiter(void 0, void 0, void 0, function* () { const logger = typedi_1.Container.get('logger'); logger.debug('Calling DELETE to /jobApplication/:id endpoint with id: %s', req.params.id); try { const jobApplicationServiceInstance = typedi_1.Container.get(JobApplicationService_1.default); const jobUser = (yield jobApplicationServiceInstance.findOne(req.params.id)) .user; if (!jobUser.id.equals(req.currentUser.id)) return res.sendStatus(403); yield jobApplicationServiceInstance.delete(req.params.id); return res.status(204).end(); } catch (e) { return next(e); } })); route.post('/', middlewares_1.isAuth, middlewares_1.attachUser, celebrate_1.celebrate({ body: celebrate_1.Joi.object({ role: celebrate_1.Joi.string().required(), description: celebrate_1.Joi.string().required(), company: celebrate_1.Joi.string().required(), status: celebrate_1.Joi.string().required(), appliedDate: celebrate_1.Joi.string().required(), }), }), (req, res, next) => __awaiter(void 0, void 0, void 0, function* () { const logger = typedi_1.Container.get('logger'); logger.debug('Calling POST to /jobApplication/:id endpoint with body: %o', req.body); try { const jobApplicationServiceInstance = typedi_1.Container.get(JobApplicationService_1.default); req.body.company = (yield typedi_1.Container.get(CompanyService_1.default).findOne(req.body.company)).id; req.body.user = req.currentUser.id; const jobApplication = yield jobApplicationServiceInstance.create(new JobApplication_1.JobApplication(req.body)); return res.status(201).json(jobApplication); } catch (e) { return next(e); } })); route.put('/:id', middlewares_1.isAuth, middlewares_1.attachUser, celebrate_1.celebrate({ body: celebrate_1.Joi.object({ role: celebrate_1.Joi.string(), description: celebrate_1.Joi.string(), status: celebrate_1.Joi.string(), appliedDate: celebrate_1.Joi.string(), }), }), (req, res, next) => __awaiter(void 0, void 0, void 0, function* () { const logger = typedi_1.Container.get('logger'); logger.debug('Calling PUT to /jobApplication/:id endpoint with body: %o', req.body); try { const jobApplicationServiceInstance = typedi_1.Container.get(JobApplicationService_1.default); const job = yield jobApplicationServiceInstance .getRepo() .findOne(req.params.id); if (!job) return res.sendStatus(500); if (req.currentUser.role !== 'admin' && !job.user.equals(req.currentUser.id)) { return res.sendStatus(403); } const jobApplication = yield jobApplicationServiceInstance.update(req.params.id, req.body); return res.status(200).json(jobApplication); } catch (e) { return next(e); } })); exports.default = route; //# sourceMappingURL=jobApplication.js.map