38781-vm/backend/src/db/api/camera_profiles.js
2026-02-26 05:35:15 +00:00

812 lines
21 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 Camera_profilesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const camera_profiles = await db.camera_profiles.create(
{
id: data.id || undefined,
profile_name: data.profile_name
||
null
,
capture_mode: data.capture_mode
||
null
,
digital_zoom: data.digital_zoom
||
null
,
exposure_compensation: data.exposure_compensation
||
null
,
iso: data.iso
||
null
,
focus_distance: data.focus_distance
||
null
,
white_balance_kelvin: data.white_balance_kelvin
||
null
,
sharpness: data.sharpness
||
null
,
denoise: data.denoise
||
null
,
stabilization_enabled: data.stabilization_enabled
||
false
,
torch_enabled: data.torch_enabled
||
false
,
hdr_enabled: data.hdr_enabled
||
false
,
grid_enabled: data.grid_enabled
||
false
,
crosshair_enabled: data.crosshair_enabled
||
false
,
object_labels_enabled: data.object_labels_enabled
||
false
,
is_default: data.is_default
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await camera_profiles.setUser( data.user || null, {
transaction,
});
return camera_profiles;
}
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 camera_profilesData = data.map((item, index) => ({
id: item.id || undefined,
profile_name: item.profile_name
||
null
,
capture_mode: item.capture_mode
||
null
,
digital_zoom: item.digital_zoom
||
null
,
exposure_compensation: item.exposure_compensation
||
null
,
iso: item.iso
||
null
,
focus_distance: item.focus_distance
||
null
,
white_balance_kelvin: item.white_balance_kelvin
||
null
,
sharpness: item.sharpness
||
null
,
denoise: item.denoise
||
null
,
stabilization_enabled: item.stabilization_enabled
||
false
,
torch_enabled: item.torch_enabled
||
false
,
hdr_enabled: item.hdr_enabled
||
false
,
grid_enabled: item.grid_enabled
||
false
,
crosshair_enabled: item.crosshair_enabled
||
false
,
object_labels_enabled: item.object_labels_enabled
||
false
,
is_default: item.is_default
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const camera_profiles = await db.camera_profiles.bulkCreate(camera_profilesData, { transaction });
// For each item created, replace relation files
return camera_profiles;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const camera_profiles = await db.camera_profiles.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.profile_name !== undefined) updatePayload.profile_name = data.profile_name;
if (data.capture_mode !== undefined) updatePayload.capture_mode = data.capture_mode;
if (data.digital_zoom !== undefined) updatePayload.digital_zoom = data.digital_zoom;
if (data.exposure_compensation !== undefined) updatePayload.exposure_compensation = data.exposure_compensation;
if (data.iso !== undefined) updatePayload.iso = data.iso;
if (data.focus_distance !== undefined) updatePayload.focus_distance = data.focus_distance;
if (data.white_balance_kelvin !== undefined) updatePayload.white_balance_kelvin = data.white_balance_kelvin;
if (data.sharpness !== undefined) updatePayload.sharpness = data.sharpness;
if (data.denoise !== undefined) updatePayload.denoise = data.denoise;
if (data.stabilization_enabled !== undefined) updatePayload.stabilization_enabled = data.stabilization_enabled;
if (data.torch_enabled !== undefined) updatePayload.torch_enabled = data.torch_enabled;
if (data.hdr_enabled !== undefined) updatePayload.hdr_enabled = data.hdr_enabled;
if (data.grid_enabled !== undefined) updatePayload.grid_enabled = data.grid_enabled;
if (data.crosshair_enabled !== undefined) updatePayload.crosshair_enabled = data.crosshair_enabled;
if (data.object_labels_enabled !== undefined) updatePayload.object_labels_enabled = data.object_labels_enabled;
if (data.is_default !== undefined) updatePayload.is_default = data.is_default;
updatePayload.updatedById = currentUser.id;
await camera_profiles.update(updatePayload, {transaction});
if (data.user !== undefined) {
await camera_profiles.setUser(
data.user,
{ transaction }
);
}
return camera_profiles;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const camera_profiles = await db.camera_profiles.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of camera_profiles) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of camera_profiles) {
await record.destroy({transaction});
}
});
return camera_profiles;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const camera_profiles = await db.camera_profiles.findByPk(id, options);
await camera_profiles.update({
deletedBy: currentUser.id
}, {
transaction,
});
await camera_profiles.destroy({
transaction
});
return camera_profiles;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const camera_profiles = await db.camera_profiles.findOne(
{ where },
{ transaction },
);
if (!camera_profiles) {
return camera_profiles;
}
const output = camera_profiles.get({plain: true});
output.observation_sessions_camera_profile = await camera_profiles.getObservation_sessions_camera_profile({
transaction
});
output.user = await camera_profiles.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.profile_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'camera_profiles',
'profile_name',
filter.profile_name,
),
};
}
if (filter.digital_zoomRange) {
const [start, end] = filter.digital_zoomRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
digital_zoom: {
...where.digital_zoom,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
digital_zoom: {
...where.digital_zoom,
[Op.lte]: end,
},
};
}
}
if (filter.exposure_compensationRange) {
const [start, end] = filter.exposure_compensationRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
exposure_compensation: {
...where.exposure_compensation,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
exposure_compensation: {
...where.exposure_compensation,
[Op.lte]: end,
},
};
}
}
if (filter.isoRange) {
const [start, end] = filter.isoRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
iso: {
...where.iso,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
iso: {
...where.iso,
[Op.lte]: end,
},
};
}
}
if (filter.focus_distanceRange) {
const [start, end] = filter.focus_distanceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
focus_distance: {
...where.focus_distance,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
focus_distance: {
...where.focus_distance,
[Op.lte]: end,
},
};
}
}
if (filter.white_balance_kelvinRange) {
const [start, end] = filter.white_balance_kelvinRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
white_balance_kelvin: {
...where.white_balance_kelvin,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
white_balance_kelvin: {
...where.white_balance_kelvin,
[Op.lte]: end,
},
};
}
}
if (filter.sharpnessRange) {
const [start, end] = filter.sharpnessRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
sharpness: {
...where.sharpness,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
sharpness: {
...where.sharpness,
[Op.lte]: end,
},
};
}
}
if (filter.denoiseRange) {
const [start, end] = filter.denoiseRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
denoise: {
...where.denoise,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
denoise: {
...where.denoise,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.capture_mode) {
where = {
...where,
capture_mode: filter.capture_mode,
};
}
if (filter.stabilization_enabled) {
where = {
...where,
stabilization_enabled: filter.stabilization_enabled,
};
}
if (filter.torch_enabled) {
where = {
...where,
torch_enabled: filter.torch_enabled,
};
}
if (filter.hdr_enabled) {
where = {
...where,
hdr_enabled: filter.hdr_enabled,
};
}
if (filter.grid_enabled) {
where = {
...where,
grid_enabled: filter.grid_enabled,
};
}
if (filter.crosshair_enabled) {
where = {
...where,
crosshair_enabled: filter.crosshair_enabled,
};
}
if (filter.object_labels_enabled) {
where = {
...where,
object_labels_enabled: filter.object_labels_enabled,
};
}
if (filter.is_default) {
where = {
...where,
is_default: filter.is_default,
};
}
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.camera_profiles.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(
'camera_profiles',
'profile_name',
query,
),
],
};
}
const records = await db.camera_profiles.findAll({
attributes: [ 'id', 'profile_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['profile_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.profile_name,
}));
}
};