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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {
get,
post,
put,
del,
param,
requestBody,
HttpErrors,
} from '@loopback/rest';
import {service, inject} from '@loopback/core';
import {repository} from '@loopback/repository';
import {authenticate} from '@loopback/authentication';
import {SecurityBindings} from '@loopback/security';
import {UserProfile} from '@logchain/components/aws';
import {SlaConfigRepository} from '@logchain/repositories/scan/sla-config.repository';
import {HolidayCalendarRepository} from '@logchain/repositories/scan/holiday-calendar.repository';
import {SlaConfigService} from '@logchain/services/scan/sla-config.service';
import {SlaConfig} from '@logchain/models/scan/sla-config.model';
import {HolidayCalendar} from '@logchain/models/scan/holiday-calendar.model';
@authenticate('aws-cognito')
export class SlaConfigController {
constructor(
@repository(SlaConfigRepository)
private slaConfigRepo: SlaConfigRepository,
@repository(HolidayCalendarRepository)
private holidayCalendarRepo: HolidayCalendarRepository,
@service(SlaConfigService)
private slaConfigService: SlaConfigService,
@inject(SecurityBindings.USER, {optional: true})
private currentUser: UserProfile,
) {}
/**
* GET: List all SLA providers
*/
@get('/sla-configs')
async listSlaConfigs(
@param.query.string('countryCode') countryCode?: string,
@param.query.boolean('activeOnly') activeOnly: boolean = true,
) {
let configs: SlaConfig[];
if (activeOnly) {
configs = await this.slaConfigRepo.findActive(countryCode);
} else {
const where: any = {};
if (countryCode) {
where.country_code = countryCode;
}
configs = await this.slaConfigRepo.find({
where,
order: ['provider ASC'],
});
}
return {
success: true,
data: configs,
count: configs.length,
};
}
/**
* GET: Get SLA provider details by ID
*/
@get('/sla-configs/{id}')
async getSlaConfigById(
@param.path.number('id') id: number,
) {
const config = await this.slaConfigRepo.findById(id);
if (!config) {
throw new HttpErrors.NotFound(`SLA configuration with ID ${id} not found`);
}
return {
success: true,
data: config,
};
}
/**
* GET: Get SLA provider by provider name
*/
@get('/sla-configs/by-provider/{provider}')
async getSlaConfigByProvider(
@param.path.string('provider') provider: string,
@param.query.string('countryCode') countryCode?: string,
) {
const config = await this.slaConfigRepo.findByProviderAndCountry(
provider,
countryCode,
);
if (!config) {
throw new HttpErrors.NotFound(
`SLA configuration not found for provider: ${provider}`,
);
}
return {
success: true,
data: config,
};
}
/**
* POST: Create new SLA provider configuration
*/
@post('/sla-configs')
async createSlaConfig(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
required: ['provider', 'sla_hours'],
properties: {
provider: {type: 'string', description: 'Provider/Forwarder name'},
sla_hours: {type: 'number', description: 'SLA time in hours'},
include_weekend: {type: 'boolean', default: false},
include_public_holiday: {type: 'boolean', default: false},
country_code: {type: 'string', description: 'Country code (e.g., VN, US)'},
description: {type: 'string'},
},
},
},
},
})
body: {
provider: string;
sla_hours: number;
include_weekend?: boolean;
include_public_holiday?: boolean;
country_code?: string;
description?: string;
},
) {
if (!body.provider || !body.sla_hours) {
throw new HttpErrors.BadRequest('Provider name and SLA hours are required');
}
if (body.sla_hours <= 0) {
throw new HttpErrors.BadRequest('SLA hours must be greater than 0');
}
const existing = await this.slaConfigRepo.findByProviderAndCountry(
body.provider,
body.country_code,
);
if (existing && !existing.is_deleted) {
throw new HttpErrors.Conflict(
`SLA configuration for provider "${body.provider}" already exists`,
);
}
const config = await this.slaConfigRepo.create({
provider: body.provider.trim(),
sla_hours: body.sla_hours,
include_weekend: body.include_weekend ?? false,
include_public_holiday: body.include_public_holiday ?? false,
country_code: body.country_code?.toUpperCase(),
description: body.description?.trim(),
is_active: true,
is_deleted: false,
created_at: new Date(),
});
return {
success: true,
message: `SLA configuration for "${body.provider}" created successfully`,
data: config,
};
}
/**
* PUT: Update SLA provider configuration
*/
@put('/sla-configs/{id}')
async updateSlaConfig(
@param.path.number('id') id: number,
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
provider: {type: 'string'},
sla_hours: {type: 'number'},
include_weekend: {type: 'boolean'},
include_public_holiday: {type: 'boolean'},
country_code: {type: 'string'},
description: {type: 'string'},
is_active: {type: 'boolean'},
},
},
},
},
})
body: Partial<{
provider: string;
sla_hours: number;
include_weekend: boolean;
include_public_holiday: boolean;
country_code: string;
description: string;
is_active: boolean;
}>,
) {
const config = await this.slaConfigRepo.findById(id);
if (!config) {
throw new HttpErrors.NotFound(`SLA configuration with ID ${id} not found`);
}
if (body.sla_hours !== undefined && body.sla_hours <= 0) {
throw new HttpErrors.BadRequest('SLA hours must be greater than 0');
}
if (body.provider && body.provider !== config.provider) {
const existing = await this.slaConfigRepo.findByProviderAndCountry(
body.provider,
body.country_code,
);
if (existing && existing.id !== id && !existing.is_deleted) {
throw new HttpErrors.Conflict(
`SLA configuration for provider "${body.provider}" already exists`,
);
}
}
const updateData = {
...body,
provider: body.provider?.trim(),
description: body.description?.trim(),
country_code: body.country_code?.toUpperCase(),
updated_at: new Date(),
};
await this.slaConfigRepo.updateById(id, updateData);
const updated = await this.slaConfigRepo.findById(id);
return {
success: true,
message: `SLA configuration updated successfully`,
data: updated,
};
}
/**
* DELETE: Soft delete SLA provider (deactivate)
*/
@del('/sla-configs/{id}')
async deleteSlaConfig(
@param.path.number('id') id: number,
) {
const config = await this.slaConfigRepo.findById(id);
if (!config) {
throw new HttpErrors.NotFound(`SLA configuration with ID ${id} not found`);
}
await this.slaConfigRepo.softDelete(id);
return {
success: true,
message: `SLA configuration deactivated and archived successfully`,
};
}
/**
* GET: Get providers dropdown list
*/
@get('/sla-configs/providers/list')
async getProvidersList() {
const providers = await this.slaConfigRepo.getProvidersList();
return {
success: true,
data: providers,
};
}
/**
* POST: Calculate SLA deadline for a provider.
*/
@post('/sla-configs/calculate-deadline')
async calculateDeadline(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
required: ['provider', 'startDateTime'],
properties: {
provider: {type: 'string', description: 'Provider/Forwarder name'},
startDateTime: {type: 'string', format: 'date-time', description: 'Start date time'},
countryCode: {type: 'string', description: 'Country code (optional)'},
},
},
},
},
})
body: {
provider: string;
startDateTime: string;
countryCode?: string;
},
) {
if (!body.provider || !body.startDateTime) {
throw new HttpErrors.BadRequest('Provider and startDateTime are required');
}
const startDateTime = new Date(body.startDateTime);
if (isNaN(startDateTime.getTime())) {
throw new HttpErrors.BadRequest('Invalid startDateTime format');
}
const result = await this.slaConfigService.calculateSlaDeadline({
provider: body.provider,
startDateTime,
countryCode: body.countryCode,
});
return {
success: true,
data: result,
};
}
/**
* POST: Get remaining time until deadline
*/
@post('/sla-configs/remaining-time')
async getRemainingTime(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
required: ['deadline'],
properties: {
deadline: {type: 'string', format: 'date-time'},
},
},
},
},
})
body: {deadline: string},
) {
const deadline = new Date(body.deadline);
if (isNaN(deadline.getTime())) {
throw new HttpErrors.BadRequest('Invalid deadline format');
}
const remaining = this.slaConfigService.getRemainingTime(deadline);
return {
success: true,
data: remaining,
};
}
// ==========================================
// HOLIDAY CALENDAR MANAGEMENT
// ==========================================
@get('/holiday-calendar')
async listHolidays(
@param.query.string('countryCode') countryCode?: string,
@param.query.boolean('activeOnly') activeOnly: boolean = true,
) {
const where: any = {};
if (activeOnly) {
where.is_active = true;
}
if (countryCode) {
where.country_code = countryCode;
}
const holidays = await this.holidayCalendarRepo.find({
where,
order: ['holiday_date ASC'],
});
return {
success: true,
data: holidays,
count: holidays.length,
};
}
@post('/holiday-calendar')
async addHoliday(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
required: ['holiday_date', 'holiday_name'],
properties: {
holiday_date: {type: 'string', format: 'date'},
holiday_name: {type: 'string'},
country_code: {type: 'string'},
},
},
},
},
})
body: {
holiday_date: string;
holiday_name: string;
country_code?: string;
},
) {
if (!body.holiday_date || !body.holiday_name) {
throw new HttpErrors.BadRequest('Holiday date and name are required');
}
const holiday = await this.holidayCalendarRepo.create({
holiday_date: new Date(body.holiday_date),
holiday_name: body.holiday_name.trim(),
country_code: body.country_code?.toUpperCase(),
is_active: true,
created_at: new Date(),
});
return {
success: true,
message: `Holiday "${body.holiday_name}" added successfully`,
data: holiday,
};
}
@put('/holiday-calendar/{id}')
async updateHoliday(
@param.path.number('id') id: number,
@requestBody()
body: Partial<{
holiday_date: string;
holiday_name: string;
country_code: string;
is_active: boolean;
}>,
) {
const holiday = await this.holidayCalendarRepo.findById(id);
if (!holiday) {
throw new HttpErrors.NotFound(`Holiday with ID ${id} not found`);
}
const updateData: any = {
...body,
updated_at: new Date(),
};
if (body.holiday_date) {
updateData.holiday_date = new Date(body.holiday_date);
}
await this.holidayCalendarRepo.updateById(id, updateData);
const updated = await this.holidayCalendarRepo.findById(id);
return {
success: true,
message: `Holiday updated successfully`,
data: updated,
};
}
@del('/holiday-calendar/{id}')
async deleteHoliday(
@param.path.number('id') id: number,
) {
const holiday = await this.holidayCalendarRepo.findById(id);
if (!holiday) {
throw new HttpErrors.NotFound(`Holiday with ID ${id} not found`);
}
await this.holidayCalendarRepo.deleteById(id);
return {
success: true,
message: `Holiday deleted successfully`,
};
}
@post('/holiday-calendar/bulk-import')
async bulkImportHolidays(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
required: ['holidays'],
properties: {
holidays: {
type: 'array',
items: {
type: 'object',
required: ['holiday_date', 'holiday_name'],
properties: {
holiday_date: {type: 'string', format: 'date'},
holiday_name: {type: 'string'},
country_code: {type: 'string'},
},
},
},
},
},
},
},
})
body: {
holidays: Array<{
holiday_date: string;
holiday_name: string;
country_code?: string;
}>;
},
) {
if (!body.holidays || body.holidays.length === 0) {
throw new HttpErrors.BadRequest('Holidays array is required and cannot be empty');
}
const holidayData = body.holidays.map(h => ({
holiday_date: new Date(h.holiday_date),
holiday_name: h.holiday_name.trim(),
country_code: h.country_code?.toUpperCase(),
is_active: true,
created_at: new Date(),
}));
const result = await this.holidayCalendarRepo.bulkInsertHolidays(holidayData);
return {
success: true,
message: `${result.length} holidays imported successfully`,
data: result,
};
}
}
|