I have created a Season_to_date filter type to aggregate data for two seasons: S1 -> Q1, Q2 & S2 -> Q3, Q4. However, the implementation is not functioning as expected. Any assistance on how to correct this issue would be greatly appreciated.
const FY_Q1: string[] = ['AUG', 'SEP', 'OCT'];
const FY_Q2: string[] = ['NOV', 'DEC', 'JAN'];
const FY_Q3: string[] = ['FEB', 'MAR', 'APR'];
const FY_Q4: string[] = ['MAY', 'JUN', 'JUL'];
//-------Seasons-------------------------
const FY_S1 = [FY_Q1, FY_Q2];
const FY_S2 = [FY_Q3,FY_Q4];
export async function getAggregationsForManagerSales(filter: ManagerSalesFilter, isManager: boolean): Promise<ManagerSalesResponse> {
let managerSalesResponse: ManagerSalesResponse;
var aggrSalesChart: ManagerSalesChart[] = [];
var aggrNetSales = 0;
var aggrReturns = 0;
var aggrPreSales = 0;
var aggrLyNetSales = 0;
logger.debug("Inside getAggregationsForManagerSales()..." );
try {
const filterType = filter.filterType;
const { dateFrom, dateTo, fiscalMonth,fiscalQuarterStart} = await buildDateRangeFromFilterType(filterType, filter.dateFrom, filter.dateTo, filter.storeId);
logger.debug("dateFrom: " + dateFrom + ", dateTo: " + dateTo + ", fiscalMonth: " + fiscalMonth);
const fiscalMap = buildDateRangeFiscalMap(filterType, dateFrom, dateTo, fiscalMonth, fiscalQuarterStart);
const lyDateFrom = await getLastFiscalDay(dateFrom);
const lyDateTo = await getLastFiscalDay(dateTo);
let managerSalesQuery = aggreagatedSalesQueryManagerMetrics({ ...filter, dateFrom, dateTo }, isManager, lyDateFrom, lyDateTo);
const result: EsResponse = await salesEsClient.search(managerSalesQuery);
logger.debug("EsResponse: \n" + JSON.stringify(result));
function buildDateRangeFiscalMap(filterType: ManagerMetricsFilterType, dateFrom: string, dateTo: string, fiscalMonth: string, fiscalQuarterStart : string): Map<any, any> {
logger.debug('Inside buildDateRangeFiscalMap');
const df = new Date(dateFrom);
const dt = new Date(dateTo);
const fiscalMap: Map<any, any> = new Map();
switch (filterType) {
case ManagerMetricsFilterType.MONTH_TO_DATE:
var newDate = new Date(df);
while (newDate <= dt) {
fiscalMap.set(newDate.toISOString().slice(0, 10), fiscalMonth);
var temp = newDate.setDate(newDate.getDate() + 1);
newDate = new Date(temp);
}
return fiscalMap;
case ManagerMetricsFilterType.QUARTER_TO_DATE:
return fiscalQuarterFunction(dateFrom, dateTo, fiscalMonth);
//------season-to-date----------------------------
case ManagerMetricsFilterType.SEASON_TO_DATE:
return fiscalSeasonFunction(dateFrom, dateTo,fiscalQuarterStart);
//--------------------------------------------------
case ManagerMetricsFilterType.YEAR_TO_DATE:
return fiscalQuarterFunction(dateFrom, dateTo);
default:
return fiscalMap;
}
}
function fiscalQuarterFunction(dateFrom: string, dateTo: string, fiscalMonth?: string): Map<any, any> {
logger.debug('Inside fiscalQuarterFunction');
var loopStartDate = new Date(dateFrom);
const dt = new Date(dateTo);
var loopEndDate = new Date(dt.setDate(dt.getDate() + 1));
const fyQuaterlyMap = new Map();
const fyQuarterlyList = [FY_Q1, FY_Q2, FY_Q3, FY_Q4];
QUARTER_LOOP: for (const fyQuarter of fyQuarterlyList) {
const fyQuarterlyMonths = fiscalMonth ? findFiscalQuarter(fiscalMonth) : findFiscalQuarter(fyQuarter[0]);
if (fyQuarterlyMonths && fyQuarterlyMonths.length) {
for (var month = 0; month < 3; month++) {
const fyMonth = fyQuarterlyMonths[month];
for (var week = 0; week <= 5; week++) {
if (month == 0 && week == 4) {
break;
} else if (month == 1 && week == 5) {
break;
} else if (month == 2 && week == 4) {
break;
}
var day = 1;
while (loopStartDate < loopEndDate) {
if (day > 7) {
break;
}
fyQuaterlyMap.set(loopStartDate.toISOString().slice(0, 10), fyMonth);
var temp = loopStartDate.setDate(loopStartDate.getDate() + 1);
loopStartDate = new Date(temp);
day += 1;
}
if (loopStartDate > dt) {
break QUARTER_LOOP;
}
}
}
} else break QUARTER_LOOP;
}
return fyQuaterlyMap;
}
function findFiscalQuarter(fiscalMonth: string): string[] {
if (FY_Q1.includes(fiscalMonth)) {
return FY_Q1;
}
if (FY_Q2.includes(fiscalMonth)) {
return FY_Q2;
}
if (FY_Q3.includes(fiscalMonth)) {
return FY_Q3;
}
if (FY_Q4.includes(fiscalMonth)) {
return FY_Q4;
}
return [];
}
//-------------new_season_date----------------------------------------------------
function fiscalSeasonFunction(dateFrom: string, dateTo: string, fiscalQuarterStart?: string): Map<any, any> {
var loopStartDate = new Date(dateFrom);
const dt = new Date(dateTo);
var loopEndDate = new Date(dt.setDate(dt.getDate() + 1));
const fySeasonMap = new Map();
const fySeasonList = [FY_S1,FY_S2];
SEASON_LOOP: for (const fySeason of fySeasonList) {
const fySeasonQuarters = fiscalQuarterStart ? findFiscalSeason(fiscalQuarterStart) : findFiscalSeason(fySeason[0]);
if (fySeasonQuarters && fySeasonQuarters.length) {
for (var quarter = 0; quarter < 2; quarter++) {
const fyquater = fySeasonQuarters[quarter];
for (var months = 0; months <= 4; months++) {
if (quarter == 0 && months == 3) {
break;
} else if (quarter == 1 && months == 3 ){
break;
}
var day = 1;
while (loopStartDate < loopEndDate) {
if (day > 7) {
break;
}
fySeasonMap.set(loopStartDate.toISOString().slice(0, 10), fyquater);
var temp = loopStartDate.setDate(loopStartDate.getDate() + 1);
loopStartDate = new Date(temp);
day += 1;
}
if (loopStartDate > dt) {
break SEASON_LOOP;
}
}
}
} else break SEASON_LOOP;
console.log("---------1--------------", fySeasonMap)
}
return fySeasonMap;
}
function findFiscalSeason(fiscalQuarterStart: string): string[] {
if (FY_S1.includes(fiscalQuarterStart)) {
return FY_Q1;
}
if (FY_S2.includes(fiscalQuarterStart)) {
return FY_Q2;
}
return [];
}
The current implementation of Season_to_date is encountering issues that need to be resolved. Any guidance or suggestions on rectifying this problem would be helpful. Thank you.