@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
105 lines (104 loc) • 4.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const app_config_1 = require("../app.config");
const plugins_1 = require("../plugins");
const controller_parser_1 = require("../plugins/controller-parser");
const ResponseData_1 = require("../interfaces/ResponseData");
class BaseController {
constructor(service) {
if (service) {
this.service = service;
this.req = service.req;
}
this.ownership = { owner: this.user, workspace: this.workspace };
}
async read() {
if (!this.filter)
this.filter = {};
let data;
if (this.filter._id) {
data = await this.service.findOne(this.filter, this.options);
if ((0, lodash_1.isEmpty)(data))
return this.filter.owner ? (0, ResponseData_1.respondFailure)({ msg: `Unauthorized.` }) : (0, ResponseData_1.respondFailure)({ msg: `Item not found.` });
}
else {
data = (await this.service.find(this.filter, this.options, this.pagination)) || [];
// if (isEmpty(data)) return this.filter.owner ? respondFailure({ msg: `Unauthorized.` }) : respondFailure({ msg: "" });
}
return (0, ResponseData_1.respondSuccess)({ data, ...this.pagination });
}
async create(inputData) {
const data = await this.service.create(inputData);
if (!data)
return (0, ResponseData_1.respondFailure)("Can't create new item.");
return (0, ResponseData_1.respondSuccess)({ data });
}
async update(updateData) {
const data = await this.service.update(this.filter, updateData, this.options);
if ((0, lodash_1.isEmpty)(data))
return this.filter.owner ? (0, ResponseData_1.respondFailure)({ msg: `Unauthorized.` }) : (0, ResponseData_1.respondFailure)({ msg: `Item not found.` });
return (0, ResponseData_1.respondSuccess)({ data });
}
async delete() {
const tobeDeletedItems = await this.service.count(this.filter);
if (tobeDeletedItems === 0)
return this.filter.owner ? (0, ResponseData_1.respondFailure)({ msg: `Unauthorized.` }) : (0, ResponseData_1.respondFailure)({ msg: `Items not found.` });
const data = await this.service.delete(this.filter);
return (0, ResponseData_1.respondSuccess)({ data });
}
async softDelete() {
const tobeDeletedItems = await this.service.count(this.filter);
if (tobeDeletedItems === 0)
return this.filter.owner ? (0, ResponseData_1.respondFailure)({ msg: `Unauthorized.` }) : (0, ResponseData_1.respondFailure)({ msg: `Items not found.` });
const data = await this.service.softDelete(this.filter);
return (0, ResponseData_1.respondSuccess)({ data });
}
async empty() {
let data;
let result = { status: 1, data, messages: [] };
if (app_config_1.Config.ENV === "development") {
const emptyRes = await this.service.empty(this.filter);
result.data.ok = emptyRes.ok;
}
else {
result.data = { ok: 0 };
result.messages.push(`This function is restricted to use on development environment only.`);
}
return result;
}
parseDateRange(req, res, next) {
// TODO: process date range filter: from_date, to_date, from_time, to_time, date
if (next)
next();
}
parseBody(req, res, next) {
// log("req.body [1] >>", req.body);
req.body = (0, plugins_1.preprocessInputData)(req.body);
if (next)
next();
}
/**
* Parse the filter option from the URL:
* - List (first page, 10 item per page, sort "desc" by "updatedAt" first, then "desc" by "createdAt"): `https://example.com/api/v1/user?page=1&size=10&sort=-updatedAt,-createdAt`
* - Search (by username that contains "john"): `https://example.com/api/v1/user?page=1&size=10&username=john&search=true`
*/
parseFilter(req, res, next) {
const parsed = (0, controller_parser_1.parseFilterAndOptions)(req);
// assign to controller:
this.options = parsed.options;
this.filter = parsed.filter;
if (next)
next();
}
async parsePagination(req, res, next) {
if (!this.service)
return;
const pagination = await (0, controller_parser_1.parsePagination)(this.service, req);
this.pagination = pagination;
// log(`this.pagination >>`, this.pagination);
if (next)
next();
}
}
exports.default = BaseController;