init commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
EMAIL_ADDRESS_NOT_FOUND: 'Email Address Not Found',
|
||||
EMAIL_ADDRESS_ALREADY_EXIST: 'Email Address Already Exists',
|
||||
PASSWORD_NOT_MATCH: 'Password Does Not Match',
|
||||
INVALID_EMAIL_CONFIRMATION_CODE: 'Invalid Email Confirmation Code',
|
||||
INVALID_EMAIL_OR_PASSWORD: 'Invalid Email Or Password',
|
||||
USER_NOT_FOUND: 'User Not Found',
|
||||
};
|
||||
@@ -0,0 +1,76 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const sanitizeHtml = require('sanitize-html');
|
||||
module.exports = {
|
||||
filterEmptyFields(object) {
|
||||
Object.keys(object).forEach((key) => {
|
||||
if (this.empty(object[key])) {
|
||||
delete object[key];
|
||||
}
|
||||
});
|
||||
return object;
|
||||
},
|
||||
empty(value) {
|
||||
return value === '' || value === null || value === undefined;
|
||||
},
|
||||
getMappingKey(mappingFunction, value) {
|
||||
return Object.keys(mappingFunction()).find((key) => mappingFunction()[key].toLowerCase() === value.toLowerCase());
|
||||
},
|
||||
checkFor(requiredFields) {
|
||||
for (const [key, value] of Object.entries(requiredFields)) {
|
||||
if (!value) {
|
||||
throw new Error(`Must provide ${key}`);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
},
|
||||
convertToCents(amount) {
|
||||
return parseFloat(amount) * 100;
|
||||
},
|
||||
convertFromCents(amount) {
|
||||
return parseFloat(amount) / 100;
|
||||
},
|
||||
inject_substitute(text, normalKey, value) {
|
||||
text = text.replace(new RegExp('{{{' + normalKey + '}}}', 'g'), value);
|
||||
return text;
|
||||
},
|
||||
createDirectoriesRecursive(filePath) {
|
||||
let fileDirectoryPath = path.dirname(filePath);
|
||||
if (!fs.existsSync(fileDirectoryPath)) {
|
||||
fs.mkdirSync(fileDirectoryPath, { recursive: true });
|
||||
}
|
||||
},
|
||||
createDirectoriesRecursiveV2(folderPath) {
|
||||
if (!fs.existsSync(folderPath)) {
|
||||
fs.mkdirSync(folderPath, { recursive: true });
|
||||
}
|
||||
},
|
||||
sanitizeInputs(body) {
|
||||
if (Array.isArray(body)) {
|
||||
body.forEach((item) => {
|
||||
item = sanitizeHtml(item);
|
||||
});
|
||||
return body;
|
||||
}
|
||||
if (this.isObject(body)) {
|
||||
Object.keys(body).forEach((key) => {
|
||||
body[key] = sanitizeHtml(body[key]);
|
||||
});
|
||||
return body;
|
||||
}
|
||||
return sanitizeHtml(body);
|
||||
},
|
||||
isObject(obj) {
|
||||
return obj === Object(obj);
|
||||
},
|
||||
ucFirst(string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
},
|
||||
getLocalPath(string) {
|
||||
if (string.includes('\\')) {
|
||||
return string.split('\\public')[1].replace(/\\/g, '/');
|
||||
} else {
|
||||
return string.split('/public')[1];
|
||||
}
|
||||
},
|
||||
};
|
||||
+313
@@ -0,0 +1,313 @@
|
||||
const { isInteger } = require("lodash");
|
||||
|
||||
module.exports = async function (Table) {
|
||||
Table._primaryKey = function () {
|
||||
return "id";
|
||||
};
|
||||
Table.getLast = async function (parameters) {
|
||||
let where = parameters;
|
||||
if (!where) {
|
||||
where = {};
|
||||
}
|
||||
for (const key in where) {
|
||||
const element = where[key];
|
||||
if (element == undefined || element == null) {
|
||||
delete where.key;
|
||||
}
|
||||
}
|
||||
result = await Table.findAll({
|
||||
limit: 1,
|
||||
where: where,
|
||||
order: [["id", "DESC"]],
|
||||
});
|
||||
return result[0];
|
||||
};
|
||||
Table.getAll = function (parameters, orderBy = Table._primaryKey(), direction = "ASC", orderAssociations, include = []) {
|
||||
let where = parameters;
|
||||
if (!where) {
|
||||
where = {};
|
||||
}
|
||||
for (const key in where) {
|
||||
const element = where[key];
|
||||
if (element == undefined || element == null) {
|
||||
delete where.key;
|
||||
}
|
||||
}
|
||||
|
||||
let params = {
|
||||
where: where,
|
||||
include: include,
|
||||
distinct: true,
|
||||
order: [[orderBy, direction]],
|
||||
};
|
||||
|
||||
if (Array.isArray(orderAssociations) && orderAssociations.length > 0) {
|
||||
orderAssociations.forEach(function (order) {
|
||||
params.order[0].unshift(order);
|
||||
});
|
||||
}
|
||||
return Table.findAll(params);
|
||||
};
|
||||
Table._count = function (parameters, include = []) {
|
||||
let where = parameters;
|
||||
if (!where) {
|
||||
where = {};
|
||||
}
|
||||
for (const key in where) {
|
||||
const element = where[key];
|
||||
if (element == undefined || element == null) {
|
||||
delete where.key;
|
||||
}
|
||||
}
|
||||
|
||||
Table._customCountingConditions(where);
|
||||
|
||||
return Table.count({
|
||||
where: where,
|
||||
include: include,
|
||||
distinct: true,
|
||||
});
|
||||
};
|
||||
|
||||
Table.getPaginated = function (page, limit, parameters, orderBy, direction, orderAssociations = [], include = []) {
|
||||
let where = parameters;
|
||||
|
||||
for (const key in where) {
|
||||
if (where.hasOwnProperty.call(where, key)) {
|
||||
const element = where[key];
|
||||
if (element.length || isInteger(element) || element === Object(element)) {
|
||||
where[key] = element;
|
||||
} else {
|
||||
delete where[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!page) {
|
||||
page = 0;
|
||||
}
|
||||
if (!limit) {
|
||||
limit = 10;
|
||||
}
|
||||
if (!where) {
|
||||
where = {};
|
||||
}
|
||||
if (!orderBy) {
|
||||
orderBy = Table._primaryKey();
|
||||
}
|
||||
if (!direction) {
|
||||
direction = "ASC";
|
||||
}
|
||||
|
||||
for (const key in where) {
|
||||
const element = where[key];
|
||||
if (element == undefined || element == null) {
|
||||
delete where.key;
|
||||
}
|
||||
}
|
||||
let params = {
|
||||
where: where,
|
||||
offset: page * limit,
|
||||
limit: limit,
|
||||
include: include,
|
||||
order: [[orderBy, direction]],
|
||||
distinct: true,
|
||||
};
|
||||
if (Array.isArray(orderAssociations) && orderAssociations.length > 0) {
|
||||
orderAssociations.forEach(function (order) {
|
||||
params.order[0].unshift(order);
|
||||
});
|
||||
}
|
||||
return Table.findAll(params);
|
||||
};
|
||||
Table.getPaginatedV2 = function (page, limit, parameters, order, direction, include = []) {
|
||||
let where = parameters;
|
||||
|
||||
for (const key in where) {
|
||||
if (where.hasOwnProperty.call(where, key)) {
|
||||
const element = where[key];
|
||||
if (element.length || isInteger(element) || element === Object(element)) {
|
||||
where[key] = element;
|
||||
} else {
|
||||
delete where[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!page) {
|
||||
page = 0;
|
||||
}
|
||||
if (!limit) {
|
||||
limit = 10;
|
||||
}
|
||||
if (!where) {
|
||||
where = {};
|
||||
}
|
||||
if (!order) {
|
||||
order = Table._primaryKey();
|
||||
}
|
||||
if (!direction) {
|
||||
direction = "ASC";
|
||||
}
|
||||
|
||||
for (const key in where) {
|
||||
const element = where[key];
|
||||
if (element == undefined || element == null) {
|
||||
delete where.key;
|
||||
}
|
||||
}
|
||||
let params = {
|
||||
where: where,
|
||||
offset: page * limit,
|
||||
limit: limit,
|
||||
include: include,
|
||||
order: order,
|
||||
distinct: true,
|
||||
};
|
||||
return Table.findAll(params);
|
||||
};
|
||||
Table.getAllByStatus = function (status) {
|
||||
return Table.findAll({
|
||||
where: {
|
||||
status: status,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Table.getByField = function (field, value) {
|
||||
return Table.findOne({
|
||||
where: {
|
||||
[field]: value,
|
||||
},
|
||||
distinct: true,
|
||||
});
|
||||
};
|
||||
|
||||
Table.getByPK = async function (id, options) {
|
||||
return await Table.findByPk(id, options);
|
||||
};
|
||||
|
||||
Table.getByFields = function (parameters) {
|
||||
let where = parameters;
|
||||
if (!where) {
|
||||
where = {};
|
||||
}
|
||||
for (const key in where) {
|
||||
const element = where[key];
|
||||
if (element == undefined || element == null) {
|
||||
delete where.key;
|
||||
}
|
||||
}
|
||||
|
||||
return Table.findOne({
|
||||
where: where,
|
||||
});
|
||||
};
|
||||
|
||||
Table.getAllByKeyValue = async function (field, parameters) {
|
||||
let where = parameters;
|
||||
if (!where) {
|
||||
where = {};
|
||||
}
|
||||
let data = [];
|
||||
const results = await Table.findAll({
|
||||
where: where,
|
||||
});
|
||||
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
const element = results[i];
|
||||
let singleField = element[field];
|
||||
data.push({ [element.id]: singleField });
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
Table.insert = async function (data, { returnAllFields = false } = {}) {
|
||||
data = Table._preCreateProcessing(data);
|
||||
const insertedRow = await Table.create(Table._filterAllowKeys(data));
|
||||
if (returnAllFields === true) {
|
||||
return insertedRow;
|
||||
}
|
||||
if (insertedRow) {
|
||||
return insertedRow.id;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
Table.batchInsert = async function (data) {
|
||||
const insertedRow = await Table.bulkCreate(data, { returning: true });
|
||||
|
||||
if (insertedRow) {
|
||||
return insertedRow;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
Table.edit = async function (data, id) {
|
||||
data = Table._postCreateProcessing(data);
|
||||
const updateRow = await Table.update(Table._filterAllowKeys(data), {
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
}).catch((error) => {
|
||||
throw new Error(error);
|
||||
});
|
||||
|
||||
return updateRow;
|
||||
};
|
||||
|
||||
Table.editByField = async function (data, where) {
|
||||
data = Table._postCreateProcessing(data);
|
||||
|
||||
const updateRow = await Table.update(Table._filterAllowKeys(data), {
|
||||
where,
|
||||
});
|
||||
|
||||
return updateRow;
|
||||
};
|
||||
|
||||
Table.delete = async function (id) {
|
||||
const updateRow = await Table.update(
|
||||
{
|
||||
status: 0,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
}
|
||||
);
|
||||
return updateRow;
|
||||
};
|
||||
|
||||
Table.realDelete = async function (id) {
|
||||
return Table.destroy({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
Table.realDeleteByFields = async function (parameters, id) {
|
||||
let where = parameters;
|
||||
if (!where) {
|
||||
where = {
|
||||
id: id,
|
||||
};
|
||||
} else {
|
||||
where["id"] = id;
|
||||
}
|
||||
return Table.destroy({
|
||||
where: where,
|
||||
});
|
||||
};
|
||||
Table.realDeleteByUniqueField = async function (field, value) {
|
||||
let where = {};
|
||||
where[field] = value;
|
||||
return Table.destroy({
|
||||
where: where,
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
exports.validateEmail = (email) => {
|
||||
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
|
||||
const reStartAndEnd = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/g;
|
||||
|
||||
if (re.test(email) && reStartAndEnd.test(email)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
Reference in New Issue
Block a user