@contrail/flexplm 1.3.0-alpha.0 → 1.3.0-alpha.4

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.
Files changed (124) hide show
  1. package/.claude/settings.local.json +2 -1
  2. package/.github/pull_request_template.md +31 -31
  3. package/.github/workflows/flexplm-lib.yml +27 -27
  4. package/CHANGELOG.md +1 -1
  5. package/lib/entity-processor/base-entity-processor.d.ts +42 -0
  6. package/lib/entity-processor/base-entity-processor.js +377 -0
  7. package/lib/entity-processor/base-entity-processor.spec.d.ts +1 -0
  8. package/lib/entity-processor/base-entity-processor.spec.js +426 -0
  9. package/lib/flexplm-request.d.ts +3 -0
  10. package/lib/flexplm-request.js +34 -0
  11. package/lib/flexplm-utils.d.ts +5 -0
  12. package/lib/flexplm-utils.js +33 -0
  13. package/lib/flexplm-utils.spec.d.ts +1 -0
  14. package/lib/flexplm-utils.spec.js +26 -0
  15. package/lib/index.d.ts +22 -0
  16. package/lib/index.js +38 -0
  17. package/lib/interfaces/interfaces.d.ts +105 -0
  18. package/lib/interfaces/interfaces.js +2 -0
  19. package/lib/interfaces/item-family-changes.d.ts +20 -0
  20. package/lib/interfaces/item-family-changes.js +56 -0
  21. package/lib/interfaces/publish-change-data.d.ts +19 -0
  22. package/lib/interfaces/publish-change-data.js +32 -0
  23. package/lib/publish/base-process-publish-assortment-callback.d.ts +9 -0
  24. package/lib/publish/base-process-publish-assortment-callback.js +38 -0
  25. package/lib/publish/base-process-publish-assortment.d.ts +93 -0
  26. package/lib/publish/base-process-publish-assortment.js +944 -0
  27. package/lib/publish/base-process-publish-assortment.spec.d.ts +1 -0
  28. package/lib/publish/base-process-publish-assortment.spec.js +1670 -0
  29. package/lib/publish/mockData.d.ts +1389 -0
  30. package/lib/publish/mockData.js +4519 -0
  31. package/lib/transform/identifier-conversion-spec-mockData.d.ts +0 -0
  32. package/lib/transform/identifier-conversion-spec-mockData.js +444 -0
  33. package/lib/transform/identifier-conversion.d.ts +15 -0
  34. package/lib/transform/identifier-conversion.js +212 -0
  35. package/lib/transform/identifier-conversion.spec.d.ts +1 -0
  36. package/lib/transform/identifier-conversion.spec.js +339 -0
  37. package/lib/util/config-defaults.d.ts +8 -0
  38. package/lib/util/config-defaults.js +85 -0
  39. package/lib/util/config-defaults.spec.d.ts +1 -0
  40. package/lib/util/config-defaults.spec.js +293 -0
  41. package/lib/util/data-converter-spec-mockData.d.ts +0 -0
  42. package/lib/util/data-converter-spec-mockData.js +205 -0
  43. package/lib/util/data-converter.d.ts +39 -0
  44. package/lib/util/data-converter.js +592 -0
  45. package/lib/util/data-converter.spec.d.ts +1 -0
  46. package/lib/util/data-converter.spec.js +904 -0
  47. package/lib/util/error-response-object.d.ts +4 -0
  48. package/lib/util/error-response-object.js +47 -0
  49. package/lib/util/error-response-object.spec.d.ts +1 -0
  50. package/lib/util/error-response-object.spec.js +99 -0
  51. package/lib/util/event-short-message-status.d.ts +18 -0
  52. package/lib/util/event-short-message-status.js +22 -0
  53. package/lib/util/federation.d.ts +15 -0
  54. package/lib/util/federation.js +149 -0
  55. package/lib/util/flexplm-connect.d.ts +22 -0
  56. package/lib/util/flexplm-connect.js +176 -0
  57. package/lib/util/flexplm-connect.spec.d.ts +1 -0
  58. package/lib/util/flexplm-connect.spec.js +88 -0
  59. package/lib/util/logger-config.d.ts +1 -0
  60. package/lib/util/logger-config.js +26 -0
  61. package/lib/util/map-util-spec-mockData.d.ts +0 -0
  62. package/lib/util/map-util-spec-mockData.js +205 -0
  63. package/lib/util/map-utils.d.ts +6 -0
  64. package/lib/util/map-utils.js +15 -0
  65. package/lib/util/map-utils.spec.d.ts +1 -0
  66. package/lib/util/map-utils.spec.js +89 -0
  67. package/lib/util/mockData.d.ts +80 -0
  68. package/lib/util/mockData.js +103 -0
  69. package/lib/util/thumbnail-util.d.ts +34 -0
  70. package/lib/util/thumbnail-util.js +211 -0
  71. package/lib/util/thumbnail-util.spec.d.ts +1 -0
  72. package/lib/util/thumbnail-util.spec.js +398 -0
  73. package/lib/util/type-conversion-utils-spec-mockData.d.ts +0 -0
  74. package/lib/util/type-conversion-utils-spec-mockData.js +259 -0
  75. package/lib/util/type-conversion-utils.d.ts +23 -0
  76. package/lib/util/type-conversion-utils.js +266 -0
  77. package/lib/util/type-conversion-utils.spec.d.ts +1 -0
  78. package/lib/util/type-conversion-utils.spec.js +868 -0
  79. package/lib/util/type-defaults.d.ts +16 -0
  80. package/lib/util/type-defaults.js +221 -0
  81. package/lib/util/type-defaults.spec.d.ts +1 -0
  82. package/lib/util/type-defaults.spec.js +516 -0
  83. package/lib/util/type-utils.d.ts +13 -0
  84. package/lib/util/type-utils.js +114 -0
  85. package/lib/util/type-utils.spec.d.ts +1 -0
  86. package/lib/util/type-utils.spec.js +190 -0
  87. package/package.json +1 -1
  88. package/publish.bat +4 -4
  89. package/publish.sh +4 -4
  90. package/src/entity-processor/base-entity-processor.spec.ts +157 -0
  91. package/src/entity-processor/base-entity-processor.ts +21 -2
  92. package/src/flexplm-request.ts +28 -28
  93. package/src/flexplm-utils.spec.ts +27 -27
  94. package/src/flexplm-utils.ts +29 -29
  95. package/src/index.ts +21 -21
  96. package/src/interfaces/item-family-changes.ts +66 -66
  97. package/src/interfaces/publish-change-data.ts +42 -42
  98. package/src/publish/base-process-publish-assortment-callback.ts +50 -50
  99. package/src/transform/identifier-conversion-spec-mockData.ts +495 -495
  100. package/src/transform/identifier-conversion.spec.ts +353 -353
  101. package/src/transform/identifier-conversion.ts +281 -281
  102. package/src/util/config-defaults.spec.ts +350 -350
  103. package/src/util/config-defaults.ts +92 -92
  104. package/src/util/data-converter-spec-mockData.ts +230 -230
  105. package/src/util/error-response-object.spec.ts +115 -115
  106. package/src/util/error-response-object.ts +49 -49
  107. package/src/util/federation.ts +172 -172
  108. package/src/util/logger-config.ts +19 -19
  109. package/src/util/map-util-spec-mockData.ts +230 -230
  110. package/src/util/map-utils.spec.ts +102 -102
  111. package/src/util/map-utils.ts +40 -40
  112. package/src/util/mockData.ts +101 -97
  113. package/src/util/thumbnail-util.spec.ts +190 -0
  114. package/src/util/thumbnail-util.ts +126 -5
  115. package/src/util/type-conversion-utils.spec.ts +25 -25
  116. package/src/util/type-conversion-utils.ts +10 -9
  117. package/src/util/type-defaults.spec.ts +668 -668
  118. package/src/util/type-defaults.ts +280 -280
  119. package/src/util/type-utils.spec.ts +227 -227
  120. package/src/util/type-utils.ts +144 -144
  121. package/tsconfig.json +28 -26
  122. package/tslint.json +57 -57
  123. package/scripts/output.png +0 -0
  124. package/scripts/test-get-request.ts +0 -35
