@contrail/flexplm 1.0.9 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,149 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Federation = void 0;
7
- const sdk_1 = require("@contrail/sdk");
8
- const p_limit_1 = __importDefault(require("p-limit"));
9
- const limit = (0, p_limit_1.default)(30);
10
- const FED_CONFIG = {
11
- appIdentifier: '@vibeiq/flexplm-connector',
12
- federationSchema: 'DEFAULT'
13
- };
14
- class Federation {
15
- constructor() {
16
- this.CHUNK_SIZE = 50;
17
- }
18
- async getFederatedMappedRefId(entityType, entityId) {
19
- const criteria = {
20
- reference: entityType + ':' + entityId,
21
- appIdentifier: FED_CONFIG.appIdentifier,
22
- federationSchema: FED_CONFIG.federationSchema
23
- };
24
- console.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
25
- const fedRecords = await new sdk_1.Entities().get({
26
- entityName: 'federation',
27
- criteria
28
- });
29
- const federatedId = (fedRecords[0])
30
- ? fedRecords[0]['mappedReference']
31
- : '';
32
- return federatedId;
33
- }
34
- async createFederatedRecord(eventBody) {
35
- const itemResults = eventBody.payload;
36
- for (let i = 0; i < itemResults.length; i++) {
37
- if (!itemResults[i].federatedId) {
38
- continue;
39
- }
40
- const payload = {
41
- appIdentifier: FED_CONFIG.appIdentifier,
42
- reference: itemResults[i].entityReference,
43
- mappedReference: itemResults[i].federatedId,
44
- federationSchema: FED_CONFIG.federationSchema
45
- };
46
- try {
47
- const results = await new sdk_1.Entities().create({ entityName: 'federation', object: payload });
48
- return results;
49
- }
50
- catch (error) {
51
- console.log('createFederatedRecord-error: ', error);
52
- throw new Error('Error creating federation record: ' + error.message);
53
- }
54
- }
55
- }
56
- async getFederationRecordFromMappedRefId(mappedRefId) {
57
- const criteria = {
58
- appIdentifier: FED_CONFIG.appIdentifier,
59
- mappedReference: mappedRefId,
60
- federationSchema: FED_CONFIG.federationSchema
61
- };
62
- try {
63
- const fedRecords = await new sdk_1.Entities().get({
64
- entityName: 'federation',
65
- criteria
66
- });
67
- return (fedRecords && fedRecords[0]) ? fedRecords[0] : undefined;
68
- }
69
- catch (e) {
70
- console.log('getFederationData-error: ' + e.message);
71
- }
72
- return undefined;
73
- }
74
- static getEntityId(fedRecord) {
75
- const entityString = fedRecord['reference'];
76
- const entitySplit = entityString.split(':');
77
- const entityType = entitySplit[0];
78
- const entityId = entitySplit[1];
79
- return { entityType, entityId };
80
- }
81
- async getEntityFromMappedRefId(mappedRefId) {
82
- const fedRecord = await this.getFederationRecordFromMappedRefId(mappedRefId);
83
- console.log('fedRecord: ' + JSON.stringify(fedRecord));
84
- if (!fedRecord) {
85
- console.log('Federation Record doesnt exist. Cant get entity!');
86
- return;
87
- }
88
- const { entityType, entityId } = Federation.getEntityId(fedRecord);
89
- const criteria = {
90
- id: entityId
91
- };
92
- const entities = await new sdk_1.Entities().get({
93
- entityName: entityType,
94
- criteria
95
- });
96
- const entity = (entities && entities[0]) ? entities[0] : undefined;
97
- return entity;
98
- }
99
- async getFederationRecordsFromIds(ids) {
100
- const fedRecords = [];
101
- for (let i = 0; i < ids.length; i++) {
102
- const criteria = {
103
- reference: ids[i],
104
- appIdentifier: FED_CONFIG.appIdentifier,
105
- federationSchema: FED_CONFIG.federationSchema
106
- };
107
- const recs = await new sdk_1.Entities().get({
108
- entityName: 'federation',
109
- criteria
110
- });
111
- if (recs[0]) {
112
- fedRecords.push(recs[0]);
113
- }
114
- }
115
- return fedRecords;
116
- }
117
- async getFederationRecordsFromIdsBulk(ids) {
118
- const chunks = this.splitIntoChunksByLen(ids, this.CHUNK_SIZE);
119
- const fedRecords = [];
120
- const federatedPromises = [];
121
- const entities = new sdk_1.Entities();
122
- for (const chunk of chunks) {
123
- const fedPromise = limit(async () => {
124
- const criteria = {
125
- references: chunk,
126
- appIdentifier: FED_CONFIG.appIdentifier,
127
- federationSchema: FED_CONFIG.federationSchema
128
- };
129
- const records = await entities.get({
130
- entityName: 'federation',
131
- criteria
132
- });
133
- fedRecords.push(...records);
134
- });
135
- federatedPromises.push(fedPromise);
136
- }
137
- await Promise.all(federatedPromises);
138
- return fedRecords;
139
- }
140
- splitIntoChunksByLen(arr, len) {
141
- const chunks = [], n = arr?.length;
142
- let i = 0;
143
- while (i < n) {
144
- chunks.push(arr.slice(i, i += len));
145
- }
146
- return chunks;
147
- }
148
- }
149
- exports.Federation = Federation;
@@ -1,16 +0,0 @@
1
- import { FCConfig, FlexPLMResponseData, PayloadType } from './interfaces';
2
- export declare class FlexPLMConnect {
3
- private config;
4
- private vibeEventEndpoint;
5
- payloadSendAsArray: boolean;
6
- constructor(_config: FCConfig, endPoint?: any, payloadAsArray?: any);
7
- private getRequestOptions;
8
- getCSRF(): Promise<{
9
- nonce_key: any;
10
- nonce: any;
11
- }>;
12
- sendRequest(csrf: any, payload: any): Promise<Response>;
13
- protected processRequest(payload: any): Promise<FlexPLMResponseData>;
14
- sendToFlexPLM(payload: PayloadType): Promise<FlexPLMResponseData>;
15
- sendMultipleToFlexPLM(payload: PayloadType[]): Promise<FlexPLMResponseData>;
16
- }
@@ -1,130 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FlexPLMConnect = void 0;
4
- const app_framework_1 = require("@contrail/app-framework");
5
- class FlexPLMConnect {
6
- constructor(_config, endPoint = undefined, payloadAsArray = undefined) {
7
- this.vibeEventEndpoint = '';
8
- this.payloadSendAsArray = true;
9
- this.config = _config;
10
- this.vibeEventEndpoint = (endPoint)
11
- ? endPoint
12
- : this.config.vibeEventEndpoint;
13
- this.payloadSendAsArray = (payloadAsArray != undefined)
14
- ? payloadAsArray
15
- : this.config['payloadDefaultAsArray'];
16
- }
17
- getRequestOptions(method) {
18
- const csrfOptions = {
19
- method,
20
- headers: {
21
- 'Content-Type': 'application/json',
22
- PLM_ENV: this.config.plmEnviornment,
23
- Authorization: 'Basic ' + Buffer.from(`${this.config.userName()}:${this.config.password()}`, 'binary').toString('base64')
24
- }
25
- };
26
- if (app_framework_1.Logger.isInfoOn()) {
27
- const logOptions = JSON.parse(JSON.stringify(csrfOptions));
28
- logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
29
- console.info('csrfOptions: ' + JSON.stringify(logOptions));
30
- }
31
- return csrfOptions;
32
- }
33
- async getCSRF() {
34
- const urlContext = this.config.urlContext;
35
- const csrfEndpoint = this.config.csrfEndpoint;
36
- const csrfURL = this.config.apiHost + urlContext + csrfEndpoint;
37
- console.info('csrfURL: ' + csrfURL);
38
- const csrfOptions = this.getRequestOptions('GET');
39
- const response = await fetch(csrfURL, csrfOptions);
40
- if (response.status >= 300) {
41
- const message = 'Error connecting to FlexPLM:status: ' + response.status;
42
- console.error(message);
43
- console.error(await response.text());
44
- throw new Error(message);
45
- }
46
- try {
47
- const body = await response.json();
48
- const nonce_key = body.items[0].attributes.nonce_key;
49
- const nonce = body.items[0].attributes.nonce;
50
- console.info('nonce_key: ' + nonce_key);
51
- console.info('nonce: ' + nonce);
52
- return {
53
- nonce_key,
54
- nonce
55
- };
56
- }
57
- catch (e) {
58
- const message = 'Error connecting to FlexPLM: ' + e.message;
59
- console.error(message);
60
- console.error(await response.text());
61
- throw new Error(message);
62
- }
63
- }
64
- async sendRequest(csrf, payload) {
65
- if (this.payloadSendAsArray && !Array.isArray(payload)) {
66
- payload = [payload];
67
- }
68
- const xferOptions = this.getRequestOptions('POST');
69
- xferOptions['body'] = JSON.stringify(payload);
70
- xferOptions.headers[csrf.nonce_key] = csrf.nonce;
71
- const urlContext = this.config.urlContext;
72
- const vibeEventsURL = this.config.apiHost + urlContext + '/servlet/rest' + this.vibeEventEndpoint;
73
- if (app_framework_1.Logger.isInfoOn()) {
74
- console.info('Request:');
75
- console.info('vibeEventsURL: ' + vibeEventsURL);
76
- const logOptions = JSON.parse(JSON.stringify(xferOptions));
77
- logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
78
- console.info('csrfOptions: ' + JSON.stringify(logOptions));
79
- console.info('Making call to xfer data to FlexPLM');
80
- }
81
- const eventResponse = await fetch(vibeEventsURL, xferOptions);
82
- return eventResponse;
83
- }
84
- async processRequest(payload) {
85
- if (!payload) {
86
- const message = 'No payload to send to FlexPLM';
87
- console.error(message);
88
- throw new Error(message);
89
- }
90
- if (app_framework_1.Logger.isInfoOn()) {
91
- console.info('payload: ' + JSON.stringify(payload));
92
- }
93
- const csrf = await this.getCSRF();
94
- if (!csrf) {
95
- const message = 'Failed to get CSRF nonce';
96
- console.error(message);
97
- throw new Error(message);
98
- }
99
- const response = await this.sendRequest(csrf, payload);
100
- const status = response.status;
101
- if (status >= 300) {
102
- const message = 'Error sending data to FlexPLM:status: ' + response.status;
103
- console.error(message);
104
- console.error(await response.text());
105
- throw new Error(message);
106
- }
107
- try {
108
- const data = await response.json();
109
- const res = {
110
- status,
111
- data
112
- };
113
- console.log('eventResponse.status: ' + status);
114
- return res;
115
- }
116
- catch (e) {
117
- const message = 'Error getting json data from FlexPLM: ' + e.message;
118
- console.error(message);
119
- console.error(await response.text());
120
- throw new Error(message);
121
- }
122
- }
123
- async sendToFlexPLM(payload) {
124
- return await this.processRequest(payload);
125
- }
126
- async sendMultipleToFlexPLM(payload) {
127
- return await this.processRequest(payload);
128
- }
129
- }
130
- exports.FlexPLMConnect = FlexPLMConnect;
@@ -1,91 +0,0 @@
1
- export interface FCConfig {
2
- apiKey?: string;
3
- orgSlug?: string;
4
- action?: string;
5
- configFile?: string;
6
- eventObjectClassParam?: string;
7
- eventFlexTypePathParam?: string;
8
- taskId?: string;
9
- apiHost: string;
10
- userName(): string;
11
- password(): string;
12
- plmEnviornment?: string;
13
- urlContext: string;
14
- vibeEventEndpoint: string;
15
- csrfEndpoint: string;
16
- itemPreDevelopmentLifecycleStages: string[];
17
- identifierAtts?: {
18
- [key: string]: string[];
19
- };
20
- propertyMapping?: object;
21
- }
22
- export interface ProductFederation {
23
- entityReference: string;
24
- objectClass: 'LCSProduct';
25
- federatedId?: string;
26
- vibeIQIdentifier?: string;
27
- }
28
- export interface ProductData extends ProductFederation {
29
- data: object;
30
- productSeason: {
31
- data: object;
32
- };
33
- }
34
- export interface SeasonFederation {
35
- entityReference: string;
36
- objectClass: 'LCSSeason';
37
- flexPLMSeasonName?: string;
38
- federationId?: string;
39
- }
40
- export interface SkuFederation {
41
- entityReference: string;
42
- objectClass: 'LCSSKU';
43
- LCSProduct?: ProductFederation;
44
- federatedId?: string;
45
- vibeIQIdentifier?: string;
46
- }
47
- export interface SkuData extends SkuFederation {
48
- data: object;
49
- }
50
- export interface PayloadType {
51
- eventType: string;
52
- objectClass: string;
53
- }
54
- export interface AsyncPayloadType extends PayloadType {
55
- taskId: string;
56
- }
57
- export interface AsyncEventsPayloadType extends AsyncPayloadType {
58
- events?: PayloadType[];
59
- eventsFileId?: string;
60
- eventsDownloadLink?: string;
61
- }
62
- export interface EntityPayloadType extends PayloadType {
63
- entityReference: string;
64
- federatedId?: string;
65
- flexPLMTypePath?: string;
66
- data: object;
67
- }
68
- export interface ItemPayloadType extends EntityPayloadType {
69
- LCSProduct?: ProductFederation;
70
- }
71
- export interface SeasonalPayload extends EntityPayloadType {
72
- eventType: 'UPSERT_ON_SEASON' | 'REMOVE_FROM_SEASON';
73
- objectClass: 'LCSProductSeasonLink' | 'LCSSKUSeasonLink';
74
- LCSSeason: SeasonFederation;
75
- LCSProduct?: ProductFederation;
76
- LCSSKU?: SkuFederation;
77
- }
78
- export interface ExportPayloadType extends AsyncPayloadType {
79
- flexPLMTypePath: string;
80
- }
81
- export interface FederationRecord {
82
- reference: string;
83
- mappedReference: string;
84
- appIdentifier: string;
85
- federationSchema: string;
86
- }
87
- export interface FlexPLMResponseData {
88
- status: number;
89
- data?: object;
90
- error?: string;
91
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export declare function setLoggerConfig(appConfig: any): Promise<void>;
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setLoggerConfig = void 0;
4
- const app_framework_1 = require("@contrail/app-framework");
5
- async function setLoggerConfig(appConfig) {
6
- let logLevel = app_framework_1.LogLevel.INFO;
7
- if (!appConfig.logLevel) {
8
- }
9
- else if (appConfig.logLevel === 'error') {
10
- logLevel = app_framework_1.LogLevel.ERROR;
11
- }
12
- else if (appConfig.logLevel === 'warn') {
13
- logLevel = app_framework_1.LogLevel.WARN;
14
- }
15
- else if (appConfig.logLevel === 'info') {
16
- logLevel = app_framework_1.LogLevel.INFO;
17
- }
18
- else if (appConfig.logLevel === 'debug') {
19
- logLevel = app_framework_1.LogLevel.DEBUG;
20
- }
21
- else {
22
- console.log('could not parse log level:', appConfig);
23
- }
24
- app_framework_1.Logger.setConfig({ logLevel: logLevel });
25
- }
26
- exports.setLoggerConfig = setLoggerConfig;
@@ -1,39 +0,0 @@
1
- export declare const empty_custom_sizes: any[];
2
- export declare const four_custom_sizes: {
3
- size: number;
4
- id: string;
5
- slug: string;
6
- }[];
7
- export declare const thumbnail_content_entity: {
8
- fileName: string;
9
- primaryFileId: string;
10
- primaryFileUrl: string;
11
- tinyViewableId: string;
12
- tinyViewableUrl: string;
13
- smallViewableId: string;
14
- smallViewableUrl: string;
15
- mediumViewableId: string;
16
- mediumViewableUrl: string;
17
- largeViewableId: string;
18
- largeViewableUrl: string;
19
- CS_300Id: string;
20
- CS_300Url: string;
21
- CS_500Id: string;
22
- CS_500Url: string;
23
- CS_800Id: string;
24
- CS_800Url: string;
25
- CS_1000Id: string;
26
- CS_1000Url: string;
27
- createdOn: string;
28
- orgId: string;
29
- id: string;
30
- contentType: string;
31
- createdById: string;
32
- updatedById: string;
33
- fileSize: number;
34
- };
35
- export declare const cuRficeyoStwTF_f_fileEntities: {
36
- fileName: string;
37
- id: string;
38
- size: number;
39
- }[];
@@ -1,100 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cuRficeyoStwTF_f_fileEntities = exports.thumbnail_content_entity = exports.four_custom_sizes = exports.empty_custom_sizes = void 0;
4
- exports.empty_custom_sizes = [];
5
- exports.four_custom_sizes = [
6
- {
7
- size: 1000,
8
- id: '-QwFOWnCs0Gb5jkA',
9
- slug: 'CS_1000'
10
- },
11
- {
12
- size: 300,
13
- id: 'AKXOowvhJRnkerG8',
14
- slug: 'CS_300'
15
- },
16
- {
17
- size: 500,
18
- id: '9asQ65N188RIYgnh',
19
- slug: 'CS_500'
20
- },
21
- {
22
- size: 800,
23
- id: 'xkzhwE4nJ6iRwfvP',
24
- slug: 'CS_800'
25
- }
26
- ];
27
- exports.thumbnail_content_entity = {
28
- fileName: 'JACKET 300_SOLDIER OLIVE.png',
29
- primaryFileId: 'yn2d5oHD4rXHRzyB',
30
- primaryFileUrl: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2Ff646d3cd-e23a-4689-bd16-0301d2036720.png',
31
- tinyViewableId: '6HWzJrQ-Jx1iefmj',
32
- tinyViewableUrl: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2Fb68f8652-f33f-452f-ae34-25397b4521eb.png',
33
- smallViewableId: 'D3WRW4iRmjLzp5VX',
34
- smallViewableUrl: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2F541c53f8-fd85-418e-ae28-ad7410658b90.png',
35
- mediumViewableId: 'AsRvJenpeqxksUNW',
36
- mediumViewableUrl: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2Fe803c2d3-3e1b-4d9c-9bb9-2c488f1f295b.png',
37
- largeViewableId: 'vo4N4mCd-tFrw101',
38
- largeViewableUrl: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2Fb1b7a55c-b8ba-4857-be4e-e1c3f11d8e9a.png',
39
- CS_300Id: '6A5AG33nVu3Z6ogO',
40
- CS_300Url: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2F3f2ea306-a77f-4cc3-a49a-75fb5d8b113c.png',
41
- CS_500Id: 'rBaHc1J2xdOWdbhI',
42
- CS_500Url: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2F979c8c65-b63f-4220-aa2e-11520f2f68bf.png',
43
- CS_800Id: 'ZPAeYI3JiqjQ7unY',
44
- CS_800Url: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2Fc58eeffa-2592-4698-a058-19281cda447c.png',
45
- CS_1000Id: 'Hg_CthwsD4ozopCb',
46
- CS_1000Url: 'https://api.vibeiq.com/prod/api/files/downloadUrl/5LZnKVTNxvwMqufy%2Fcontent:cuRficeyoStwTF-f%2F2c827c7b-361b-4e05-b77d-4280294f9d9c.png',
47
- createdOn: '2023-07-05T15:02:13.950Z',
48
- orgId: '5LZnKVTNxvwMqufy',
49
- id: 'cuRficeyoStwTF-f',
50
- contentType: 'image/png',
51
- createdById: 'SWIFPZeZzkBvrSLD',
52
- updatedById: 'SWIFPZeZzkBvrSLD',
53
- fileSize: 1757973
54
- };
55
- exports.cuRficeyoStwTF_f_fileEntities = [
56
- {
57
- fileName: 'JACKET 300_SOLDIER OLIVE.png',
58
- id: 'yn2d5oHD4rXHRzyB',
59
- size: 1757973
60
- },
61
- {
62
- fileName: 'JACKET 300_SOLDIER OLIVE_tiny.png',
63
- id: '6HWzJrQ-Jx1iefmj',
64
- size: 1784
65
- },
66
- {
67
- fileName: 'JACKET 300_SOLDIER OLIVE_small.png',
68
- id: 'D3WRW4iRmjLzp5VX',
69
- size: 8699
70
- },
71
- {
72
- fileName: 'JACKET 300_SOLDIER OLIVE_medium.png',
73
- id: 'AsRvJenpeqxksUNW',
74
- size: 77558
75
- },
76
- {
77
- fileName: 'JACKET 300_SOLDIER OLIVE_large.png',
78
- id: 'vo4N4mCd-tFrw101',
79
- size: 1781538
80
- },
81
- {
82
- fileName: 'JACKET 300_SOLDIER OLIVE_CS_300.png',
83
- id: '6A5AG33nVu3Z6ogO',
84
- size: 141153
85
- },
86
- {
87
- fileName: 'JACKET 300_SOLDIER OLIVE_CS_500.png',
88
- id: 'rBaHc1J2xdOWdbhI',
89
- size: 382057
90
- },
91
- {
92
- fileName: 'JACKET 300_SOLDIER OLIVE_CS_800.png',
93
- id: 'ZPAeYI3JiqjQ7unY',
94
- size: 1008702
95
- }, {
96
- fileName: 'JACKET 300_SOLDIER OLIVE_CS_1000.png',
97
- id: 'Hg_CthwsD4ozopCb',
98
- size: 1605889
99
- }
100
- ];
@@ -1,15 +0,0 @@
1
- import { FCConfig } from './interfaces';
2
- export declare class ThumbnailUtil {
3
- private config;
4
- private max_thumbnail_size;
5
- private entities;
6
- static NEW_THUMBNAIL_ID: string;
7
- static EXISTING_THUMBNAIL_ID: string;
8
- static REMOVE_THUMBNAIL: string;
9
- constructor(config: FCConfig);
10
- setOutboundThumbnail(data: any, event: any): Promise<any>;
11
- getFileId(primaryViewableId: string): Promise<string>;
12
- getCustomSizes(): Promise<any[]>;
13
- getContentEntity(primaryViewableId: any): Promise<any>;
14
- getFileEntities(fileEntityIds: any[]): Promise<any>;
15
- }
@@ -1,112 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ThumbnailUtil = void 0;
4
- const app_framework_1 = require("@contrail/app-framework");
5
- const sdk_1 = require("@contrail/sdk");
6
- class ThumbnailUtil {
7
- constructor(config) {
8
- this.config = config;
9
- this.max_thumbnail_size = 5 * 1024 * 1024;
10
- this.entities = new sdk_1.Entities();
11
- if (this.config['max_thumbnail_size']) {
12
- this.max_thumbnail_size = this.config['max_thumbnail_size'];
13
- }
14
- }
15
- async setOutboundThumbnail(data, event) {
16
- if (event?.newData?.primaryViewableId && event?.newData?.largeViewableDownloadUrl) {
17
- const primaryViewableId = event.newData.primaryViewableId;
18
- const fileId = await this.getFileId(primaryViewableId);
19
- if (fileId) {
20
- const key = (event?.propertyDiffs?.largeViewableDownloadUrl)
21
- ? ThumbnailUtil.NEW_THUMBNAIL_ID
22
- : ThumbnailUtil.EXISTING_THUMBNAIL_ID;
23
- data[key] = fileId;
24
- }
25
- }
26
- else if (event?.propertyDiffs?.largeViewableDownloadUrl?.oldValue && !event?.propertyDiffs?.largeViewableDownloadUrl?.newValue) {
27
- data[ThumbnailUtil.NEW_THUMBNAIL_ID] = ThumbnailUtil.REMOVE_THUMBNAIL;
28
- }
29
- return data;
30
- }
31
- async getFileId(primaryViewableId) {
32
- console.info('getFileId()-' + primaryViewableId);
33
- const sizes = await this.getCustomSizes();
34
- const OOBSizes = [{ slug: 'largeViewable' }, { slug: 'mediumViewable' }, { slug: 'smallViewable' }, { slug: 'tinyViewable' }];
35
- sizes.push(...OOBSizes);
36
- console.info('sizes: ' + JSON.stringify(sizes));
37
- const content = await this.getContentEntity(primaryViewableId);
38
- if (app_framework_1.Logger.isDebugOn()) {
39
- console.debug('content: ' + JSON.stringify(content));
40
- }
41
- if (!content) {
42
- return undefined;
43
- }
44
- const contentKeys = Object.getOwnPropertyNames(content);
45
- const fileEntityIds = [];
46
- for (const size of sizes) {
47
- const key = size.slug + 'Id';
48
- if (contentKeys.includes(key) && !fileEntityIds.includes(content[key])) {
49
- fileEntityIds.push(content[key]);
50
- size['fileId'] = content[key];
51
- }
52
- }
53
- const fileResults = await this.getFileEntities(fileEntityIds);
54
- if (app_framework_1.Logger.isDebugOn()) {
55
- console.debug('fileResults: ' + JSON.stringify(fileResults));
56
- }
57
- let fileId = undefined;
58
- let tempFileSize = 0;
59
- const isDebugOn = app_framework_1.Logger.isDebugOn();
60
- for (const sizeObj of sizes) {
61
- if (isDebugOn) {
62
- console.debug('size: ' + JSON.stringify(sizeObj));
63
- }
64
- if (sizeObj['fileId']) {
65
- const file = fileResults.find(f => f['id'] === sizeObj['fileId']);
66
- if (file['size'] > tempFileSize && file['size'] < this.max_thumbnail_size) {
67
- tempFileSize = file['size'];
68
- fileId = file['id'];
69
- if (isDebugOn) {
70
- console.debug('fileId: ' + fileId);
71
- }
72
- }
73
- }
74
- }
75
- console.info('getFileId(): returning-' + fileId);
76
- return fileId;
77
- }
78
- async getCustomSizes() {
79
- const customSizes = await this.entities.get({
80
- entityName: 'content-custom-size'
81
- });
82
- const sizes = [];
83
- sizes.push(...customSizes);
84
- return sizes;
85
- }
86
- async getContentEntity(primaryViewableId) {
87
- const criteria = {
88
- id: primaryViewableId
89
- };
90
- const contentResults = await this.entities.get({
91
- entityName: 'content',
92
- criteria
93
- });
94
- console.info('contentResults: ' + JSON.stringify(contentResults));
95
- const content = (contentResults && contentResults[0]) ? contentResults[0] : undefined;
96
- return content;
97
- }
98
- async getFileEntities(fileEntityIds) {
99
- const criteria = {
100
- ids: fileEntityIds
101
- };
102
- const fileResults = await this.entities.get({
103
- entityName: 'file',
104
- criteria
105
- });
106
- return fileResults;
107
- }
108
- }
109
- exports.ThumbnailUtil = ThumbnailUtil;
110
- ThumbnailUtil.NEW_THUMBNAIL_ID = 'NEW_THUMBNAIL_ID';
111
- ThumbnailUtil.EXISTING_THUMBNAIL_ID = 'EXISTING_THUMBNAIL_ID';
112
- ThumbnailUtil.REMOVE_THUMBNAIL = 'REMOVE_THUMBNAIL';