649 lines
16 KiB
JavaScript
649 lines
16 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class DeploymentsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deployments = await db.deployments.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
target_platform: data.target_platform
|
|
||
|
|
null
|
|
,
|
|
|
|
environment: data.environment
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
deployed_url: data.deployed_url
|
|
||
|
|
null
|
|
,
|
|
|
|
build_version: data.build_version
|
|
||
|
|
null
|
|
,
|
|
|
|
build_started_at: data.build_started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
build_finished_at: data.build_finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
deployed_at: data.deployed_at
|
|
||
|
|
null
|
|
,
|
|
|
|
build_log: data.build_log
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await deployments.setProject( data.project || null, {
|
|
transaction,
|
|
});
|
|
|
|
await deployments.setTriggered_by( data.triggered_by || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return deployments;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const deploymentsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
target_platform: item.target_platform
|
|
||
|
|
null
|
|
,
|
|
|
|
environment: item.environment
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
deployed_url: item.deployed_url
|
|
||
|
|
null
|
|
,
|
|
|
|
build_version: item.build_version
|
|
||
|
|
null
|
|
,
|
|
|
|
build_started_at: item.build_started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
build_finished_at: item.build_finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
deployed_at: item.deployed_at
|
|
||
|
|
null
|
|
,
|
|
|
|
build_log: item.build_log
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const deployments = await db.deployments.bulkCreate(deploymentsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return deployments;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const deployments = await db.deployments.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.target_platform !== undefined) updatePayload.target_platform = data.target_platform;
|
|
|
|
|
|
if (data.environment !== undefined) updatePayload.environment = data.environment;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.deployed_url !== undefined) updatePayload.deployed_url = data.deployed_url;
|
|
|
|
|
|
if (data.build_version !== undefined) updatePayload.build_version = data.build_version;
|
|
|
|
|
|
if (data.build_started_at !== undefined) updatePayload.build_started_at = data.build_started_at;
|
|
|
|
|
|
if (data.build_finished_at !== undefined) updatePayload.build_finished_at = data.build_finished_at;
|
|
|
|
|
|
if (data.deployed_at !== undefined) updatePayload.deployed_at = data.deployed_at;
|
|
|
|
|
|
if (data.build_log !== undefined) updatePayload.build_log = data.build_log;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await deployments.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.project !== undefined) {
|
|
await deployments.setProject(
|
|
|
|
data.project,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.triggered_by !== undefined) {
|
|
await deployments.setTriggered_by(
|
|
|
|
data.triggered_by,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return deployments;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deployments = await db.deployments.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of deployments) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of deployments) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return deployments;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deployments = await db.deployments.findByPk(id, options);
|
|
|
|
await deployments.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await deployments.destroy({
|
|
transaction
|
|
});
|
|
|
|
return deployments;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deployments = await db.deployments.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!deployments) {
|
|
return deployments;
|
|
}
|
|
|
|
const output = deployments.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.project = await deployments.getProject({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.triggered_by = await deployments.getTriggered_by({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.html_projects,
|
|
as: 'project',
|
|
|
|
where: filter.project ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
project_name: {
|
|
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'triggered_by',
|
|
|
|
where: filter.triggered_by ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.triggered_by.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.triggered_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.deployed_url) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'deployments',
|
|
'deployed_url',
|
|
filter.deployed_url,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.build_version) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'deployments',
|
|
'build_version',
|
|
filter.build_version,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.build_log) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'deployments',
|
|
'build_log',
|
|
filter.build_log,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
build_started_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
deployed_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.build_started_atRange) {
|
|
const [start, end] = filter.build_started_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
build_started_at: {
|
|
...where.build_started_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
build_started_at: {
|
|
...where.build_started_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.build_finished_atRange) {
|
|
const [start, end] = filter.build_finished_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
build_finished_at: {
|
|
...where.build_finished_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
build_finished_at: {
|
|
...where.build_finished_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.deployed_atRange) {
|
|
const [start, end] = filter.deployed_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
deployed_at: {
|
|
...where.deployed_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
deployed_at: {
|
|
...where.deployed_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.target_platform) {
|
|
where = {
|
|
...where,
|
|
target_platform: filter.target_platform,
|
|
};
|
|
}
|
|
|
|
if (filter.environment) {
|
|
where = {
|
|
...where,
|
|
environment: filter.environment,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.deployments.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'deployments',
|
|
'build_version',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.deployments.findAll({
|
|
attributes: [ 'id', 'build_version' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['build_version', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.build_version,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|