558 lines
13 KiB
JavaScript
558 lines
13 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 User_settingsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const user_settings = await db.user_settings.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
push_enabled: data.push_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
email_enabled: data.email_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
sms_enabled: data.sms_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
theme_preference: data.theme_preference
|
|
||
|
|
null
|
|
,
|
|
|
|
language_preference: data.language_preference
|
|
||
|
|
null
|
|
,
|
|
|
|
show_sensitive_content: data.show_sensitive_content
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
allow_message_requests: data.allow_message_requests
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
read_receipts_enabled: data.read_receipts_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
activity_status_visible: data.activity_status_visible
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await user_settings.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return user_settings;
|
|
}
|
|
|
|
|
|
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 user_settingsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
push_enabled: item.push_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
email_enabled: item.email_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
sms_enabled: item.sms_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
theme_preference: item.theme_preference
|
|
||
|
|
null
|
|
,
|
|
|
|
language_preference: item.language_preference
|
|
||
|
|
null
|
|
,
|
|
|
|
show_sensitive_content: item.show_sensitive_content
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
allow_message_requests: item.allow_message_requests
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
read_receipts_enabled: item.read_receipts_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
activity_status_visible: item.activity_status_visible
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const user_settings = await db.user_settings.bulkCreate(user_settingsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return user_settings;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const user_settings = await db.user_settings.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.push_enabled !== undefined) updatePayload.push_enabled = data.push_enabled;
|
|
|
|
|
|
if (data.email_enabled !== undefined) updatePayload.email_enabled = data.email_enabled;
|
|
|
|
|
|
if (data.sms_enabled !== undefined) updatePayload.sms_enabled = data.sms_enabled;
|
|
|
|
|
|
if (data.theme_preference !== undefined) updatePayload.theme_preference = data.theme_preference;
|
|
|
|
|
|
if (data.language_preference !== undefined) updatePayload.language_preference = data.language_preference;
|
|
|
|
|
|
if (data.show_sensitive_content !== undefined) updatePayload.show_sensitive_content = data.show_sensitive_content;
|
|
|
|
|
|
if (data.allow_message_requests !== undefined) updatePayload.allow_message_requests = data.allow_message_requests;
|
|
|
|
|
|
if (data.read_receipts_enabled !== undefined) updatePayload.read_receipts_enabled = data.read_receipts_enabled;
|
|
|
|
|
|
if (data.activity_status_visible !== undefined) updatePayload.activity_status_visible = data.activity_status_visible;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await user_settings.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await user_settings.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return user_settings;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const user_settings = await db.user_settings.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of user_settings) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of user_settings) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return user_settings;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const user_settings = await db.user_settings.findByPk(id, options);
|
|
|
|
await user_settings.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await user_settings.destroy({
|
|
transaction
|
|
});
|
|
|
|
return user_settings;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const user_settings = await db.user_settings.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!user_settings) {
|
|
return user_settings;
|
|
}
|
|
|
|
const output = user_settings.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await user_settings.getUser({
|
|
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.users,
|
|
as: 'user',
|
|
|
|
where: filter.user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.push_enabled) {
|
|
where = {
|
|
...where,
|
|
push_enabled: filter.push_enabled,
|
|
};
|
|
}
|
|
|
|
if (filter.email_enabled) {
|
|
where = {
|
|
...where,
|
|
email_enabled: filter.email_enabled,
|
|
};
|
|
}
|
|
|
|
if (filter.sms_enabled) {
|
|
where = {
|
|
...where,
|
|
sms_enabled: filter.sms_enabled,
|
|
};
|
|
}
|
|
|
|
if (filter.theme_preference) {
|
|
where = {
|
|
...where,
|
|
theme_preference: filter.theme_preference,
|
|
};
|
|
}
|
|
|
|
if (filter.language_preference) {
|
|
where = {
|
|
...where,
|
|
language_preference: filter.language_preference,
|
|
};
|
|
}
|
|
|
|
if (filter.show_sensitive_content) {
|
|
where = {
|
|
...where,
|
|
show_sensitive_content: filter.show_sensitive_content,
|
|
};
|
|
}
|
|
|
|
if (filter.allow_message_requests) {
|
|
where = {
|
|
...where,
|
|
allow_message_requests: filter.allow_message_requests,
|
|
};
|
|
}
|
|
|
|
if (filter.read_receipts_enabled) {
|
|
where = {
|
|
...where,
|
|
read_receipts_enabled: filter.read_receipts_enabled,
|
|
};
|
|
}
|
|
|
|
if (filter.activity_status_visible) {
|
|
where = {
|
|
...where,
|
|
activity_status_visible: filter.activity_status_visible,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.user_settings.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(
|
|
'user_settings',
|
|
'theme_preference',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.user_settings.findAll({
|
|
attributes: [ 'id', 'theme_preference' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['theme_preference', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.theme_preference,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|