From 306c94514bb1d02b02f959bbe9bc4ec2dfea9b4f Mon Sep 17 00:00:00 2001 From: malmen237 Date: Mon, 2 Sep 2024 07:50:25 +0200 Subject: [PATCH] feat: source-status is set to purge if it has been status gone for more than 30 days --- src/api/manager/job/syncInventory.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/api/manager/job/syncInventory.ts b/src/api/manager/job/syncInventory.ts index 65428de..e948a03 100644 --- a/src/api/manager/job/syncInventory.ts +++ b/src/api/manager/job/syncInventory.ts @@ -55,6 +55,23 @@ export async function runSyncInventory() { const apiSources = await getSourcesFromAPI(); const dbInventory = await db.collection('inventory').find().toArray(); + const statusUpdateCheck = (inventorySource: WithId, apiSource: Source) => { + const databaseStatus = inventorySource.status; + const apiStatus = apiSource.status; + let currentTime = new Date().getTime(); + let createdAtTime = new Date(inventorySource.createdAt).getTime(); + let monthInMilliseconds = 30 * 24 * 60 * 60 * 1000; + let expiryTime = createdAtTime + monthInMilliseconds; + + if (databaseStatus === 'purge' && apiStatus === 'gone') { + return databaseStatus; + } else if (apiStatus === 'gone' && currentTime > expiryTime) { + return 'purge'; + } else { + return apiStatus; + } + }; + // Update status of all sources in the inventory to the status found in API. // If a source is not found in the API, it is marked as gone. const dbInventoryWithCorrectStatus = dbInventory.map((inventorySource) => { @@ -69,15 +86,10 @@ export async function runSyncInventory() { return { ...inventorySource, status: 'gone' } satisfies WithId; } - const databaseStatus = inventorySource.status; - const apiStatus = apiSource.status; - const isStatusGoneAndSetToPurge = - databaseStatus === 'purge' && apiStatus === 'gone'; - // Keep all old fields from the inventory source (name, tags, id, audio_stream etc), but update the status return { ...inventorySource, - status: isStatusGoneAndSetToPurge ? databaseStatus : apiStatus + status: statusUpdateCheck(inventorySource, apiSource) } satisfies WithId; });