Skip to content

Commit

Permalink
feat: source-status is set to purge if it has been status gone for mo…
Browse files Browse the repository at this point in the history
…re than 30 days
  • Loading branch information
malmen237 committed Sep 2, 2024
1 parent fa2a93b commit 306c945
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/api/manager/job/syncInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ export async function runSyncInventory() {
const apiSources = await getSourcesFromAPI();
const dbInventory = await db.collection<Source>('inventory').find().toArray();

const statusUpdateCheck = (inventorySource: WithId<Source>, 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) => {
Expand All @@ -69,15 +86,10 @@ export async function runSyncInventory() {
return { ...inventorySource, status: 'gone' } satisfies WithId<Source>;
}

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<Source>;
});

Expand Down

0 comments on commit 306c945

Please sign in to comment.