-
Notifications
You must be signed in to change notification settings - Fork 92
/
GetVendorListCommand.ts
71 lines (48 loc) · 2.02 KB
/
GetVendorListCommand.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
71
import {GVL} from '@iabtcf/core';
import {CmpDataReader} from '../../cmpdata';
import {Param, VendorListCallback} from '../../types';
import {Validatable, ValidationMessages, ValidationResult} from '../../validation';
import {Callback} from '../callback/Callback';
import {GlobalVendorListBldr} from '../responsebuilders';
import {BaseCommand} from './BaseCommand';
import {Command} from './Command';
/**
* Gets a version of the Global Vendors List
*/
export class GetVendorListCommand extends BaseCommand implements Command, Validatable {
public constructor(cmpData: CmpDataReader, command: string, version: number, callback: Callback, param?: Param) {
super(cmpData, command, version, callback, param);
}
/**
* Executes the get vendors list command
*/
public execute(): void {
/**
* Return a clone of the current GVL if no param/version was used. Otherwise, create a new GVL with the
* specific version.
*/
const _gvl: GVL = this.param ? new GVL(this.param as string | number) : this.cmpData.getTcModel().gvl.clone();
_gvl.readyPromise.then(() => {
const gvl = new GlobalVendorListBldr(_gvl);
this.setBaseReturnFields(gvl);
(this.callback.function as VendorListCallback)(gvl.buildResponse(), true);
}, ((reason) => this.callback.fail(reason))).catch((reason) => this.callback.fail(reason));
}
/**
* Validates the vendor list version was valid and returns the result.
* Base class validation is also handled.
* @param {boolean} failCallbackIfNotValid
* @return {ValidationResult}
*/
public validate(failCallbackIfNotValid: boolean = false): ValidationResult {
const validationResult = super.validate(failCallbackIfNotValid);
if (!this.isValidVendorListVersion()) {
validationResult.validationMessages.push(ValidationMessages.VENDOR_LIST_VERSION_INVALID);
validationResult.isValid = false;
if (failCallbackIfNotValid) {
this.callback.fail(validationResult.validationMessages);
}
}
return validationResult;
}
}