38203-vm/backend/src/db/api/reading_sessions.js
2026-02-05 03:50:33 +00:00

751 lines
19 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 Reading_sessionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const reading_sessions = await db.reading_sessions.create(
{
id: data.id || undefined,
started_at: data.started_at
||
null
,
ended_at: data.ended_at
||
null
,
reading_mode: data.reading_mode
||
null
,
page_fit: data.page_fit
||
null
,
zoom_level: data.zoom_level
||
null
,
direction: data.direction
||
null
,
crop_borders: data.crop_borders
||
false
,
keep_screen_on: data.keep_screen_on
||
false
,
show_page_number: data.show_page_number
||
false
,
skip_read_chapters: data.skip_read_chapters
||
false
,
start_page_index: data.start_page_index
||
null
,
end_page_index: data.end_page_index
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await reading_sessions.setUser( data.user || null, {
transaction,
});
await reading_sessions.setChapter( data.chapter || null, {
transaction,
});
return reading_sessions;
}
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 reading_sessionsData = data.map((item, index) => ({
id: item.id || undefined,
started_at: item.started_at
||
null
,
ended_at: item.ended_at
||
null
,
reading_mode: item.reading_mode
||
null
,
page_fit: item.page_fit
||
null
,
zoom_level: item.zoom_level
||
null
,
direction: item.direction
||
null
,
crop_borders: item.crop_borders
||
false
,
keep_screen_on: item.keep_screen_on
||
false
,
show_page_number: item.show_page_number
||
false
,
skip_read_chapters: item.skip_read_chapters
||
false
,
start_page_index: item.start_page_index
||
null
,
end_page_index: item.end_page_index
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const reading_sessions = await db.reading_sessions.bulkCreate(reading_sessionsData, { transaction });
// For each item created, replace relation files
return reading_sessions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const reading_sessions = await db.reading_sessions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
if (data.ended_at !== undefined) updatePayload.ended_at = data.ended_at;
if (data.reading_mode !== undefined) updatePayload.reading_mode = data.reading_mode;
if (data.page_fit !== undefined) updatePayload.page_fit = data.page_fit;
if (data.zoom_level !== undefined) updatePayload.zoom_level = data.zoom_level;
if (data.direction !== undefined) updatePayload.direction = data.direction;
if (data.crop_borders !== undefined) updatePayload.crop_borders = data.crop_borders;
if (data.keep_screen_on !== undefined) updatePayload.keep_screen_on = data.keep_screen_on;
if (data.show_page_number !== undefined) updatePayload.show_page_number = data.show_page_number;
if (data.skip_read_chapters !== undefined) updatePayload.skip_read_chapters = data.skip_read_chapters;
if (data.start_page_index !== undefined) updatePayload.start_page_index = data.start_page_index;
if (data.end_page_index !== undefined) updatePayload.end_page_index = data.end_page_index;
updatePayload.updatedById = currentUser.id;
await reading_sessions.update(updatePayload, {transaction});
if (data.user !== undefined) {
await reading_sessions.setUser(
data.user,
{ transaction }
);
}
if (data.chapter !== undefined) {
await reading_sessions.setChapter(
data.chapter,
{ transaction }
);
}
return reading_sessions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const reading_sessions = await db.reading_sessions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of reading_sessions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of reading_sessions) {
await record.destroy({transaction});
}
});
return reading_sessions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const reading_sessions = await db.reading_sessions.findByPk(id, options);
await reading_sessions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await reading_sessions.destroy({
transaction
});
return reading_sessions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const reading_sessions = await db.reading_sessions.findOne(
{ where },
{ transaction },
);
if (!reading_sessions) {
return reading_sessions;
}
const output = reading_sessions.get({plain: true});
output.user = await reading_sessions.getUser({
transaction
});
output.chapter = await reading_sessions.getChapter({
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}%` }))
}
},
]
} : {},
},
{
model: db.chapters,
as: 'chapter',
where: filter.chapter ? {
[Op.or]: [
{ id: { [Op.in]: filter.chapter.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.chapter.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
started_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
ended_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
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.ended_atRange) {
const [start, end] = filter.ended_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ended_at: {
...where.ended_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ended_at: {
...where.ended_at,
[Op.lte]: end,
},
};
}
}
if (filter.zoom_levelRange) {
const [start, end] = filter.zoom_levelRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
zoom_level: {
...where.zoom_level,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
zoom_level: {
...where.zoom_level,
[Op.lte]: end,
},
};
}
}
if (filter.start_page_indexRange) {
const [start, end] = filter.start_page_indexRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
start_page_index: {
...where.start_page_index,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
start_page_index: {
...where.start_page_index,
[Op.lte]: end,
},
};
}
}
if (filter.end_page_indexRange) {
const [start, end] = filter.end_page_indexRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
end_page_index: {
...where.end_page_index,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
end_page_index: {
...where.end_page_index,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.reading_mode) {
where = {
...where,
reading_mode: filter.reading_mode,
};
}
if (filter.page_fit) {
where = {
...where,
page_fit: filter.page_fit,
};
}
if (filter.direction) {
where = {
...where,
direction: filter.direction,
};
}
if (filter.crop_borders) {
where = {
...where,
crop_borders: filter.crop_borders,
};
}
if (filter.keep_screen_on) {
where = {
...where,
keep_screen_on: filter.keep_screen_on,
};
}
if (filter.show_page_number) {
where = {
...where,
show_page_number: filter.show_page_number,
};
}
if (filter.skip_read_chapters) {
where = {
...where,
skip_read_chapters: filter.skip_read_chapters,
};
}
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.reading_sessions.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(
'reading_sessions',
'reading_mode',
query,
),
],
};
}
const records = await db.reading_sessions.findAll({
attributes: [ 'id', 'reading_mode' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['reading_mode', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.reading_mode,
}));
}
};