38788-vm/backend/src/db/api/work_items.js
2026-02-26 15:28:15 +00:00

689 lines
17 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 Work_itemsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const work_items = await db.work_items.create(
{
id: data.id || undefined,
external_key: data.external_key
||
null
,
title: data.title
||
null
,
item_type: data.item_type
||
null
,
status: data.status
||
null
,
estimate_points: data.estimate_points
||
null
,
created_at_source: data.created_at_source
||
null
,
started_at: data.started_at
||
null
,
done_at: data.done_at
||
null
,
assignee: data.assignee
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await work_items.setProject( data.project || null, {
transaction,
});
await work_items.setSprint( data.sprint || null, {
transaction,
});
await work_items.setSource_import( data.source_import || null, {
transaction,
});
return work_items;
}
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 work_itemsData = data.map((item, index) => ({
id: item.id || undefined,
external_key: item.external_key
||
null
,
title: item.title
||
null
,
item_type: item.item_type
||
null
,
status: item.status
||
null
,
estimate_points: item.estimate_points
||
null
,
created_at_source: item.created_at_source
||
null
,
started_at: item.started_at
||
null
,
done_at: item.done_at
||
null
,
assignee: item.assignee
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const work_items = await db.work_items.bulkCreate(work_itemsData, { transaction });
// For each item created, replace relation files
return work_items;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const work_items = await db.work_items.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.external_key !== undefined) updatePayload.external_key = data.external_key;
if (data.title !== undefined) updatePayload.title = data.title;
if (data.item_type !== undefined) updatePayload.item_type = data.item_type;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.estimate_points !== undefined) updatePayload.estimate_points = data.estimate_points;
if (data.created_at_source !== undefined) updatePayload.created_at_source = data.created_at_source;
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
if (data.done_at !== undefined) updatePayload.done_at = data.done_at;
if (data.assignee !== undefined) updatePayload.assignee = data.assignee;
updatePayload.updatedById = currentUser.id;
await work_items.update(updatePayload, {transaction});
if (data.project !== undefined) {
await work_items.setProject(
data.project,
{ transaction }
);
}
if (data.sprint !== undefined) {
await work_items.setSprint(
data.sprint,
{ transaction }
);
}
if (data.source_import !== undefined) {
await work_items.setSource_import(
data.source_import,
{ transaction }
);
}
return work_items;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const work_items = await db.work_items.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of work_items) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of work_items) {
await record.destroy({transaction});
}
});
return work_items;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const work_items = await db.work_items.findByPk(id, options);
await work_items.update({
deletedBy: currentUser.id
}, {
transaction,
});
await work_items.destroy({
transaction
});
return work_items;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const work_items = await db.work_items.findOne(
{ where },
{ transaction },
);
if (!work_items) {
return work_items;
}
const output = work_items.get({plain: true});
output.work_item_events_work_item = await work_items.getWork_item_events_work_item({
transaction
});
output.project = await work_items.getProject({
transaction
});
output.sprint = await work_items.getSprint({
transaction
});
output.source_import = await work_items.getSource_import({
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.projects,
as: 'project',
where: filter.project ? {
[Op.or]: [
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.sprints,
as: 'sprint',
where: filter.sprint ? {
[Op.or]: [
{ id: { [Op.in]: filter.sprint.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.sprint.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.csv_imports,
as: 'source_import',
where: filter.source_import ? {
[Op.or]: [
{ id: { [Op.in]: filter.source_import.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.source_import.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.external_key) {
where = {
...where,
[Op.and]: Utils.ilike(
'work_items',
'external_key',
filter.external_key,
),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'work_items',
'title',
filter.title,
),
};
}
if (filter.assignee) {
where = {
...where,
[Op.and]: Utils.ilike(
'work_items',
'assignee',
filter.assignee,
),
};
}
if (filter.estimate_pointsRange) {
const [start, end] = filter.estimate_pointsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
estimate_points: {
...where.estimate_points,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
estimate_points: {
...where.estimate_points,
[Op.lte]: end,
},
};
}
}
if (filter.created_at_sourceRange) {
const [start, end] = filter.created_at_sourceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
created_at_source: {
...where.created_at_source,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
created_at_source: {
...where.created_at_source,
[Op.lte]: end,
},
};
}
}
if (filter.started_atRange) {
const [start, end] = filter.started_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.lte]: end,
},
};
}
}
if (filter.done_atRange) {
const [start, end] = filter.done_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
done_at: {
...where.done_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
done_at: {
...where.done_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.item_type) {
where = {
...where,
item_type: filter.item_type,
};
}
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.work_items.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(
'work_items',
'title',
query,
),
],
};
}
const records = await db.work_items.findAll({
attributes: [ 'id', 'title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title,
}));
}
};