All files / src/repositories/scan holiday-calendar.repository.ts

26.92% Statements 7/26
0% Branches 0/4
0% Functions 0/7
21.74% Lines 5/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981x 1x 1x       1x   1x                                                                                                                                                                                  
import {inject} from '@loopback/core';
import {LogChainDataSource} from '@logchain/datasources';
import {
  HolidayCalendar,
  HolidayCalendarRelations,
} from '@logchain/models/scan/holiday-calendar.model';
import {BaseCrudRepository} from '../base-crud.repository.base';
 
export class HolidayCalendarRepository extends BaseCrudRepository<
  HolidayCalendar,
  typeof HolidayCalendar.prototype.id,
  HolidayCalendarRelations
> {
  constructor(
    @inject('datasources.logchain')
    dataSource: LogChainDataSource,
  ) {
    super(HolidayCalendar, dataSource);
  }
 
  /**
   * Get all active holidays for a country in a date range
   */
  async getHolidaysInRange(
    startDate: Date,
    endDate: Date,
    countryCode?: string,
  ): Promise<Date[]> {
    const where: any = {
      holiday_date: {
        between: [startDate, endDate],
      },
      is_active: true,
    };
 
    if (countryCode) {
      where.country_code = countryCode;
    }
 
    const holidays = await this.find({
      where,
      order: ['holiday_date ASC'],
    });
 
    return holidays.map(h => new Date(h.holiday_date));
  }
 
  /**
   * Check if a specific date is a holiday
   */
  async isHoliday(date: Date, countryCode?: string): Promise<boolean> {
    const startOfDay = new Date(date);
    startOfDay.setHours(0, 0, 0, 0);
 
    const endOfDay = new Date(date);
    endOfDay.setHours(23, 59, 59, 999);
 
    const where: any = {
      holiday_date: {
        between: [startOfDay, endOfDay],
      },
      is_active: true,
    };
 
    if (countryCode) {
      where.country_code = countryCode;
    }
 
    const holiday = await this.findOne({where});
    return !!holiday;
  }
 
  /**
   * Get all active holidays by country
   */
  async getHolidaysByCountry(countryCode: string): Promise<HolidayCalendar[]> {
    return this.find({
      where: {
        country_code: countryCode,
        is_active: true,
      },
      order: ['holiday_date ASC'],
    });
  }
 
  /**
   * Bulk insert holidays
   */
  async bulkInsertHolidays(holidays: Partial<HolidayCalendar>[]): Promise<HolidayCalendar[]> {
    return Promise.all(
      holidays.map(holiday => this.create({
        ...holiday,
        created_at: new Date(),
      })),
    );
  }
}