Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
mudeerapi.abasa.com
/
nodetest
/
src
/
services
/
Filename :
tenureHelper.js
back
Copy
import { user } from '../models/user.models.js'; import { UserTenure } from '../models/userTenure.model.js'; // Add exactly one tenure-year to a date. export const addOneTenureYear = (date) => { const d = new Date(date); if (d.getUTCMonth() === 1 && d.getUTCDate() === 29) { d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000); } else { d.setUTCFullYear(d.getUTCFullYear() + 1); } return d; }; // Given a user's joined_on date and any target date, return which tenure // window the target falls in: { tenureIndex (1-based), start (Date), end (Date exclusive) } export const getTenureForDate = (joinedOn, targetDate) => { let start = new Date(joinedOn); let tenureIndex = 1; while (true) { const end = addOneTenureYear(start); if (targetDate < end) { return { tenureIndex, start, end }; } start = end; tenureIndex++; } }; // All tenure windows from join date through the window that contains refDate (inclusive). export const listTenureWindowsFromJoinThroughDate = (joinedOn, refDate) => { const windows = []; let start = new Date(joinedOn); let tenureIndex = 1; while (true) { const end = addOneTenureYear(start); windows.push({ tenureIndex, start: new Date(start.getTime()), end: new Date(end.getTime()), }); if (refDate < end) break; start = end; tenureIndex += 1; } return windows; }; // Find or create the userTenure document for the tenure that contains targetDate. export const ensureUserTenure = async (userId, joinedOn, targetDate) => { const { tenureIndex, start, end } = getTenureForDate(joinedOn, targetDate); const existing = await UserTenure.findOne({ userId, tenureIndex }).lean(); if (existing) return existing; const u = await user.findById(userId).select('first_name last_name').lean(); const first_name = u?.first_name ?? ''; const last_name = u?.last_name ?? ''; const doc = await UserTenure.findOneAndUpdate( { userId, tenureIndex }, { $setOnInsert: { userId, tenureIndex, startDate: start, endDate: end, first_name, last_name, paidLeavesAllocated: 12, paidLeavesUsed: 0, occasionalOffsUsed: 0, unpaidLeavesUsed: 0, }, }, { upsert: true, new: true } ); return doc; };