From 895803bd626118c1ea84417dfad9ad18ca359509 Mon Sep 17 00:00:00 2001 From: Freek Bes Date: Tue, 8 Oct 2024 17:08:58 +0200 Subject: [PATCH] fix sync using last sync time from server start --- src/env.ts | 3 --- src/sync/base.ts | 19 ++++++++++++++++--- src/sync/coalitions_users.ts | 6 +----- src/sync/projects.ts | 7 ++----- src/sync/users.ts | 7 ++----- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/env.ts b/src/env.ts index 97e27b7..0145bfd 100644 --- a/src/env.ts +++ b/src/env.ts @@ -10,6 +10,3 @@ export const CURSUS_ID: number = parseInt(process.env.INTRA_CURSUS_ID!); // Not defined in .env but in regular shell environment variables export const DEV_DAYS_LIMIT: number = process.env.DEV_DAYS_LIMIT ? parseInt(process.env.DEV_DAYS_LIMIT) : 365; - -// Read from the .sync-timestamp file -export const LAST_SYNC_TIMESTAMP: number = fs.existsSync('.sync-timestamp') ? parseInt(fs.readFileSync('.sync-timestamp', 'utf8')) : 0; diff --git a/src/sync/base.ts b/src/sync/base.ts index 6f8363d..26be29e 100644 --- a/src/sync/base.ts +++ b/src/sync/base.ts @@ -162,6 +162,18 @@ export const syncDataCB = async function(api: Fast42, syncDate: Date, lastSyncDa await fetchMultiple42ApiPagesCallback(api, path, params, callback); } +const getLastSyncTimestamp = async function(): Promise { + return new Promise((resolve, reject) => { + fs.readFile('.sync-timestamp', 'utf8', (err, data) => { + if (err) { + // return reject(err); + return resolve(new Date(0)); + } + return resolve(new Date(parseInt(data))); + }); + }); +} + const saveSyncTimestamp = async function(timestamp: Date): Promise { console.log('Saving timestamp of synchronization to ./.sync-timestamp...'); // Save to current folder in .sync-timestamp file @@ -173,13 +185,14 @@ export const syncWithIntra = async function(api: Fast42): Promise { const now = new Date(); console.info(`Starting Intra synchronization at ${now.toISOString()}...`); + const lastSync = await getLastSyncTimestamp(); await initCodamQuiz(); await initCodamCoalitionFixedTypes(); - await syncProjects(api, now); - await syncUsers(api, now); + await syncProjects(api, lastSync, now); + await syncUsers(api, lastSync, now); await syncBlocs(api, now); // also syncs coalitions - await syncCoalitionUsers(api, now); + await syncCoalitionUsers(api, lastSync, now); await cleanupDB(api); await saveSyncTimestamp(now); diff --git a/src/sync/coalitions_users.ts b/src/sync/coalitions_users.ts index 72b74b3..dfad0da 100644 --- a/src/sync/coalitions_users.ts +++ b/src/sync/coalitions_users.ts @@ -1,6 +1,5 @@ import Fast42 from '@codam/fast42'; import { prisma, syncData } from './base'; -import { LAST_SYNC_TIMESTAMP } from '../env'; import { getCoalitionIds } from '../utils'; // User object can be an object returned by /v2/users/:id or the user object in /v2/cursus_users/:id ! @@ -68,10 +67,7 @@ export const syncCoalitionUser = async function(coalitionUser: any): Promise { - // Fetch the time of the last shutdown - const syncSince = new Date(LAST_SYNC_TIMESTAMP); - +export const syncCoalitionUsers = async function(api: Fast42, syncSince: Date, syncDate: Date): Promise { // Fetch all of our coalition ids const coalitionIds = await getCoalitionIds(prisma); diff --git a/src/sync/projects.ts b/src/sync/projects.ts index 5337fee..9c3186a 100644 --- a/src/sync/projects.ts +++ b/src/sync/projects.ts @@ -1,6 +1,6 @@ import Fast42 from '@codam/fast42'; import { prisma, syncData } from './base'; -import { CURSUS_ID, LAST_SYNC_TIMESTAMP } from '../env'; +import { CURSUS_ID } from '../env'; export const syncProject = async function(project: any): Promise { try { @@ -33,10 +33,7 @@ export const syncProject = async function(project: any): Promise { } }; -export const syncProjects = async function(api: Fast42, syncDate: Date): Promise { - // Fetch the time of the last shutdown - const syncSince = new Date(LAST_SYNC_TIMESTAMP); - +export const syncProjects = async function(api: Fast42, syncSince: Date, syncDate: Date): Promise { // Fetch all projects from the API updated since the last shutdown const projects = await syncData(api, syncDate, syncSince, `/cursus/${CURSUS_ID}/projects`, {}); diff --git a/src/sync/users.ts b/src/sync/users.ts index 79b2a72..12269b5 100644 --- a/src/sync/users.ts +++ b/src/sync/users.ts @@ -1,6 +1,6 @@ import Fast42 from '@codam/fast42'; import { prisma, syncData } from './base'; -import { CAMPUS_ID, LAST_SYNC_TIMESTAMP } from '../env'; +import { CAMPUS_ID } from '../env'; // User object can be an object returned by /v2/users/:id or the user object in /v2/cursus_users/:id ! export const syncUser = async function(user: any): Promise { @@ -58,10 +58,7 @@ export const syncUser = async function(user: any): Promise { } }; -export const syncUsers = async function(api: Fast42, syncDate: Date): Promise { - // Fetch the time of the last shutdown - const syncSince = new Date(LAST_SYNC_TIMESTAMP); - +export const syncUsers = async function(api: Fast42, syncSince: Date, syncDate: Date): Promise { // Fetch all users from the API updated since the last shutdown const users = await syncData(api, syncDate, syncSince, `/campus/${CAMPUS_ID}/users`, {});