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

1035 lines
27 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 CapturesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const captures = await db.captures.create(
{
id: data.id || undefined,
capture_name: data.capture_name
||
null
,
capture_type: data.capture_type
||
null
,
processing_state: data.processing_state
||
null
,
captured_at: data.captured_at
||
null
,
exposure_seconds: data.exposure_seconds
||
null
,
iso: data.iso
||
null
,
focal_length_mm_equiv: data.focal_length_mm_equiv
||
null
,
digital_zoom: data.digital_zoom
||
null
,
field_of_view_deg: data.field_of_view_deg
||
null
,
azimuth_deg: data.azimuth_deg
||
null
,
altitude_deg: data.altitude_deg
||
null
,
ra_hours: data.ra_hours
||
null
,
dec_deg: data.dec_deg
||
null
,
latitude: data.latitude
||
null
,
longitude: data.longitude
||
null
,
is_favorite: data.is_favorite
||
false
,
processing_notes: data.processing_notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await captures.setObservation_session( data.observation_session || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'image_frames',
belongsToId: captures.id,
},
data.image_frames,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'video_file',
belongsToId: captures.id,
},
data.video_file,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'raw_files',
belongsToId: captures.id,
},
data.raw_files,
options,
);
return captures;
}
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 capturesData = data.map((item, index) => ({
id: item.id || undefined,
capture_name: item.capture_name
||
null
,
capture_type: item.capture_type
||
null
,
processing_state: item.processing_state
||
null
,
captured_at: item.captured_at
||
null
,
exposure_seconds: item.exposure_seconds
||
null
,
iso: item.iso
||
null
,
focal_length_mm_equiv: item.focal_length_mm_equiv
||
null
,
digital_zoom: item.digital_zoom
||
null
,
field_of_view_deg: item.field_of_view_deg
||
null
,
azimuth_deg: item.azimuth_deg
||
null
,
altitude_deg: item.altitude_deg
||
null
,
ra_hours: item.ra_hours
||
null
,
dec_deg: item.dec_deg
||
null
,
latitude: item.latitude
||
null
,
longitude: item.longitude
||
null
,
is_favorite: item.is_favorite
||
false
,
processing_notes: item.processing_notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const captures = await db.captures.bulkCreate(capturesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < captures.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'image_frames',
belongsToId: captures[i].id,
},
data[i].image_frames,
options,
);
}
for (let i = 0; i < captures.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'video_file',
belongsToId: captures[i].id,
},
data[i].video_file,
options,
);
}
for (let i = 0; i < captures.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'raw_files',
belongsToId: captures[i].id,
},
data[i].raw_files,
options,
);
}
return captures;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const captures = await db.captures.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.capture_name !== undefined) updatePayload.capture_name = data.capture_name;
if (data.capture_type !== undefined) updatePayload.capture_type = data.capture_type;
if (data.processing_state !== undefined) updatePayload.processing_state = data.processing_state;
if (data.captured_at !== undefined) updatePayload.captured_at = data.captured_at;
if (data.exposure_seconds !== undefined) updatePayload.exposure_seconds = data.exposure_seconds;
if (data.iso !== undefined) updatePayload.iso = data.iso;
if (data.focal_length_mm_equiv !== undefined) updatePayload.focal_length_mm_equiv = data.focal_length_mm_equiv;
if (data.digital_zoom !== undefined) updatePayload.digital_zoom = data.digital_zoom;
if (data.field_of_view_deg !== undefined) updatePayload.field_of_view_deg = data.field_of_view_deg;
if (data.azimuth_deg !== undefined) updatePayload.azimuth_deg = data.azimuth_deg;
if (data.altitude_deg !== undefined) updatePayload.altitude_deg = data.altitude_deg;
if (data.ra_hours !== undefined) updatePayload.ra_hours = data.ra_hours;
if (data.dec_deg !== undefined) updatePayload.dec_deg = data.dec_deg;
if (data.latitude !== undefined) updatePayload.latitude = data.latitude;
if (data.longitude !== undefined) updatePayload.longitude = data.longitude;
if (data.is_favorite !== undefined) updatePayload.is_favorite = data.is_favorite;
if (data.processing_notes !== undefined) updatePayload.processing_notes = data.processing_notes;
updatePayload.updatedById = currentUser.id;
await captures.update(updatePayload, {transaction});
if (data.observation_session !== undefined) {
await captures.setObservation_session(
data.observation_session,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'image_frames',
belongsToId: captures.id,
},
data.image_frames,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'video_file',
belongsToId: captures.id,
},
data.video_file,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.captures.getTableName(),
belongsToColumn: 'raw_files',
belongsToId: captures.id,
},
data.raw_files,
options,
);
return captures;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const captures = await db.captures.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of captures) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of captures) {
await record.destroy({transaction});
}
});
return captures;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const captures = await db.captures.findByPk(id, options);
await captures.update({
deletedBy: currentUser.id
}, {
transaction,
});
await captures.destroy({
transaction
});
return captures;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const captures = await db.captures.findOne(
{ where },
{ transaction },
);
if (!captures) {
return captures;
}
const output = captures.get({plain: true});
output.object_identifications_capture = await captures.getObject_identifications_capture({
transaction
});
output.observation_session = await captures.getObservation_session({
transaction
});
output.image_frames = await captures.getImage_frames({
transaction
});
output.video_file = await captures.getVideo_file({
transaction
});
output.raw_files = await captures.getRaw_files({
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.observation_sessions,
as: 'observation_session',
where: filter.observation_session ? {
[Op.or]: [
{ id: { [Op.in]: filter.observation_session.split('|').map(term => Utils.uuid(term)) } },
{
session_name: {
[Op.or]: filter.observation_session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'image_frames',
},
{
model: db.file,
as: 'video_file',
},
{
model: db.file,
as: 'raw_files',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.capture_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'captures',
'capture_name',
filter.capture_name,
),
};
}
if (filter.processing_notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'captures',
'processing_notes',
filter.processing_notes,
),
};
}
if (filter.captured_atRange) {
const [start, end] = filter.captured_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
captured_at: {
...where.captured_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
captured_at: {
...where.captured_at,
[Op.lte]: end,
},
};
}
}
if (filter.exposure_secondsRange) {
const [start, end] = filter.exposure_secondsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
exposure_seconds: {
...where.exposure_seconds,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
exposure_seconds: {
...where.exposure_seconds,
[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.focal_length_mm_equivRange) {
const [start, end] = filter.focal_length_mm_equivRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
focal_length_mm_equiv: {
...where.focal_length_mm_equiv,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
focal_length_mm_equiv: {
...where.focal_length_mm_equiv,
[Op.lte]: end,
},
};
}
}
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.field_of_view_degRange) {
const [start, end] = filter.field_of_view_degRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
field_of_view_deg: {
...where.field_of_view_deg,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
field_of_view_deg: {
...where.field_of_view_deg,
[Op.lte]: end,
},
};
}
}
if (filter.azimuth_degRange) {
const [start, end] = filter.azimuth_degRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
azimuth_deg: {
...where.azimuth_deg,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
azimuth_deg: {
...where.azimuth_deg,
[Op.lte]: end,
},
};
}
}
if (filter.altitude_degRange) {
const [start, end] = filter.altitude_degRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
altitude_deg: {
...where.altitude_deg,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
altitude_deg: {
...where.altitude_deg,
[Op.lte]: end,
},
};
}
}
if (filter.ra_hoursRange) {
const [start, end] = filter.ra_hoursRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ra_hours: {
...where.ra_hours,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ra_hours: {
...where.ra_hours,
[Op.lte]: end,
},
};
}
}
if (filter.dec_degRange) {
const [start, end] = filter.dec_degRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
dec_deg: {
...where.dec_deg,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
dec_deg: {
...where.dec_deg,
[Op.lte]: end,
},
};
}
}
if (filter.latitudeRange) {
const [start, end] = filter.latitudeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
latitude: {
...where.latitude,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
latitude: {
...where.latitude,
[Op.lte]: end,
},
};
}
}
if (filter.longitudeRange) {
const [start, end] = filter.longitudeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
longitude: {
...where.longitude,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
longitude: {
...where.longitude,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.capture_type) {
where = {
...where,
capture_type: filter.capture_type,
};
}
if (filter.processing_state) {
where = {
...where,
processing_state: filter.processing_state,
};
}
if (filter.is_favorite) {
where = {
...where,
is_favorite: filter.is_favorite,
};
}
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.captures.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(
'captures',
'capture_name',
query,
),
],
};
}
const records = await db.captures.findAll({
attributes: [ 'id', 'capture_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['capture_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.capture_name,
}));
}
};