function callService(context) {
const globalStatusFlag = api.config.v1.getOrDefault('form.globalStatus.isOutage', 'false');
const maintenanceFlag = api.config.v1.getOrDefault('form.maintenance.isScheduled', 'false');
const maintenanceStartDate = api.config.v1.getOrDefault('form.maintenance.startDate', '');
const maintenanceEndDate = api.config.v1.getOrDefault('form.maintenance.endDate', '');
const scheduleRuleType = api.config.v1.getOrDefault('form.schedule.ruleType', 'ALWAYS_AVAILABLE');
const scheduleExactStartDateTime = api.config.v1.getOrDefault('form.schedule.exact.startDateTime', '');
const scheduleExactEndDateTime = api.config.v1.getOrDefault('form.schedule.exact.endDateTime', '');
const scheduleRecurringStartMonthDay = api.config.v1.getOrDefault('form.schedule.recurring.startMonthDay', '');
const scheduleRecurringEndMonthDay = api.config.v1.getOrDefault('form.schedule.recurring.endMonthDay', '');
const globalStatusTextContent = api.config.v1.getOrDefault('form.globalStatus.textContent', 'formUnavailabilityFailure-*');
const maintenanceTextContent = api.config.v1.getOrDefault('form.maintenance.textContent', 'formUnavailabilityMaintenance-*');
const scheduleTextContent = api.config.v1.getOrDefault('form.schedule.textContent', 'formUnavailabilityScheduled-*');
const now = new Date();
// Priority 1: Failure
if (globalStatusFlag === 'true') {
Logger.info("Access to the application has been blocked. " + "globalStatusFlag=" + globalStatusFlag);
throwBusinessError(globalStatusTextContent, "Application unavailable due to a failure");
}
// Priority 2: Maintenance break
if (maintenanceFlag === 'true') {
const maintenanceStart = new Date(maintenanceStartDate);
const maintenanceEnd = new Date(maintenanceEndDate);
if (now >= maintenanceStart && now <= maintenanceEnd) {
Logger.info(
"Access to the application has been blocked. " +
"maintenanceFlag=" + maintenanceFlag +
", maintenanceStartDate=" + maintenanceStartDate +
", maintenanceEndDate=" + maintenanceEndDate
);
throwBusinessError(maintenanceTextContent, "Application unavailable due to a planned maintenance break");
}
}
// Priority 3: Schedule
if (scheduleRuleType === "EXACT_DATE_TIME") {
const startDateTime = new Date(scheduleExactStartDateTime);
const endDateTime = new Date(scheduleExactEndDateTime);
if (now < startDateTime || now > endDateTime) {
Logger.info(
"Access to the application has been blocked. " +
"scheduleRuleType=" + scheduleRuleType +
", startDateTime=" + scheduleExactStartDateTime +
", endDateTime=" + scheduleExactEndDateTime
);
throwBusinessError(scheduleTextContent, "Application unavailable outside the configured availability period");
}
}
// Note: no support for a date range that crosses the new year (e.g. December 2026 - February 2027)
else if (scheduleRuleType === "YEARLY_RECURRING") {
const currentYear = now.getFullYear();
const cycleStart = new Date(`${currentYear}-${scheduleRecurringStartMonthDay}T00:00:00`);
const cycleEnd = new Date(`${currentYear}-${scheduleRecurringEndMonthDay}T23:59:59`);
if (now < cycleStart || now > cycleEnd) {
Logger.info(
"Access to the application has been blocked. " +
"scheduleRuleType=" + scheduleRuleType +
", recurringStartMonthDay=" + scheduleRecurringStartMonthDay +
", recurringEndMonthDay=" + scheduleRecurringEndMonthDay
);
throwBusinessError(scheduleTextContent, "Application unavailable outside the recurring availability period");
}
}
/**
* Throwing a business error
*/
function throwBusinessError(textcontentName, errorDescription) {
const builder = context.getErrorPageDefinitionBuilder();
builder.bodyTextContent(textcontentName);
builder.msg(errorDescription);
context.throwBusinessException(builder);
}
}