@@ -0,0 +1,211 @@
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
+ const flexplm_connect_1 = require("./flexplm-connect");
7
+ ;
8
+ class ThumbnailUtil {
9
+ constructor(config) {
10
+ this.config = config;
11
+ this.max_thumbnail_size = 5 * 1024 * 1024;
12
+ this.entities = new sdk_1.Entities();
13
+ if (this.config['max_thumbnail_size']) {
14
+ this.max_thumbnail_size = this.config['max_thumbnail_size'];
15
+ }
16
+ }
17
+ async setOutboundThumbnail(data, event) {
18
+ if (event?.newData?.primaryViewableId && event?.newData?.largeViewableDownloadUrl) {
19
+ const primaryViewableId = event.newData.primaryViewableId;
20
+ const fileId = await this.getFileId(primaryViewableId);
21
+ if (fileId) {
22
+ const customSizes = await this.getCustomSizes();
23
+ const key = (this.isThumbnailNew(event, customSizes))
24
+ ? ThumbnailUtil.NEW_THUMBNAIL_ID
25
+ : ThumbnailUtil.EXISTING_THUMBNAIL_ID;
26
+ data[key] = fileId;
27
+ }
28
+ }
29
+ else if (event?.propertyDiffs?.largeViewableDownloadUrl?.oldValue && !event?.propertyDiffs?.largeViewableDownloadUrl?.newValue) {
30
+ data[ThumbnailUtil.NEW_THUMBNAIL_ID] = ThumbnailUtil.REMOVE_THUMBNAIL;
31
+ }
32
+ return data;
33
+ }
34
+ isThumbnailNew(event, customSizes) {
35
+ const propertyDiffs = event?.propertyDiffs;
36
+ if (propertyDiffs) {
37
+ const diffKeys = Object.keys(propertyDiffs);
38
+ const sizeKeys = ThumbnailUtil.OOB_SIZES.map(s => s.slug + 'DownloadUrl');
39
+ if (customSizes) {
40
+ sizeKeys.push(...customSizes.map(s => s.slug + 'Url'));
41
+ }
42
+ sizeKeys.push('primaryFileUrl');
43
+ console.info('diffKeys: ' + JSON.stringify(diffKeys));
44
+ console.info('sizeKeys: ' + JSON.stringify(sizeKeys));
45
+ return diffKeys.some(s => sizeKeys.includes(s));
46
+ }
47
+ return false;
48
+ }
49
+ async getFileId(primaryViewableId) {
50
+ console.info('ThumbnailUtil.getFileId()-' + primaryViewableId);
51
+ const sizes = await this.getCustomSizes();
52
+ const OOBSizes = JSON.parse(JSON.stringify(ThumbnailUtil.OOB_SIZES));
53
+ sizes.push(...OOBSizes);
54
+ console.info('sizes: ' + JSON.stringify(sizes));
55
+ const content = await this.getContentEntity(primaryViewableId, sizes);
56
+ if (!content) {
57
+ return undefined;
58
+ }
59
+ const contentKeys = Object.getOwnPropertyNames(content);
60
+ const isDebugOn = app_framework_1.Logger.isDebugOn();
61
+ let fileId = undefined;
62
+ let tempFileSize = 0;
63
+ for (const size of sizes) {
64
+ const slug = size?.slug;
65
+ if (contentKeys.includes(slug)) {
66
+ const file = content[slug];
67
+ if (isDebugOn) {
68
+ console.debug('size: ' + slug);
69
+ console.debug('fileId: ' + file['id']);
70
+ }
71
+ if (file && file['size'] > tempFileSize && file['size'] < this.max_thumbnail_size) {
72
+ tempFileSize = file['size'];
73
+ fileId = file['id'];
74
+ if (isDebugOn) {
75
+ console.debug('fileId: ' + fileId);
76
+ }
77
+ }
78
+ }
79
+ }
80
+ console.info('ThumbnailUtil.getFileId(): returning-' + fileId);
81
+ return fileId;
82
+ }
83
+ async getCustomSizes() {
84
+ const customSizes = await this.entities.get({
85
+ entityName: 'content-custom-size'
86
+ });
87
+ const sizes = [];
88
+ sizes.push(...customSizes);
89
+ return sizes;
90
+ }
91
+ async getContentEntity(primaryViewableId, sizes) {
92
+ const relations = sizes.map(s => s.slug);
93
+ relations.push('primaryFile');
94
+ const content = await this.entities.get({
95
+ entityName: 'content',
96
+ id: primaryViewableId,
97
+ relations
98
+ });
99
+ this.logContentResults(content, relations);
100
+ return content;
101
+ }
102
+ logContentResults(content, relations) {
103
+ if (app_framework_1.Logger.isDebugOn() && content && relations) {
104
+ const contentCopy = JSON.parse(JSON.stringify(content));
105
+ relations.forEach(r => {
106
+ delete contentCopy[r];
107
+ });
108
+ console.debug('content: ' + JSON.stringify(contentCopy));
109
+ }
110
+ }
111
+ async syncThumbnailToVibeIQ({ entityId, primaryViewableId, event, entityName }) {
112
+ console.debug(`syncThumbnailToVibeIQ: entityId=${entityId}, primaryViewableId=${primaryViewableId}, entityName=${entityName}`);
113
+ const eventData = event.data || {};
114
+ const newThumbnailId = eventData[ThumbnailUtil.NEW_THUMBNAIL_ID];
115
+ const existingThumbnailId = eventData[ThumbnailUtil.EXISTING_THUMBNAIL_ID];
116
+ const thumbnailUrl = newThumbnailId || existingThumbnailId;
117
+ if (newThumbnailId === ThumbnailUtil.REMOVE_THUMBNAIL) {
118
+ if (primaryViewableId) {
119
+ await this.entities.delete({ entityName: 'content', id: primaryViewableId });
120
+ }
121
+ const clearUpdates = await this.getClearPrimaryViewableUpdates();
122
+ console.debug(`syncThumbnailToVibeIQ: returning clear updates for entityId=${entityId}`);
123
+ return clearUpdates;
124
+ }
125
+ if (!thumbnailUrl) {
126
+ console.debug(`syncThumbnailToVibeIQ: no thumbnail URL for entityId=${entityId}`);
127
+ return undefined;
128
+ }
129
+ if (!primaryViewableId) {
130
+ const content = await this.createContentFromFlexPLM(thumbnailUrl, entityId, entityName);
131
+ await this.entities.update({ entityName: 'content', id: content.id, object: { flexplmThumbnailUrl: thumbnailUrl } });
132
+ const primaryUpdates = await this.getPrimaryViewableUpdates(content);
133
+ console.debug(`syncThumbnailToVibeIQ: created new content ${content.id} for entityId=${entityId}`);
134
+ return primaryUpdates;
135
+ }
136
+ const primaryViewable = await this.entities.get({ entityName: 'content', id: primaryViewableId });
137
+ if (primaryViewable?.flexplmThumbnailUrl === thumbnailUrl) {
138
+ console.debug(`syncThumbnailToVibeIQ: thumbnail already synced for entityId=${entityId}`);
139
+ return undefined;
140
+ }
141
+ const content = await this.createContentFromFlexPLM(thumbnailUrl, entityId, entityName);
142
+ await this.entities.update({ entityName: 'content', id: content.id, object: { flexplmThumbnailUrl: thumbnailUrl } });
143
+ const primaryUpdates = await this.getPrimaryViewableUpdates(content);
144
+ await this.entities.delete({ entityName: 'content', id: primaryViewableId });
145
+ console.debug(`syncThumbnailToVibeIQ: replaced content ${primaryViewableId} with ${content.id} for entityId=${entityId}`);
146
+ return primaryUpdates;
147
+ }
148
+ async createContentFromFlexPLM(thumbnailUrl, entityId, entityName) {
149
+ const flexPLMConnect = new flexplm_connect_1.FlexPLMConnect(this.config);
150
+ const response = await flexPLMConnect.getRequest({
151
+ urlPath: thumbnailUrl,
152
+ includeUrlContext: false,
153
+ returnFullResponse: true,
154
+ });
155
+ const fileBuffer = await response.arrayBuffer();
156
+ const buffer = Buffer.from(fileBuffer);
157
+ const contentTypeHeader = response.headers.get('content-type');
158
+ const contentType = contentTypeHeader ? contentTypeHeader.split(';')[0] : 'application/octet-stream';
159
+ const urlParts = thumbnailUrl.split('/');
160
+ const fileName = urlParts[urlParts.length - 1] || 'thumbnail';
161
+ const contentHolderReference = `${entityName}:${entityId}`;
162
+ const content = await new sdk_1.Content().create({
163
+ fileBuffer: buffer,
164
+ fileName,
165
+ contentType,
166
+ contentHolderReference,
167
+ });
168
+ return content;
169
+ }
170
+ async getPrimaryViewableUpdates(content) {
171
+ const updates = {
172
+ primaryViewableId: content.id,
173
+ contentType: content.contentType,
174
+ fileName: content.fileName,
175
+ primaryFileUrl: content.primaryFileUrl,
176
+ largeViewableDownloadUrl: content.largeViewableUrl || content.primaryFileUrl,
177
+ mediumLargeViewableDownloadUrl: content.mediumLargeViewableUrl || content.primaryFileUrl,
178
+ mediumViewableDownloadUrl: content.mediumViewableUrl || content.primaryFileUrl,
179
+ smallViewableDownloadUrl: content.smallViewableUrl || content.primaryFileUrl,
180
+ tinyViewableDownloadUrl: content.tinyViewableUrl || content.primaryFileUrl,
181
+ };
182
+ const customSizes = await this.getCustomSizes();
183
+ for (const size of customSizes) {
184
+ updates[`${size.slug}DownloadUrl`] = content[`${size.slug}Url`] || content.primaryFileUrl;
185
+ }
186
+ return updates;
187
+ }
188
+ async getClearPrimaryViewableUpdates() {
189
+ const updates = {
190
+ primaryViewableId: null,
191
+ contentType: null,
192
+ fileName: null,
193
+ primaryFileUrl: null,
194
+ largeViewableDownloadUrl: null,
195
+ mediumLargeViewableDownloadUrl: null,
196
+ mediumViewableDownloadUrl: null,
197
+ smallViewableDownloadUrl: null,
198
+ tinyViewableDownloadUrl: null,
199
+ };
200
+ const customSizes = await this.getCustomSizes();
201
+ for (const size of customSizes) {
202
+ updates[`${size.slug}DownloadUrl`] = null;
203
+ }
204
+ return updates;
205
+ }
206
+ }
207
+ exports.ThumbnailUtil = ThumbnailUtil;
208
+ ThumbnailUtil.NEW_THUMBNAIL_ID = 'NEW_THUMBNAIL_ID';
209
+ ThumbnailUtil.EXISTING_THUMBNAIL_ID = 'EXISTING_THUMBNAIL_ID';
210
+ ThumbnailUtil.REMOVE_THUMBNAIL = 'REMOVE_THUMBNAIL';
211
+ ThumbnailUtil.OOB_SIZES = [{ slug: 'largeViewable' }, { slug: 'mediumLargeViewable' }, { slug: 'mediumViewable' }, { slug: 'smallViewable' }, { slug: 'tinyViewable' }];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,398 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const thumbnail_util_1 = require("./thumbnail-util");
4
+ const mockData_1 = require("./mockData");
5
+ const mockEntitiesGet = jest.fn();
6
+ const mockEntitiesUpdate = jest.fn();
7
+ const mockEntitiesDelete = jest.fn();
8
+ const mockContentCreate = jest.fn();
9
+ jest.mock('@contrail/sdk', () => {
10
+ return {
11
+ Entities: jest.fn().mockImplementation(() => ({
12
+ get: mockEntitiesGet,
13
+ update: mockEntitiesUpdate,
14
+ delete: mockEntitiesDelete,
15
+ })),
16
+ Content: jest.fn().mockImplementation(() => ({
17
+ create: mockContentCreate,
18
+ })),
19
+ };
20
+ });
21
+ const mockGetRequest = jest.fn();
22
+ jest.mock('./flexplm-connect', () => {
23
+ return {
24
+ FlexPLMConnect: jest.fn().mockImplementation(() => ({
25
+ getRequest: mockGetRequest,
26
+ })),
27
+ };
28
+ });
29
+ describe('ThumbnailUtil Tests', () => {
30
+ const config = {};
31
+ describe('setOutboundThumbnail()', () => {
32
+ const tu = new thumbnail_util_1.ThumbnailUtil(config);
33
+ beforeEach(() => {
34
+ jest.clearAllMocks();
35
+ });
36
+ it('no data', async () => {
37
+ const data = {};
38
+ const event = {};
39
+ const results = await tu.setOutboundThumbnail(data, event);
40
+ const keys = Object.keys(results);
41
+ expect(results).toEqual(data);
42
+ expect(keys.length).toEqual(0);
43
+ });
44
+ it('remove thumbnail', async () => {
45
+ const data = {};
46
+ const event = {
47
+ propertyDiffs: {
48
+ largeViewableDownloadUrl: {
49
+ oldValue: '123',
50
+ newValue: null
51
+ }
52
+ }
53
+ };
54
+ const results = await tu.setOutboundThumbnail(data, event);
55
+ const keys = Object.keys(results);
56
+ expect(keys.length).toEqual(1);
57
+ expect(keys[0]).toEqual(thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID);
58
+ expect(results[thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID]).toEqual(thumbnail_util_1.ThumbnailUtil.REMOVE_THUMBNAIL);
59
+ });
60
+ it('calling getFileId - new thumbnail', async () => {
61
+ const data = {};
62
+ const newDownloadUrl = 'https://api.vibeiq.com/dev/api/files/downloadUrl/w3ckfeGAD8ViOZj-%2Fcontent:E1iBQuWbr74lcdcw%2F9e30ds9o-d34b-451e-ae59-b4km36018d5a.png';
63
+ const oldDownloadUrl = 'xxx';
64
+ const thumbnailId = 'Eey3ZOiqdrUA84F8';
65
+ const event = {
66
+ newData: {
67
+ primaryViewableId: 'm5bJa4RtTLUtKBP5',
68
+ largeViewableDownloadUrl: newDownloadUrl
69
+ },
70
+ propertyDiffs: {
71
+ largeViewableDownloadUrl: {
72
+ newValue: newDownloadUrl,
73
+ oldValue: oldDownloadUrl
74
+ }
75
+ }
76
+ };
77
+ const spyCustomSizes = jest.spyOn(tu, 'getCustomSizes');
78
+ spyCustomSizes.mockReturnValue(Promise.resolve(mockData_1.empty_custom_sizes));
79
+ const spyGetFileId = jest.spyOn(tu, 'getFileId');
80
+ spyGetFileId.mockReturnValue(Promise.resolve(thumbnailId));
81
+ const results = await tu.setOutboundThumbnail(data, event);
82
+ const keys = Object.keys(results);
83
+ expect(keys.length).toEqual(1);
84
+ expect(keys[0]).toEqual(thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID);
85
+ const thumbnailValue = results[thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID];
86
+ expect(thumbnailValue).toEqual(thumbnailId);
87
+ });
88
+ it('calling getFileId - existing thumbnail', async () => {
89
+ const data = {};
90
+ const newDownloadUrl = 'https://api.vibeiq.com/dev/api/files/downloadUrl/w3ckfeGAD8ViOZj-%2Fcontent:E1iBQuWbr74lcdcw%2F9e30ds9o-d34b-451e-ae59-b4km36018d5a.png';
91
+ const thumbnailId = 'Eey3ZOiqdrUA84F8';
92
+ const event = {
93
+ newData: {
94
+ primaryViewableId: 'm5bJa4RtTLUtKBP5',
95
+ largeViewableDownloadUrl: newDownloadUrl
96
+ },
97
+ propertyDiffs: {}
98
+ };
99
+ const spyCustomSizes = jest.spyOn(tu, 'getCustomSizes');
100
+ spyCustomSizes.mockReturnValue(Promise.resolve(mockData_1.empty_custom_sizes));
101
+ const spyGetFileId = jest.spyOn(tu, 'getFileId');
102
+ spyGetFileId.mockReturnValue(Promise.resolve(thumbnailId));
103
+ const results = await tu.setOutboundThumbnail(data, event);
104
+ const keys = Object.keys(results);
105
+ expect(keys.length).toEqual(1);
106
+ expect(keys[0]).toEqual(thumbnail_util_1.ThumbnailUtil.EXISTING_THUMBNAIL_ID);
107
+ const thumbnailValue = results[thumbnail_util_1.ThumbnailUtil.EXISTING_THUMBNAIL_ID];
108
+ expect(thumbnailValue).toEqual(thumbnailId);
109
+ });
110
+ });
111
+ describe('getFileId', () => {
112
+ const primaryFileId = 'yn2d5oHD4rXHRzyB';
113
+ beforeEach(() => {
114
+ jest.clearAllMocks();
115
+ });
116
+ it('no custom sizes, default max_thumbnail_size - result large', async () => {
117
+ const tu = new thumbnail_util_1.ThumbnailUtil(config);
118
+ const spyCustomSizes = jest.spyOn(tu, 'getCustomSizes');
119
+ spyCustomSizes.mockReturnValue(Promise.resolve(mockData_1.empty_custom_sizes));
120
+ const spyContentEntity = jest.spyOn(tu, 'getContentEntity');
121
+ const content = JSON.parse(JSON.stringify(mockData_1.thumbnail_content_entity));
122
+ spyContentEntity.mockReturnValue(Promise.resolve(content));
123
+ const results = await tu.getFileId(primaryFileId);
124
+ expect(results).toEqual('vo4N4mCd-tFrw101');
125
+ });
126
+ it('no custom sizes, 750 * 1_024 max_thumbnail_size - result medium', async () => {
127
+ const config1 = {
128
+ max_thumbnail_size: 750 * 1024
129
+ };
130
+ const tu = new thumbnail_util_1.ThumbnailUtil(config1);
131
+ const spyCustomSizes = jest.spyOn(tu, 'getCustomSizes');
132
+ spyCustomSizes.mockReturnValue(Promise.resolve(mockData_1.empty_custom_sizes));
133
+ const spyContentEntity = jest.spyOn(tu, 'getContentEntity');
134
+ const content = mockData_1.thumbnail_content_entity;
135
+ spyContentEntity.mockReturnValue(Promise.resolve(content));
136
+ const results = await tu.getFileId(primaryFileId);
137
+ expect(results).toEqual('AsRvJenpeqxksUNW');
138
+ });
139
+ it('custom sizes, 750 * 1_024 max_thumbnail_size - result CS_500Id', async () => {
140
+ const config1 = {
141
+ max_thumbnail_size: 750 * 1024
142
+ };
143
+ const tu = new thumbnail_util_1.ThumbnailUtil(config1);
144
+ const spyCustomSizes = jest.spyOn(tu, 'getCustomSizes');
145
+ spyCustomSizes.mockReturnValue(Promise.resolve(mockData_1.four_custom_sizes));
146
+ const spyContentEntity = jest.spyOn(tu, 'getContentEntity');
147
+ const content = mockData_1.thumbnail_content_entity;
148
+ spyContentEntity.mockReturnValue(Promise.resolve(content));
149
+ const results = await tu.getFileId(primaryFileId);
150
+ expect(results).toEqual('rBaHc1J2xdOWdbhI');
151
+ });
152
+ });
153
+ describe('Test isThumbnailNew', () => {
154
+ const tu = new thumbnail_util_1.ThumbnailUtil(config);
155
+ it('no propertyDiffs', () => {
156
+ const event = {
157
+ newData: {
158
+ name: 'Test'
159
+ },
160
+ oldData: {
161
+ name: 'Test'
162
+ }
163
+ };
164
+ const results = tu.isThumbnailNew(event, undefined);
165
+ expect(results).toBeFalsy();
166
+ });
167
+ it('empty propertyDiffs', () => {
168
+ const event = {
169
+ newData: {
170
+ name: 'Test'
171
+ },
172
+ oldData: {
173
+ name: 'Test'
174
+ },
175
+ propertyDiffs: {}
176
+ };
177
+ const results = tu.isThumbnailNew(event, undefined);
178
+ expect(results).toBeFalsy();
179
+ });
180
+ it('propertyDiffs diff property', () => {
181
+ const event = {
182
+ newData: {
183
+ name: 'Test1'
184
+ },
185
+ oldData: {
186
+ name: 'Test'
187
+ },
188
+ propertyDiffs: {
189
+ name: {
190
+ propertyName: 'name',
191
+ oldValue: 'Test',
192
+ newValue: 'Test1'
193
+ }
194
+ }
195
+ };
196
+ const results = tu.isThumbnailNew(event, undefined);
197
+ expect(results).toBeFalsy();
198
+ });
199
+ it('propertyDiffs OOB property', () => {
200
+ const event = {
201
+ newData: {
202
+ name: 'Test1'
203
+ },
204
+ oldData: {
205
+ name: 'Test'
206
+ },
207
+ propertyDiffs: {
208
+ mediumLargeViewableDownloadUrl: {
209
+ propertyName: 'mediumLargeViewableDownloadUrl',
210
+ oldValue: '',
211
+ newValue: 'Test1'
212
+ }
213
+ }
214
+ };
215
+ const results = tu.isThumbnailNew(event, undefined);
216
+ expect(results).toBeTruthy();
217
+ });
218
+ it('propertyDiffs custom property', () => {
219
+ const event = {
220
+ newData: {
221
+ name: 'Test1'
222
+ },
223
+ oldData: {
224
+ name: 'Test'
225
+ },
226
+ propertyDiffs: {
227
+ RL_1000Url: {
228
+ propertyName: 'RL_1000Url',
229
+ oldValue: '',
230
+ newValue: 'Test1'
231
+ }
232
+ }
233
+ };
234
+ const results = tu.isThumbnailNew(event, [{ slug: 'RL_1000' }]);
235
+ expect(results).toBeTruthy();
236
+ });
237
+ });
238
+ describe('logContentResults', () => {
239
+ it('no content - doesnt error', () => {
240
+ const tu = new thumbnail_util_1.ThumbnailUtil(config);
241
+ const content = undefined;
242
+ const relations = [];
243
+ tu.logContentResults(content, relations);
244
+ });
245
+ it('no relations - doesnt error', () => {
246
+ const tu = new thumbnail_util_1.ThumbnailUtil(config);
247
+ const content = undefined;
248
+ const relations = undefined;
249
+ tu.logContentResults(content, relations);
250
+ });
251
+ it('content and relations - logs', () => {
252
+ const tu = new thumbnail_util_1.ThumbnailUtil(config);
253
+ const content = {
254
+ id: '123',
255
+ name: 'Test',
256
+ primaryFile: { id: 'file123' }
257
+ };
258
+ const relations = ['primaryFile'];
259
+ tu.logContentResults(content, relations);
260
+ expect(Object.keys(content)).toHaveLength(3);
261
+ expect(content).toHaveProperty('primaryFile');
262
+ expect(content.primaryFile).toHaveProperty('id');
263
+ expect(content.primaryFile.id).toEqual('file123');
264
+ });
265
+ });
266
+ describe('syncThumbnailToVibeIQ', () => {
267
+ let tu;
268
+ beforeEach(() => {
269
+ jest.clearAllMocks();
270
+ tu = new thumbnail_util_1.ThumbnailUtil(config);
271
+ mockEntitiesGet.mockImplementation((opts) => {
272
+ if (opts.entityName === 'content-custom-size')
273
+ return Promise.resolve([]);
274
+ return Promise.resolve({});
275
+ });
276
+ mockEntitiesUpdate.mockImplementation((opts) => Promise.resolve({ id: opts.id }));
277
+ mockEntitiesDelete.mockImplementation((opts) => Promise.resolve({ id: opts.id }));
278
+ });
279
+ it('returns undefined when no thumbnail IDs in event data', async () => {
280
+ const event = { data: {} };
281
+ const result = await tu.syncThumbnailToVibeIQ({ entityId: 'entity1', event, entityName: 'color' });
282
+ expect(result).toBeUndefined();
283
+ expect(mockEntitiesUpdate).not.toHaveBeenCalled();
284
+ expect(mockEntitiesDelete).not.toHaveBeenCalled();
285
+ expect(mockContentCreate).not.toHaveBeenCalled();
286
+ });
287
+ it('returns undefined when event.data is undefined', async () => {
288
+ const event = {};
289
+ const result = await tu.syncThumbnailToVibeIQ({ entityId: 'entity1', event, entityName: 'color' });
290
+ expect(result).toBeUndefined();
291
+ expect(mockEntitiesUpdate).not.toHaveBeenCalled();
292
+ expect(mockEntitiesDelete).not.toHaveBeenCalled();
293
+ });
294
+ it('REMOVE_THUMBNAIL with existing primaryViewableId deletes content and returns clear updates', async () => {
295
+ const event = { data: { [thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID]: thumbnail_util_1.ThumbnailUtil.REMOVE_THUMBNAIL } };
296
+ const result = await tu.syncThumbnailToVibeIQ({ entityId: 'entity1', primaryViewableId: 'pv1', event, entityName: 'color' });
297
+ expect(mockEntitiesDelete).toHaveBeenCalledWith({ entityName: 'content', id: 'pv1' });
298
+ expect(result).toEqual(expect.objectContaining({ primaryViewableId: null, primaryFileUrl: null }));
299
+ });
300
+ it('REMOVE_THUMBNAIL with no primaryViewableId returns clear updates without deleting', async () => {
301
+ const event = { data: { [thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID]: thumbnail_util_1.ThumbnailUtil.REMOVE_THUMBNAIL } };
302
+ const result = await tu.syncThumbnailToVibeIQ({ entityId: 'entity1', event, entityName: 'item' });
303
+ expect(mockEntitiesDelete).not.toHaveBeenCalled();
304
+ expect(result).toEqual(expect.objectContaining({ primaryViewableId: null }));
305
+ });
306
+ it('creates new content when no primaryViewableId exists and returns primary viewable updates', async () => {
307
+ const mockResponse = {
308
+ arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(8)),
309
+ headers: { get: jest.fn().mockReturnValue('image/png') },
310
+ };
311
+ mockGetRequest.mockResolvedValue(mockResponse);
312
+ const createdContent = {
313
+ id: 'newContent1',
314
+ contentType: 'image/png',
315
+ fileName: 'thumb.png',
316
+ primaryFileUrl: 'https://files/primary.png',
317
+ largeViewableUrl: 'https://files/large.png',
318
+ mediumLargeViewableUrl: null,
319
+ mediumViewableUrl: null,
320
+ smallViewableUrl: null,
321
+ tinyViewableUrl: null,
322
+ };
323
+ mockContentCreate.mockResolvedValue(createdContent);
324
+ const event = { data: { [thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID]: '/rest/thumbnail/thumb.png' } };
325
+ const result = await tu.syncThumbnailToVibeIQ({ entityId: 'entity1', event, entityName: 'color' });
326
+ expect(mockGetRequest).toHaveBeenCalledWith({ urlPath: '/rest/thumbnail/thumb.png', includeUrlContext: false, returnFullResponse: true });
327
+ expect(mockContentCreate).toHaveBeenCalledWith(expect.objectContaining({
328
+ fileName: 'thumb.png',
329
+ contentType: 'image/png',
330
+ contentHolderReference: 'color:entity1',
331
+ }));
332
+ expect(mockEntitiesUpdate).toHaveBeenCalledWith(expect.objectContaining({
333
+ entityName: 'content',
334
+ id: 'newContent1',
335
+ object: { flexplmThumbnailUrl: '/rest/thumbnail/thumb.png' },
336
+ }));
337
+ expect(result).toEqual(expect.objectContaining({
338
+ primaryViewableId: 'newContent1',
339
+ primaryFileUrl: 'https://files/primary.png',
340
+ largeViewableDownloadUrl: 'https://files/large.png',
341
+ }));
342
+ });
343
+ it('replaces content when primaryViewable.flexplmThumbnailUrl differs and returns updates', async () => {
344
+ mockEntitiesGet.mockImplementation((opts) => {
345
+ if (opts.entityName === 'content-custom-size')
346
+ return Promise.resolve([]);
347
+ if (opts.entityName === 'content' && opts.id === 'oldPv') {
348
+ return Promise.resolve({ id: 'oldPv', flexplmThumbnailUrl: '/rest/thumbnail/old.png' });
349
+ }
350
+ return Promise.resolve({});
351
+ });
352
+ const mockResponse = {
353
+ arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(8)),
354
+ headers: { get: jest.fn().mockReturnValue('image/jpeg') },
355
+ };
356
+ mockGetRequest.mockResolvedValue(mockResponse);
357
+ const createdContent = {
358
+ id: 'newContent2',
359
+ contentType: 'image/jpeg',
360
+ fileName: 'new.jpg',
361
+ primaryFileUrl: 'https://files/new-primary.jpg',
362
+ largeViewableUrl: null,
363
+ mediumLargeViewableUrl: null,
364
+ mediumViewableUrl: null,
365
+ smallViewableUrl: null,
366
+ tinyViewableUrl: null,
367
+ };
368
+ mockContentCreate.mockResolvedValue(createdContent);
369
+ const event = { data: { [thumbnail_util_1.ThumbnailUtil.NEW_THUMBNAIL_ID]: '/rest/thumbnail/new.jpg' } };
370
+ const result = await tu.syncThumbnailToVibeIQ({ entityId: 'entity1', primaryViewableId: 'oldPv', event, entityName: 'item' });
371
+ expect(mockContentCreate).toHaveBeenCalled();
372
+ expect(mockEntitiesUpdate).toHaveBeenCalledWith(expect.objectContaining({
373
+ entityName: 'content',
374
+ id: 'newContent2',
375
+ object: { flexplmThumbnailUrl: '/rest/thumbnail/new.jpg' },
376
+ }));
377
+ expect(result).toEqual(expect.objectContaining({ primaryViewableId: 'newContent2' }));
378
+ expect(mockEntitiesDelete).toHaveBeenCalledWith({ entityName: 'content', id: 'oldPv' });
379
+ });
380
+ it('returns undefined when primaryViewable.flexplmThumbnailUrl matches', async () => {
381
+ const thumbnailUrl = '/rest/thumbnail/same.png';
382
+ mockEntitiesGet.mockImplementation((opts) => {
383
+ if (opts.entityName === 'content-custom-size')
384
+ return Promise.resolve([]);
385
+ if (opts.entityName === 'content' && opts.id === 'pv1') {
386
+ return Promise.resolve({ id: 'pv1', flexplmThumbnailUrl: thumbnailUrl });
387
+ }
388
+ return Promise.resolve({});
389
+ });
390
+ const event = { data: { [thumbnail_util_1.ThumbnailUtil.EXISTING_THUMBNAIL_ID]: thumbnailUrl } };
391
+ const result = await tu.syncThumbnailToVibeIQ({ entityId: 'entity1', primaryViewableId: 'pv1', event, entityName: 'color' });
392
+ expect(result).toBeUndefined();
393
+ expect(mockContentCreate).not.toHaveBeenCalled();
394
+ expect(mockEntitiesUpdate).not.toHaveBeenCalled();
395
+ expect(mockEntitiesDelete).not.toHaveBeenCalled();
396
+ });
397
+ });
398
+ });