mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
ability to restore (soft deleted) listings
This commit is contained in:
@@ -26,6 +26,7 @@ export default async function listingsPlugin(fastify) {
|
||||
providerFilter,
|
||||
watchListFilter,
|
||||
statusFilter,
|
||||
hiddenOnly,
|
||||
sortfield = null,
|
||||
sortdir = 'asc',
|
||||
freeTextFilter,
|
||||
@@ -38,6 +39,7 @@ export default async function listingsPlugin(fastify) {
|
||||
};
|
||||
const normalizedActivity = toBool(activityFilter);
|
||||
const normalizedWatch = toBool(watchListFilter);
|
||||
const normalizedHidden = toBool(hiddenOnly) === true;
|
||||
const allowedStatuses = ['applied', 'rejected', 'accepted', 'none'];
|
||||
const normalizedStatus =
|
||||
typeof statusFilter === 'string' && allowedStatuses.includes(statusFilter.toLowerCase())
|
||||
@@ -62,6 +64,7 @@ export default async function listingsPlugin(fastify) {
|
||||
providerFilter,
|
||||
watchListFilter: normalizedWatch,
|
||||
statusFilter: normalizedStatus,
|
||||
hiddenOnly: normalizedHidden,
|
||||
sortField: sortfield || null,
|
||||
sortDir: sortdir === 'desc' ? 'desc' : 'asc',
|
||||
userId: request.session.currentUser,
|
||||
@@ -192,4 +195,21 @@ export default async function listingsPlugin(fastify) {
|
||||
}
|
||||
return reply.send();
|
||||
});
|
||||
|
||||
fastify.post('/restore', async (request, reply) => {
|
||||
const { ids } = request.body || {};
|
||||
const settings = await getSettings();
|
||||
try {
|
||||
if (settings.demoMode && !isAdminFn(request)) {
|
||||
return reply.code(403).send({ error: 'Sorry, but you cannot restore listings in demo mode ;)' });
|
||||
}
|
||||
if (Array.isArray(ids) && ids.length > 0) {
|
||||
listingStorage.restoreListingsById(ids);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return reply.code(500).send({ error: error.message });
|
||||
}
|
||||
return reply.send();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -264,6 +264,7 @@ export const storeListings = (jobId, providerId, listings) => {
|
||||
* @param {number} [params.createdBefore] - Only include listings created at or before this unix timestamp (ms).
|
||||
* @param {string} [params.userId] - Current user id used to scope listings (ignored for admins).
|
||||
* @param {boolean} [params.isAdmin=false] - When true, returns all listings.
|
||||
* @param {boolean} [params.hiddenOnly=false] - When true, returns only soft-deleted (manually_deleted = 1) listings.
|
||||
* @returns {{ totalNumber:number, page:number, result:Object[] }}
|
||||
*/
|
||||
export const queryListings = ({
|
||||
@@ -284,6 +285,7 @@ export const queryListings = ({
|
||||
maxPrice = null,
|
||||
userId = null,
|
||||
isAdmin = false,
|
||||
hiddenOnly = false,
|
||||
} = {}) => {
|
||||
// sanitize inputs
|
||||
const safePageSize = Number.isFinite(pageSize) && pageSize > 0 ? Math.min(1000, Math.floor(pageSize)) : 50;
|
||||
@@ -365,8 +367,8 @@ export const queryListings = ({
|
||||
whereParts.push('(l.price <= @maxPrice)');
|
||||
}
|
||||
|
||||
// Build whereSql (filtering by manually_deleted = 0)
|
||||
whereParts.push('(l.manually_deleted = 0)');
|
||||
// Build whereSql: in normal mode hide soft-deleted; in hiddenOnly mode show only soft-deleted.
|
||||
whereParts.push(hiddenOnly ? '(l.manually_deleted = 1)' : '(l.manually_deleted = 0)');
|
||||
|
||||
const whereSqlWithAlias = whereParts.length ? `WHERE ${whereParts.join(' AND ')}` : '';
|
||||
|
||||
@@ -463,6 +465,23 @@ export const deleteListingsById = (ids, hardDelete = false) => {
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Restore previously soft-deleted listings by clearing their `manually_deleted` flag.
|
||||
*
|
||||
* @param {string[]} ids - Array of DB row IDs to restore.
|
||||
* @returns {any} The result from SqliteConnection.execute.
|
||||
*/
|
||||
export const restoreListingsById = (ids) => {
|
||||
if (!Array.isArray(ids) || ids.length === 0) return;
|
||||
const placeholders = ids.map(() => '?').join(',');
|
||||
return SqliteConnection.execute(
|
||||
`UPDATE listings
|
||||
SET manually_deleted = 0
|
||||
WHERE id IN (${placeholders})`,
|
||||
ids,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return all listings that are active, have an address, and do not yet have geocoordinates.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user