UNPKG

@hv-kit/hexpress

Version:

facilitates typescript backend development with express

991 lines 54.1 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 }); exports._extract = exports._export = exports._publishOrUnpublish = exports._blockOrUnblock = exports._archiveOrRestore = exports._deleteOrAdd = exports._delete = exports._edit = exports._update = exports._add = exports._publishOrUnpublishMultiple = exports._blockOrUnblockMultiple = exports._archiveOrRestoreMultiple = exports._deleteMultiple = exports._editMultiple = exports._updateMultiple = exports._addMultiple = exports._exists = exports._findOne = exports._findAll = exports.ElementForUpdate = void 0; const export_functs_1 = require("./export_functs"); const other_1 = require("./other"); const jon_1 = __importDefault(require("@hivi/jon/jon")); const hexpress_config_json_1 = __importDefault(require("../../../../hexpress.config.json")); const error_1 = require("./error"); const db_1 = require("./db"); const error_2 = require("./error"); const config_1 = require("./config"); const import_functs_1 = require("./import_functs"); const formidable_1 = require("formidable"); function ElementForUpdate(oldElement, newElement, nullableAttributes = [], strictColumns = true) { strictColumns = (typeof strictColumns === 'boolean') ? strictColumns : true; nullableAttributes = (typeof nullableAttributes === 'object' && Array.isArray(nullableAttributes) === true) ? nullableAttributes.filter((value) => (typeof value === 'string' && value.length > 0)) : []; newElement = (typeof newElement === 'object' && Array.isArray(newElement) === false) ? newElement : {}; oldElement = (typeof oldElement === 'object' && Array.isArray(oldElement) === false) ? oldElement : {}; if (strictColumns) { oldElement = Object.fromEntries(new Map(Object.keys(oldElement).filter((key) => Object.keys(newElement).includes(key)).map((key) => [key, oldElement[key]]))); } // console.log(`> scripts.other | ElementForUpdate - oldElement:: `, oldElement); // console.log(`> scripts.other | ElementForUpdate - newElement:: `, newElement); // console.log(`> scripts.other | ElementForUpdate - nullableAttributes:: `, nullableAttributes); let finalData = Object.fromEntries(new Map(Object.keys(oldElement) .map((key, index) => [key, oldElement[key]]) .map((value, index) => { /* const iKey = Object.keys(newElement).find((key: string) => new RegExp(`^${value[0]}{1}$`, 'i').test(key)) || value[0]; console.log('\n\n>-------------------'); console.log(`> scripts.other | ElementForUpdate - value:: `, value); console.log(`> scripts.other | ElementForUpdate - value[0]:: `, value[0]); console.log(`> scripts.other | ElementForUpdate - value[1]:: `, value[1]); // > scripts.other | ElementForUpdate - value[0]:: updatedAt console.log(`> scripts.other | ElementForUpdate - iKey:: `, iKey); console.log(`> scripts.other | ElementForUpdate - newElement[value[0]]:: `, newElement[iKey]); console.log(`> scripts.other | ElementForUpdate - newElement['updatedAt']:: `, newElement['updatedAt']); console.log(`> scripts.other | ElementForUpdate - newElement:: `, newElement); console.log('-------------------<\n\n'); */ return value; }) .map((value, index) => { const iKey = Object.keys(newElement).find((key) => new RegExp(`^${value[0]}{1}$`, 'i').test(key)) || value[0]; const key = value[0]; let valueKey = undefined; if ((!nullableAttributes.includes(value[0]) && (!!value[1] || typeof value[1] === 'boolean' || value[1] === null) && (!!newElement[iKey] || typeof newElement[iKey] === 'boolean' || newElement[iKey] === null)) || (nullableAttributes.includes(value[0]) && (value[1] === null || newElement[iKey] === null))) { if (newElement[iKey] === null) { valueKey = null; } /* else if( value[1] === null ) { valueKey = newElement[iKey]; } */ else if (typeof newElement[iKey] === 'object' && Array.isArray(newElement[iKey]) === false && (newElement[iKey] instanceof formidable_1.PersistentFile || newElement[iKey] instanceof Date) && typeof value[1] === 'object' && Array.isArray(value[1]) === false && (value[1] instanceof formidable_1.PersistentFile || value[1] instanceof Date)) { valueKey = newElement[iKey]; } else if (typeof newElement[iKey] === 'object' && Array.isArray(newElement[iKey]) === true && typeof value[1] === 'object' && Array.isArray(value[1]) === true) { valueKey = newElement[iKey]; /* const valueObj: any = Object.fromEntries( new Map( Object.keys(value[1]).map((key: string, index: number) => [key, value[1][key]]) ) ); const newElementObj: any = Object.fromEntries( new Map( Object.keys(newElement[iKey]).map((key: string, index: number) => [key, newElement[iKey][key]]) ) ); valueKey = Object.values( ElementForUpdate(valueObj, newElementObj, nullableAttributes, false) ); */ } else if (typeof newElement[iKey] === 'object' && Array.isArray(newElement[iKey]) === false && typeof value[1] === 'object' && Array.isArray(value[1]) === false) { valueKey = ElementForUpdate(value[1], newElement[iKey], nullableAttributes, false); } else { valueKey = newElement[iKey]; } } else { valueKey = value[1]; } return ([ key, valueKey, ]); }))); return finalData; } exports.ElementForUpdate = ElementForUpdate; // read function _findAll(_kit, lang, query, progressive = false, _clean = { cleanData: (data) => data, }) { return __awaiter(this, void 0, void 0, function* () { return yield (0, error_1.ifInvalidUnknownError_list)(lang, () => __awaiter(this, void 0, void 0, function* () { progressive = (typeof progressive === 'boolean') ? progressive : false; query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } // clean query let pageSize = parseInt(query._pageSize); let page = parseInt(query._page); let order = query._order; let group = query._group; let attributes = query._attributes; if (Object.keys(query).includes('_page')) { delete query._page; } if (Object.keys(query).includes('_pageSize')) { delete query._pageSize; } if (Object.keys(query).includes('_order')) { delete query._order; } if (Object.keys(query).includes('_group')) { delete query._group; } if (Object.keys(query).includes('_attributes')) { delete query._attributes; } // pagination const metaPaginationTotal = yield _kit.readModel.count({ where: query, }); const exists = (metaPaginationTotal) > 0; const total = metaPaginationTotal; // pageSize = 3; pageSize = (config_1.pagesPossibles.includes(pageSize)) ? pageSize : config_1.pagesPossibles[0]; const takeAllDatas = (pageSize === -1); pageSize = (pageSize === -1) ? total : pageSize; const pageCount = Math.ceil(total / pageSize); page = (page >= 1 && page <= pageCount) ? page : 1; const offset = (progressive) ? 0 : ((page * pageSize) - pageSize); const limit = (progressive) ? (page * pageSize) : pageSize; // order order = ((typeof order === 'object' && Array.isArray(order) === true) || ['random'].includes(order)) ? order : undefined; if (order === 'random') { order = db_1.sequelizeInit.random(); } // group group = (typeof group === 'string' && group.length > 0) ? group : undefined; // attributes attributes = (typeof attributes === 'object' && Array.isArray(attributes) === true) ? attributes : undefined; let condDatas = { where: query, offset: offset, limit: limit, raw: true, nest: true, }; if (order) { condDatas.order = order; } if (group) { condDatas.group = group; } if (attributes) { condDatas.attributes = attributes; } // console.log(`> scripts.crud | findAll - pageCount:: `, pageCount); // console.log(`> scripts.crud | findAll - pageSize:: `, pageSize); // console.log(`> scripts.crud | findAll - page:: `, page); console.log(`> scripts.crud | findAll - order:: `, order); // console.log(`> scripts.crud | findAll - group:: `, group); // console.log(`> scripts.crud | findAll - attributes:: `, attributes); console.log(`> scripts.crud | findAll - condDatas:: `, condDatas); let datas = (yield _kit.readModel.findAll(condDatas)); datas = (typeof datas === 'object' && Array.isArray(datas) === true) ? datas : []; for (let indexAsync = 0; indexAsync < datas.length; indexAsync++) { const data = datas[indexAsync]; datas[indexAsync] = yield _clean.cleanData(data); } const pageLength = datas.length; const result = { datas: datas, meta: { pagination: { page: page, pageSize: ((!!takeAllDatas) ? -1 : pageSize), pageCount: pageCount, pageLength: pageLength, total: total, }, }, }; return result; })); }); } exports._findAll = _findAll; function _findOne(_kit, lang, query, _clean = { cleanData: (data) => data, }) { return __awaiter(this, void 0, void 0, function* () { return yield (0, error_1.ifInvalidUnknownError_find)(lang, () => __awaiter(this, void 0, void 0, function* () { query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } // clean query let order = query._order; let group = query._group; let attributes = query._attributes; if (Object.keys(query).includes('_order')) { delete query._order; } if (Object.keys(query).includes('_group')) { delete query._group; } if (Object.keys(query).includes('_attributes')) { delete query._attributes; } // order order = ((typeof order === 'object' && Array.isArray(order) === true) || ['random'].includes(order)) ? order : undefined; if (order === 'random') { order = db_1.sequelizeInit.random(); } // group group = (typeof group === 'string' && group.length > 0) ? group : undefined; // attributes attributes = (typeof attributes === 'object' && Array.isArray(attributes) === true) ? attributes : undefined; const FOFinalQuery = { where: query, }; if (order) { FOFinalQuery.order = order; } if (group) { FOFinalQuery.group = group; } if (attributes) { FOFinalQuery.attributes = attributes; } const exists = (yield _kit.readModel.count({ where: query, })) > 0; const result = { data: { data: (exists) ? (yield _clean.cleanData(yield _kit.readModel.findOne(FOFinalQuery))).toJSON() : undefined, exists: exists, }, }; return result; })); }); } exports._findOne = _findOne; // - function _exists(_kit, lang, query) { return __awaiter(this, void 0, void 0, function* () { return yield (0, error_1.ifInvalidUnknownError_exists)(lang, () => __awaiter(this, void 0, void 0, function* () { query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } const exists = (yield _kit.readModel.count({ where: query, })) > 0; return { exists, }; })); }); } exports._exists = _exists; // write multiple function _addMultiple(_kit, lang, queries, bodies, _clean = { cleanBody: (body) => body, cleanData: (data, exists, lang) => data, }, _supAction = (datas, bodies, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { queries = ((typeof queries === 'object' && Array.isArray(queries) === true) ? queries : []).map((query, indexBody) => ((typeof query === 'object' && Array.isArray(query) === false) ? query : {})).map((query, indexBody) => { if (Object.keys(query).includes('lang')) { delete query.lang; } return query; }); const query = { $or: queries, }; if (Object.keys(query).includes('lang')) { delete query.lang; } bodies = ((typeof bodies === 'object' && Array.isArray(bodies) === true) ? bodies : []).map((body, indexBody) => ((typeof body === 'object' && Array.isArray(body) === false) ? body : {})); let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { var _a; let res = { data: undefined, }; const form = yield _kit.form(lang); for (let indexAsync = 0; indexAsync < bodies.length; indexAsync++) { const body = bodies[indexAsync]; bodies[indexAsync] = yield _clean.cleanBody(body); } const validator = jon_1.default.Validator.validate(form, bodies); if (validator.valid) { const length = yield _kit.readModel.count({ where: query, }); const exists = (length === bodies.length); console.log(`> scripts.crud | addMultiple - length:: `, length); console.log(`> scripts.crud | addMultiple - exists:: `, exists); console.log(`> scripts.crud | addMultiple - query:: `, query); console.log(`> scripts.crud | addMultiple - queries:: `, queries); // console.log(`> scripts.crud | addMultiple - bodies:: `, bodies); if (exists) { res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.data_exists.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.data_exists.code, hexpress_config_json_1.default.errors.data_exists.message[(0, other_1.getLang)(lang)]).res; } else { let datas = validator.data; for (let indexAsync = 0; indexAsync < datas.length; indexAsync++) { const data = datas[indexAsync]; datas[indexAsync] = yield _clean.cleanData(data, exists, lang); } console.log(`> scripts.crud | addMultiple - datas:: `, datas); yield _kit.writeModel.bulkCreate(datas); res.data = (yield _kit.readModel.findAll({ where: query, })); res = yield _supAction(res.data, validator.data, true, lang, res); } } else { // console.log(`> scripts.crud | addMultiple - validator.error:: `, validator.error); res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.invalid_form.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.invalid_form.code, (_a = validator.error) === null || _a === void 0 ? void 0 : _a.message).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | addMultiple - res.error:: `, res.error); } return res; })); if (!(typeof result.data === 'object' && Array.isArray(result.data) === true)) { result.data = []; } return result; }); } exports._addMultiple = _addMultiple; function _updateMultiple(_kit, lang, queries, bodies, nullableAttributes = [], _clean = { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => ElementForUpdate(oldData, newData, nullableAttributes), }, _supAction = (datas, bodies, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { queries = ((typeof queries === 'object' && Array.isArray(queries) === true) ? queries : []).map((query, indexBody) => ((typeof query === 'object' && Array.isArray(query) === false) ? query : {})).map((query, indexBody) => { if (Object.keys(query).includes('lang')) { delete query.lang; } return query; }); const query = { $or: queries, }; bodies = ((typeof bodies === 'object' && Array.isArray(bodies) === true) ? bodies : []).map((body, indexBody) => ((typeof body === 'object' && Array.isArray(body) === false) ? body : {})); let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { var _a; let res = { data: undefined, }; const form = yield _kit.form(lang); for (let indexAsync = 0; indexAsync < bodies.length; indexAsync++) { const body = bodies[indexAsync]; bodies[indexAsync] = yield _clean.cleanBody(body); } const validator = jon_1.default.Validator.validate(form, bodies); console.log(`> scripts.crud | updateMultiple - bodies:: `, bodies); if (validator.valid) { const length = yield _kit.readModel.count({ where: query, }); const exists = (length === bodies.length); if (exists) { let datas = validator.data; datas = (typeof datas === 'object' && Array.isArray(datas) === true) ? datas : []; const finalDatas = yield db_1.sequelizeInit.transaction(function (t) { var _a; return __awaiter(this, void 0, void 0, function* () { let res = []; for (let index = 0; index < datas.length; index++) { const data = datas[index]; const element = (_a = (yield _kit.writeModel.findOne({ where: queries[index], transaction: t, }))) === null || _a === void 0 ? void 0 : _a.toJSON(); let finalData = yield _clean.cleanData(element, data, nullableAttributes, exists, lang); res.push(finalData); yield _kit.writeModel.update(finalData, { where: queries[index], }); } return res; }); }); res.data = finalDatas; res = yield _supAction(res.data, validator.data, exists, lang, res); console.log(`> scripts.crud | updateMultiple - datas:: `, datas); console.log(`> scripts.crud | updateMultiple - finalDatas:: `, finalDatas); } else { res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.data_doesnt_exists.code, hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)]).res; } } else { // console.log(`> scripts.crud | updateMultiple - validator.error:: `, validator.error); res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.invalid_form.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.invalid_form.code, (_a = validator.error) === null || _a === void 0 ? void 0 : _a.message).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | updateMultiple - res.error:: `, res.error); } return res; })); if (!(typeof result.data === 'object' && Array.isArray(result.data) === true)) { result.data = []; } return result; }); } exports._updateMultiple = _updateMultiple; function _editMultiple(_kit, lang, queries, bodies, nullableAttributes = [], _clean = { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => ElementForUpdate(oldData, newData, nullableAttributes), }, _supAction = (datas, bodies, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { queries = ((typeof queries === 'object' && Array.isArray(queries) === true) ? queries : []).map((query, indexBody) => ((typeof query === 'object' && Array.isArray(query) === false) ? query : {})).map((query, indexBody) => { if (Object.keys(query).includes('lang')) { delete query.lang; } return query; }); const query = { $or: queries, }; bodies = ((typeof bodies === 'object' && Array.isArray(bodies) === true) ? bodies : []).map((body, indexBody) => ((typeof body === 'object' && Array.isArray(body) === false) ? body : {})); let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { var _a; let res = { data: undefined, }; const form = yield _kit.form(lang); for (let indexAsync = 0; indexAsync < bodies.length; indexAsync++) { const body = bodies[indexAsync]; bodies[indexAsync] = yield _clean.cleanBody(body); } const validator = jon_1.default.Validator.validate(form, bodies); console.log(`> scripts.crud | editMultiple - bodies:: `, bodies); console.log(`> scripts.crud | editMultiple - query:: `, query); if (validator.valid) { const length = yield _kit.readModel.count({ where: query, }); let datas = validator.data; datas = (typeof datas === 'object' && Array.isArray(datas) === true) ? datas : []; console.log(`> scripts.crud | editMultiple - datas:: `, datas); const finalDatas = yield db_1.sequelizeInit.transaction(function (t) { var _a; return __awaiter(this, void 0, void 0, function* () { let res = []; for (let index = 0; index < datas.length; index++) { const exists = (yield _kit.readModel.count({ where: queries[index], })) > 0; const data = datas[index]; if (exists) { const element = (_a = (yield _kit.writeModel.findOne({ where: queries[index], transaction: t, }))) === null || _a === void 0 ? void 0 : _a.toJSON(); let finalData = yield _clean.cleanData(element, data, nullableAttributes, exists, lang); res.push(finalData); yield _kit.writeModel.update(finalData, { where: queries[index], }); } else { let finalData = yield _clean.cleanData(data, data, nullableAttributes, exists, lang); res.push((yield _kit.writeModel.create(finalData)).toJSON()); } } return res; }); }); res.data = finalDatas; res = yield _supAction(res.data, validator.data, undefined, lang, res); console.log(`> scripts.crud | editMultiple - datas:: `, datas); console.log(`> scripts.crud | editMultiple - finalDatas:: `, finalDatas); } else { // console.log(`> scripts.crud | editMultiple - validator.error:: `, validator.error); res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.invalid_form.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.invalid_form.code, (_a = validator.error) === null || _a === void 0 ? void 0 : _a.message).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | editMultiple - res.error:: `, res.error); } return res; })); if (!(typeof result.data === 'object' && Array.isArray(result.data) === true)) { result.data = []; } return result; }); } exports._editMultiple = _editMultiple; // delete multiple function _deleteMultiple(_kit, lang, queries, _supAction = (datas, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { queries = ((typeof queries === 'object' && Array.isArray(queries) === true) ? queries : []).map((query, indexBody) => ((typeof query === 'object' && Array.isArray(query) === false) ? query : {})).map((query, indexBody) => { if (Object.keys(query).includes('lang')) { delete query.lang; } return query; }); const query = { $or: queries, }; let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { let res = { data: undefined, }; const length = yield _kit.readModel.count({ where: query, }); const exists = (length === queries.length); if (exists) { const finalDatas = yield db_1.sequelizeInit.transaction(function (t) { return __awaiter(this, void 0, void 0, function* () { let res = []; for (let index = 0; index < queries.length; index++) { const finalData = (yield _kit.readModel.findOne({ where: queries[index], })).toJSON(); yield _kit.writeModel.destroy({ where: queries[index], }); res.push(finalData); } return res; }); }); res.data = finalDatas; res = yield _supAction(res.data, exists, lang, res); console.log(`> scripts.crud | updateMultiple - finalDatas:: `, finalDatas); } else { res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.data_doesnt_exists.code, hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)]).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | updateMultiple - res.error:: `, res.error); } return res; })); if (!(typeof result.data === 'object' && Array.isArray(result.data) === true)) { result.data = []; } return result; }); } exports._deleteMultiple = _deleteMultiple; function _archiveOrRestoreMultiple(_kit, lang, queries, nullableAttributes = [], _supAction = (datas, bodies, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { queries = (typeof queries === 'object' && Array.isArray(queries) === true) ? queries : []; return yield _updateMultiple(_kit, lang, queries, queries.map((query) => ({ status: 'archived', })), nullableAttributes, { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => { newData.status = (oldData.status === 'archived') ? 'visible' : 'archived'; const res = ElementForUpdate(oldData, newData, nullableAttributes); return res; }, }, _supAction); }); } exports._archiveOrRestoreMultiple = _archiveOrRestoreMultiple; // block or unblock multiple function _blockOrUnblockMultiple(_kit, lang, queries, nullableAttributes = [], _supAction = (datas, bodies, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { queries = (typeof queries === 'object' && Array.isArray(queries) === true) ? queries : []; return yield _updateMultiple(_kit, lang, queries, queries.map((query) => ({ blocked: true, })), nullableAttributes, { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => { newData.blocked = !oldData.blocked; const res = ElementForUpdate(oldData, newData, nullableAttributes); return res; }, }, _supAction); }); } exports._blockOrUnblockMultiple = _blockOrUnblockMultiple; // publish or unpublish multiple function _publishOrUnpublishMultiple(_kit, lang, queries, nullableAttributes = [], _supAction = (datas, bodies, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { queries = (typeof queries === 'object' && Array.isArray(queries) === true) ? queries : []; return yield _updateMultiple(_kit, lang, queries, queries.map((query) => ({ published: true, })), nullableAttributes, { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => { newData.published = !oldData.published; const res = ElementForUpdate(oldData, newData, nullableAttributes); return res; }, }, _supAction); }); } exports._publishOrUnpublishMultiple = _publishOrUnpublishMultiple; // write single function _add(_kit, lang, query, body, _clean = { cleanBody: (body) => body, cleanData: (data, exists, lang) => data, }, _supAction = (data, body, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } body = (typeof body === 'object' && Array.isArray(body) === false) ? body : {}; let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { var _a; let res = { data: undefined, }; const form = yield _kit.form(lang); console.log(`> scripts.crud | add - body:: `, body); body = yield _clean.cleanBody(body); const validator = jon_1.default.Validator.validate(form, body); if (validator.valid) { const exists = (yield _kit.readModel.count({ where: query, })) > 0; console.log(`> scripts.crud | exists:: `, exists); console.log(`> scripts.crud | query:: `, query); if (exists) { res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.data_exists.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.data_exists.code, hexpress_config_json_1.default.errors.data_exists.message[(0, other_1.getLang)(lang)]).res; } else { let data = validator.data; data = yield _clean.cleanData(data, exists, lang); console.log(`> scripts.crud | data:: `, data); res.data = (yield _kit.writeModel.create(data)).toJSON(); res = yield _supAction(res.data, validator.data, exists, lang, res); } } else { // console.log(`> scripts.crud | validator.error:: `, validator.error); res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.invalid_form.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.invalid_form.code, (_a = validator.error) === null || _a === void 0 ? void 0 : _a.message).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | res.error:: `, res.error); } return res; })); return result; }); } exports._add = _add; function _update(_kit, lang, query, body, nullableAttributes = [], _clean = { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => ElementForUpdate(oldData, newData, nullableAttributes), }, _supAction = (data, body, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } body = (typeof body === 'object' && Array.isArray(body) === false) ? body : {}; let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { var _a; let res = { data: undefined, }; const form = yield _kit.form(lang); console.log(`> scripts.crud | update - body:: `, body); body = yield _clean.cleanBody(body); const validator = jon_1.default.Validator.validate(form, body); if (validator.valid) { const exists = (yield _kit.readModel.count({ where: query, })) > 0; if (exists) { let data = validator.data; const element = (yield _kit.writeModel.findOne({ where: query, })).toJSON(); let finalData = yield _clean.cleanData(element, data, nullableAttributes, exists, lang); yield _kit.writeModel.update(finalData, { where: query, }); res.data = finalData; res = yield _supAction(res.data, validator.data, exists, lang, res); console.log(`> scripts.crud | update - data:: `, data); console.log(`> scripts.crud | update - finalData:: `, finalData); } else { res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.data_doesnt_exists.code, hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)]).res; } } else { // console.log(`> scripts.crud | update - validator.error:: `, validator.error); res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.invalid_form.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.invalid_form.code, (_a = validator.error) === null || _a === void 0 ? void 0 : _a.message).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | update - res.error:: `, res.error); } return res; })); return result; }); } exports._update = _update; function _edit(_kit, lang, query, body, nullableAttributes = [], _clean = { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => ElementForUpdate(oldData, newData, nullableAttributes), }, _supAction = (data, body, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } body = (typeof body === 'object' && Array.isArray(body) === false) ? body : {}; let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { var _a; let res = { data: undefined, }; const form = yield _kit.form(lang); console.log(`> scripts.crud | edit - body:: `, body); body = yield _clean.cleanBody(body); const validator = jon_1.default.Validator.validate(form, body); if (validator.valid) { const exists = (yield _kit.readModel.count({ where: query, })) > 0; console.log(`> scripts.crud | edit - exists:: `, exists); console.log(`> scripts.crud | edit - query:: `, query); let data = validator.data; console.log(`> scripts.crud | edit - data:: `, data); if (exists) { const element = (yield _kit.writeModel.findOne({ where: query, })).toJSON(); let finalData = yield _clean.cleanData(element, data, nullableAttributes, exists, lang); console.log(`> scripts.crud | edit - element:: `, element); console.log(`> scripts.crud | edit - finalData:: `, finalData); yield _kit.writeModel.update(finalData, { where: query, }); res.data = finalData; } else { data = yield _clean.cleanData(data, data, nullableAttributes, exists, lang); console.log(`> scripts.crud | edit - element:: `, data); res.data = (yield _kit.writeModel.create(data)).toJSON(); } res = yield _supAction(res.data, validator.data, exists, lang, res); } else { // console.log(`> scripts.crud | edit - validator.error:: `, validator.error); res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.invalid_form.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.invalid_form.code, (_a = validator.error) === null || _a === void 0 ? void 0 : _a.message).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | edit - res.error:: `, res.error); } return res; })); return result; }); } exports._edit = _edit; // delete function _delete(_kit, lang, query, _supAction = (data, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { let res = { data: undefined, }; const exists = (yield _kit.readModel.count({ where: query, })) > 0; if (exists) { res.data = (yield _kit.readModel.findOne({ where: query, })).toJSON(); yield _kit.writeModel.destroy({ where: query, }); res = yield _supAction(res.data, exists, lang, res); console.log(`> scripts.crud | delete - res:: `, res); } else { res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.data_doesnt_exists.code, hexpress_config_json_1.default.errors.data_doesnt_exists.message[(0, other_1.getLang)(lang)]).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | delete - res.error:: `, res.error); } return res; })); return result; }); } exports._delete = _delete; function _deleteOrAdd(_kit, lang, query, body, _clean = { cleanBody: (body) => body, cleanData: (data, exists, lang) => data, }, _supAction = (data, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { query = (typeof query === 'object' && Array.isArray(query) === false) ? query : {}; if (Object.keys(query).includes('lang')) { delete query.lang; } body = (typeof body === 'object' && Array.isArray(body) === false) ? body : {}; let result = {}; result = yield (0, error_1.ifInvalidUnknownError)((0, other_1.getLang)(lang), () => __awaiter(this, void 0, void 0, function* () { var _a; let res = { data: undefined, }; const form = yield _kit.form(lang); console.log(`> scripts.crud | add - body:: `, body); body = yield _clean.cleanBody(body); const validator = jon_1.default.Validator.validate(form, body); if (validator.valid) { const exists = (yield _kit.readModel.count({ where: query, })) > 0; console.log(`> scripts.crud | add - exists:: `, exists); console.log(`> scripts.crud | add - query:: `, query); if (exists) { res.data = (yield _kit.readModel.findOne({ where: query, })).toJSON(); yield _kit.writeModel.destroy({ where: query, }); } else { let data = validator.data; data = yield _clean.cleanData(data, exists, lang); console.log(`> scripts.crud | add - data:: `, data); res.data = (yield _kit.writeModel.create(data)).toJSON(); } res = yield _supAction(res.data, exists, lang, res); } else { // console.log(`> scripts.crud | add - validator.error:: `, validator.error); res.error = new error_2.CustomError(hexpress_config_json_1.default.errors.invalid_form.message[(0, other_1.getLang)(lang)], hexpress_config_json_1.default.errors.invalid_form.code, (_a = validator.error) === null || _a === void 0 ? void 0 : _a.message).res; } if (res.error) { console.log(`[ ERROR ]> scripts.crud | add - res.error:: `, res.error); } return res; })); return result; }); } exports._deleteOrAdd = _deleteOrAdd; function _archiveOrRestore(_kit, lang, query, nullableAttributes = [], _supAction = (data, body, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { return yield _update(_kit, lang, query, { status: 'archived', }, nullableAttributes, { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => { newData.status = (oldData.status === 'archived') ? 'visible' : 'archived'; const res = ElementForUpdate(oldData, newData, nullableAttributes); return res; }, }, _supAction); }); } exports._archiveOrRestore = _archiveOrRestore; // block function _blockOrUnblock(_kit, lang, query, nullableAttributes = [], _supAction = (data, body, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { return yield _update(_kit, lang, query, { blocked: true, }, nullableAttributes, { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes, exists, lang) => { newData.blocked = !oldData.blocked; const res = ElementForUpdate(oldData, newData, nullableAttributes); return res; }, }, _supAction); }); } exports._blockOrUnblock = _blockOrUnblock; // publish function _publishOrUnpublish(_kit, lang, query, nullableAttributes = [], _supAction = (data, body, exists, lang, res) => res) { return __awaiter(this, void 0, void 0, function* () { return yield _update(_kit, lang, query, { published: true, }, nullableAttributes, { cleanBody: (body) => body, cleanData: (oldData, newData, nullableAttributes,