-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceSlas.ts
70 lines (59 loc) · 2.03 KB
/
ServiceSlas.ts
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
import { ServiceSlasInput, ServiceSla, ServiceSlaData } from '../interfaces/ServiceSla';
import { MissingItemsToValidateError } from '../errors/MissingItemsToValidateError';
import { InvalidServiceDataError } from '../errors/InvalidServiceDataError';
/**
* @description Builder function to return new ServiceSlas value object.
*/
export function createNewServiceSlas(serviceSlasInput: ServiceSlasInput) {
const { serviceSlas } = serviceSlasInput;
return new ServiceSlas(serviceSlas);
}
/**
* @description The ServiceSlas value object class.
*/
class ServiceSlas {
/**
* Plain list of service names as strings.
* Used as a convenience to lookup names when validating.
*/
serviceNamesList: string[];
/**
* Services and their SLAs.
*/
serviceSlas: ServiceSla[];
constructor(serviceSlas: any[]) {
this.serviceNamesList = serviceSlas.map((service: ServiceSlaData) => service.name);
const isServiceSlaDataValid = this.validateListOfItems(serviceSlas);
if (!isServiceSlaDataValid) throw new InvalidServiceDataError('Service SLA data is invalid!');
this.serviceSlas = serviceSlas;
}
/**
* @description Higher-level utility to validate lists of items.
*/
private validateListOfItems(items: Record<string, any>[]): boolean {
if (!items || items.length === 0)
throw new MissingItemsToValidateError('Missing items in list to validate!');
const results = items.map((service: Record<string, any>) => this.validateServiceSla(service));
return results.includes(false) ? false : true;
}
/**
* @description Validate a service SLA.
*/
private validateServiceSla(service: Record<string, any>): boolean {
if (!service.name || !service.sla) return false;
if (!this.serviceNamesList.includes(service.name)) return false;
return true;
}
/**
* @description Get service SLAs.
*/
public getServiceSlas(): ServiceSla[] {
return this.serviceSlas;
}
/**
* @description Get service names.
*/
public getServiceNames(): string[] {
return this.serviceNamesList;
}
}