37749-vm/backend/src/db/api/smtp_integrations.js
2026-01-23 14:53:19 +00:00

583 lines
14 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 Smtp_integrationsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const smtp_integrations = await db.smtp_integrations.create(
{
id: data.id || undefined,
name: data.name
||
null
,
provider: data.provider
||
null
,
host: data.host
||
null
,
port: data.port
||
null
,
username: data.username
||
null
,
from_email: data.from_email
||
null
,
is_verified: data.is_verified
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await smtp_integrations.setOwner( data.owner || null, {
transaction,
});
await smtp_integrations.setOrganizations( data.organizations || null, {
transaction,
});
return smtp_integrations;
}
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 smtp_integrationsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
provider: item.provider
||
null
,
host: item.host
||
null
,
port: item.port
||
null
,
username: item.username
||
null
,
from_email: item.from_email
||
null
,
is_verified: item.is_verified
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const smtp_integrations = await db.smtp_integrations.bulkCreate(smtp_integrationsData, { transaction });
// For each item created, replace relation files
return smtp_integrations;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const globalAccess = currentUser.app_role?.globalAccess;
const smtp_integrations = await db.smtp_integrations.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.provider !== undefined) updatePayload.provider = data.provider;
if (data.host !== undefined) updatePayload.host = data.host;
if (data.port !== undefined) updatePayload.port = data.port;
if (data.username !== undefined) updatePayload.username = data.username;
if (data.from_email !== undefined) updatePayload.from_email = data.from_email;
if (data.is_verified !== undefined) updatePayload.is_verified = data.is_verified;
updatePayload.updatedById = currentUser.id;
await smtp_integrations.update(updatePayload, {transaction});
if (data.owner !== undefined) {
await smtp_integrations.setOwner(
data.owner,
{ transaction }
);
}
if (data.organizations !== undefined) {
await smtp_integrations.setOrganizations(
data.organizations,
{ transaction }
);
}
return smtp_integrations;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const smtp_integrations = await db.smtp_integrations.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of smtp_integrations) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of smtp_integrations) {
await record.destroy({transaction});
}
});
return smtp_integrations;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const smtp_integrations = await db.smtp_integrations.findByPk(id, options);
await smtp_integrations.update({
deletedBy: currentUser.id
}, {
transaction,
});
await smtp_integrations.destroy({
transaction
});
return smtp_integrations;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const smtp_integrations = await db.smtp_integrations.findOne(
{ where },
{ transaction },
);
if (!smtp_integrations) {
return smtp_integrations;
}
const output = smtp_integrations.get({plain: true});
output.owner = await smtp_integrations.getOwner({
transaction
});
output.organizations = await smtp_integrations.getOrganizations({
transaction
});
return output;
}
static async findAll(
filter,
globalAccess, options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
const user = (options && options.currentUser) || null;
const userOrganizations = (user && user.organizations?.id) || null;
if (userOrganizations) {
if (options?.currentUser?.organizationsId) {
where.organizationsId = options.currentUser.organizationsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.users,
as: 'owner',
where: filter.owner ? {
[Op.or]: [
{ id: { [Op.in]: filter.owner.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.owner.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.organizations,
as: 'organizations',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'smtp_integrations',
'name',
filter.name,
),
};
}
if (filter.host) {
where = {
...where,
[Op.and]: Utils.ilike(
'smtp_integrations',
'host',
filter.host,
),
};
}
if (filter.username) {
where = {
...where,
[Op.and]: Utils.ilike(
'smtp_integrations',
'username',
filter.username,
),
};
}
if (filter.from_email) {
where = {
...where,
[Op.and]: Utils.ilike(
'smtp_integrations',
'from_email',
filter.from_email,
),
};
}
if (filter.portRange) {
const [start, end] = filter.portRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
port: {
...where.port,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
port: {
...where.port,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.provider) {
where = {
...where,
provider: filter.provider,
};
}
if (filter.is_verified) {
where = {
...where,
is_verified: filter.is_verified,
};
}
if (filter.organizations) {
const listItems = filter.organizations.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationsId: {[Op.or]: listItems}
};
}
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,
},
};
}
}
}
if (globalAccess) {
delete where.organizationsId;
}
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.smtp_integrations.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, globalAccess, organizationId,) {
let where = {};
if (!globalAccess && organizationId) {
where.organizationId = organizationId;
}
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'smtp_integrations',
'name',
query,
),
],
};
}
const records = await db.smtp_integrations.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};