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

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 (123) hide show
  1. package/.github/pull_request_template.md +31 -31
  2. package/.github/workflows/flexplm-lib.yml +27 -27
  3. package/CHANGELOG.md +1 -1
  4. package/lib/entity-processor/base-entity-processor.d.ts +42 -0
  5. package/lib/entity-processor/base-entity-processor.js +377 -0
  6. package/lib/entity-processor/base-entity-processor.spec.d.ts +1 -0
  7. package/lib/entity-processor/base-entity-processor.spec.js +426 -0
  8. package/lib/flexplm-request.d.ts +3 -0
  9. package/lib/flexplm-request.js +34 -0
  10. package/lib/flexplm-utils.d.ts +5 -0
  11. package/lib/flexplm-utils.js +33 -0
  12. package/lib/flexplm-utils.spec.d.ts +1 -0
  13. package/lib/flexplm-utils.spec.js +26 -0
  14. package/lib/index.d.ts +22 -0
  15. package/lib/index.js +38 -0
  16. package/lib/interfaces/interfaces.d.ts +105 -0
  17. package/lib/interfaces/interfaces.js +2 -0
  18. package/lib/interfaces/item-family-changes.d.ts +20 -0
  19. package/lib/interfaces/item-family-changes.js +56 -0
  20. package/lib/interfaces/publish-change-data.d.ts +19 -0
  21. package/lib/interfaces/publish-change-data.js +32 -0
  22. package/lib/publish/base-process-publish-assortment-callback.d.ts +9 -0
  23. package/lib/publish/base-process-publish-assortment-callback.js +38 -0
  24. package/lib/publish/base-process-publish-assortment.d.ts +93 -0
  25. package/lib/publish/base-process-publish-assortment.js +944 -0
  26. package/lib/publish/base-process-publish-assortment.spec.d.ts +1 -0
  27. package/lib/publish/base-process-publish-assortment.spec.js +1670 -0
  28. package/lib/publish/mockData.d.ts +1389 -0
  29. package/lib/publish/mockData.js +4519 -0
  30. package/lib/transform/identifier-conversion-spec-mockData.d.ts +0 -0
  31. package/lib/transform/identifier-conversion-spec-mockData.js +444 -0
  32. package/lib/transform/identifier-conversion.d.ts +15 -0
  33. package/lib/transform/identifier-conversion.js +212 -0
  34. package/lib/transform/identifier-conversion.spec.d.ts +1 -0
  35. package/lib/transform/identifier-conversion.spec.js +339 -0
  36. package/lib/util/config-defaults.d.ts +8 -0
  37. package/lib/util/config-defaults.js +85 -0
  38. package/lib/util/config-defaults.spec.d.ts +1 -0
  39. package/lib/util/config-defaults.spec.js +293 -0
  40. package/lib/util/data-converter-spec-mockData.d.ts +0 -0
  41. package/lib/util/data-converter-spec-mockData.js +205 -0
  42. package/lib/util/data-converter.d.ts +39 -0
  43. package/lib/util/data-converter.js +592 -0
  44. package/lib/util/data-converter.spec.d.ts +1 -0
  45. package/lib/util/data-converter.spec.js +904 -0
  46. package/lib/util/error-response-object.d.ts +4 -0
  47. package/lib/util/error-response-object.js +47 -0
  48. package/lib/util/error-response-object.spec.d.ts +1 -0
  49. package/lib/util/error-response-object.spec.js +99 -0
  50. package/lib/util/event-short-message-status.d.ts +18 -0
  51. package/lib/util/event-short-message-status.js +22 -0
  52. package/lib/util/federation.d.ts +15 -0
  53. package/lib/util/federation.js +149 -0
  54. package/lib/util/flexplm-connect.d.ts +22 -0
  55. package/lib/util/flexplm-connect.js +176 -0
  56. package/lib/util/flexplm-connect.spec.d.ts +1 -0
  57. package/lib/util/flexplm-connect.spec.js +88 -0
  58. package/lib/util/logger-config.d.ts +1 -0
  59. package/lib/util/logger-config.js +26 -0
  60. package/lib/util/map-util-spec-mockData.d.ts +0 -0
  61. package/lib/util/map-util-spec-mockData.js +205 -0
  62. package/lib/util/map-utils.d.ts +6 -0
  63. package/lib/util/map-utils.js +15 -0
  64. package/lib/util/map-utils.spec.d.ts +1 -0
  65. package/lib/util/map-utils.spec.js +89 -0
  66. package/lib/util/mockData.d.ts +79 -0
  67. package/lib/util/mockData.js +99 -0
  68. package/lib/util/thumbnail-util.d.ts +28 -0
  69. package/lib/util/thumbnail-util.js +202 -0
  70. package/lib/util/thumbnail-util.spec.d.ts +1 -0
  71. package/lib/util/thumbnail-util.spec.js +398 -0
  72. package/lib/util/type-conversion-utils-spec-mockData.d.ts +0 -0
  73. package/lib/util/type-conversion-utils-spec-mockData.js +259 -0
  74. package/lib/util/type-conversion-utils.d.ts +23 -0
  75. package/lib/util/type-conversion-utils.js +266 -0
  76. package/lib/util/type-conversion-utils.spec.d.ts +1 -0
  77. package/lib/util/type-conversion-utils.spec.js +868 -0
  78. package/lib/util/type-defaults.d.ts +16 -0
  79. package/lib/util/type-defaults.js +221 -0
  80. package/lib/util/type-defaults.spec.d.ts +1 -0
  81. package/lib/util/type-defaults.spec.js +516 -0
  82. package/lib/util/type-utils.d.ts +13 -0
  83. package/lib/util/type-utils.js +114 -0
  84. package/lib/util/type-utils.spec.d.ts +1 -0
  85. package/lib/util/type-utils.spec.js +190 -0
  86. package/package.json +1 -1
  87. package/publish.bat +4 -4
  88. package/publish.sh +4 -4
  89. package/src/entity-processor/base-entity-processor.spec.ts +157 -0
  90. package/src/entity-processor/base-entity-processor.ts +21 -2
  91. package/src/flexplm-request.ts +28 -28
  92. package/src/flexplm-utils.spec.ts +27 -27
  93. package/src/flexplm-utils.ts +29 -29
  94. package/src/index.ts +21 -21
  95. package/src/interfaces/item-family-changes.ts +66 -66
  96. package/src/interfaces/publish-change-data.ts +42 -42
  97. package/src/publish/base-process-publish-assortment-callback.ts +50 -50
  98. package/src/transform/identifier-conversion-spec-mockData.ts +495 -495
  99. package/src/transform/identifier-conversion.spec.ts +353 -353
  100. package/src/transform/identifier-conversion.ts +281 -281
  101. package/src/util/config-defaults.spec.ts +350 -350
  102. package/src/util/config-defaults.ts +92 -92
  103. package/src/util/data-converter-spec-mockData.ts +230 -230
  104. package/src/util/error-response-object.spec.ts +115 -115
  105. package/src/util/error-response-object.ts +49 -49
  106. package/src/util/federation.ts +172 -172
  107. package/src/util/logger-config.ts +19 -19
  108. package/src/util/map-util-spec-mockData.ts +230 -230
  109. package/src/util/map-utils.spec.ts +102 -102
  110. package/src/util/map-utils.ts +40 -40
  111. package/src/util/mockData.ts +97 -97
  112. package/src/util/thumbnail-util.spec.ts +190 -0
  113. package/src/util/thumbnail-util.ts +109 -1
  114. package/src/util/type-conversion-utils.spec.ts +25 -25
  115. package/src/util/type-conversion-utils.ts +10 -9
  116. package/src/util/type-defaults.spec.ts +668 -668
  117. package/src/util/type-defaults.ts +280 -280
  118. package/src/util/type-utils.spec.ts +227 -227
  119. package/src/util/type-utils.ts +144 -144
  120. package/tsconfig.json +28 -26
  121. package/tslint.json +57 -57
  122. package/scripts/output.png +0 -0
  123. package/scripts/test-get-request.ts +0 -35
@@ -0,0 +1,202 @@
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
+ class ThumbnailUtil {
8
+ constructor(config) {
9
+ this.config = config;
10
+ this.max_thumbnail_size = 5 * 1024 * 1024;
11
+ this.entities = new sdk_1.Entities();
12
+ if (this.config['max_thumbnail_size']) {
13
+ this.max_thumbnail_size = this.config['max_thumbnail_size'];
14
+ }
15
+ }
16
+ async setOutboundThumbnail(data, event) {
17
+ if (event?.newData?.primaryViewableId && event?.newData?.largeViewableDownloadUrl) {
18
+ const primaryViewableId = event.newData.primaryViewableId;
19
+ const fileId = await this.getFileId(primaryViewableId);
20
+ if (fileId) {
21
+ const customSizes = await this.getCustomSizes();
22
+ const key = (this.isThumbnailNew(event, customSizes))
23
+ ? ThumbnailUtil.NEW_THUMBNAIL_ID
24
+ : ThumbnailUtil.EXISTING_THUMBNAIL_ID;
25
+ data[key] = fileId;
26
+ }
27
+ }
28
+ else if (event?.propertyDiffs?.largeViewableDownloadUrl?.oldValue && !event?.propertyDiffs?.largeViewableDownloadUrl?.newValue) {
29
+ data[ThumbnailUtil.NEW_THUMBNAIL_ID] = ThumbnailUtil.REMOVE_THUMBNAIL;
30
+ }
31
+ return data;
32
+ }
33
+ isThumbnailNew(event, customSizes) {
34
+ const propertyDiffs = event?.propertyDiffs;
35
+ if (propertyDiffs) {
36
+ const diffKeys = Object.keys(propertyDiffs);
37
+ const sizeKeys = ThumbnailUtil.OOB_SIZES.map(s => s.slug + 'DownloadUrl');
38
+ if (customSizes) {
39
+ sizeKeys.push(...customSizes.map(s => s.slug + 'Url'));
40
+ }
41
+ sizeKeys.push('primaryFileUrl');
42
+ console.info('diffKeys: ' + JSON.stringify(diffKeys));
43
+ console.info('sizeKeys: ' + JSON.stringify(sizeKeys));
44
+ return diffKeys.some(s => sizeKeys.includes(s));
45
+ }
46
+ return false;
47
+ }
48
+ async getFileId(primaryViewableId) {
49
+ console.info('ThumbnailUtil.getFileId()-' + primaryViewableId);
50
+ const sizes = await this.getCustomSizes();
51
+ const OOBSizes = JSON.parse(JSON.stringify(ThumbnailUtil.OOB_SIZES));
52
+ sizes.push(...OOBSizes);
53
+ console.info('sizes: ' + JSON.stringify(sizes));
54
+ const content = await this.getContentEntity(primaryViewableId, sizes);
55
+ if (!content) {
56
+ return undefined;
57
+ }
58
+ const contentKeys = Object.getOwnPropertyNames(content);
59
+ const isDebugOn = app_framework_1.Logger.isDebugOn();
60
+ let fileId = undefined;
61
+ let tempFileSize = 0;
62
+ for (const size of sizes) {
63
+ const slug = size?.slug;
64
+ if (contentKeys.includes(slug)) {
65
+ const file = content[slug];
66
+ if (isDebugOn) {
67
+ console.debug('size: ' + slug);
68
+ console.debug('fileId: ' + file['id']);
69
+ }
70
+ if (file && file['size'] > tempFileSize && file['size'] < this.max_thumbnail_size) {
71
+ tempFileSize = file['size'];
72
+ fileId = file['id'];
73
+ if (isDebugOn) {
74
+ console.debug('fileId: ' + fileId);
75
+ }
76
+ }
77
+ }
78
+ }
79
+ console.info('ThumbnailUtil.getFileId(): returning-' + fileId);
80
+ return fileId;
81
+ }
82
+ async getCustomSizes() {
83
+ const customSizes = await this.entities.get({
84
+ entityName: 'content-custom-size'
85
+ });
86
+ const sizes = [];
87
+ sizes.push(...customSizes);
88
+ return sizes;
89
+ }
90
+ async getContentEntity(primaryViewableId, sizes) {
91
+ const relations = sizes.map(s => s.slug);
92
+ relations.push('primaryFile');
93
+ const content = await this.entities.get({
94
+ entityName: 'content',
95
+ id: primaryViewableId,
96
+ relations
97
+ });
98
+ this.logContentResults(content, relations);
99
+ return content;
100
+ }
101
+ logContentResults(content, relations) {
102
+ if (app_framework_1.Logger.isDebugOn() && content && relations) {
103
+ const contentCopy = JSON.parse(JSON.stringify(content));
104
+ relations.forEach(r => {
105
+ delete contentCopy[r];
106
+ });
107
+ console.debug('content: ' + JSON.stringify(contentCopy));
108
+ }
109
+ }
110
+ async syncThumbnailToVibeIQ({ entityId, primaryViewableId, event, entityName }) {
111
+ const eventData = event.data || {};
112
+ const newThumbnailId = eventData[ThumbnailUtil.NEW_THUMBNAIL_ID];
113
+ const existingThumbnailId = eventData[ThumbnailUtil.EXISTING_THUMBNAIL_ID];
114
+ const thumbnailUrl = newThumbnailId || existingThumbnailId;
115
+ if (newThumbnailId === ThumbnailUtil.REMOVE_THUMBNAIL) {
116
+ if (primaryViewableId) {
117
+ await this.entities.delete({ entityName: 'content', id: primaryViewableId });
118
+ }
119
+ return await this.getClearPrimaryViewableUpdates();
120
+ }
121
+ if (!thumbnailUrl) {
122
+ return undefined;
123
+ }
124
+ if (!primaryViewableId) {
125
+ const content = await this.createContentFromFlexPLM(thumbnailUrl, entityId, entityName);
126
+ await this.entities.update({ entityName: 'content', id: content.id, object: { flexplmThumbnailUrl: thumbnailUrl } });
127
+ return await this.getPrimaryViewableUpdates(content);
128
+ }
129
+ const primaryViewable = await this.entities.get({ entityName: 'content', id: primaryViewableId });
130
+ if (primaryViewable?.flexplmThumbnailUrl === thumbnailUrl) {
131
+ return undefined;
132
+ }
133
+ const content = await this.createContentFromFlexPLM(thumbnailUrl, entityId, entityName);
134
+ await this.entities.update({ entityName: 'content', id: content.id, object: { flexplmThumbnailUrl: thumbnailUrl } });
135
+ const primaryUpdates = await this.getPrimaryViewableUpdates(content);
136
+ await this.entities.delete({ entityName: 'content', id: primaryViewableId });
137
+ return primaryUpdates;
138
+ }
139
+ async createContentFromFlexPLM(thumbnailUrl, entityId, entityName) {
140
+ const flexPLMConnect = new flexplm_connect_1.FlexPLMConnect(this.config);
141
+ const response = await flexPLMConnect.getRequest({
142
+ urlPath: thumbnailUrl,
143
+ includeUrlContext: false,
144
+ returnFullResponse: true,
145
+ });
146
+ const fileBuffer = await response.arrayBuffer();
147
+ const buffer = Buffer.from(fileBuffer);
148
+ const contentTypeHeader = response.headers.get('content-type');
149
+ const contentType = contentTypeHeader ? contentTypeHeader.split(';')[0] : 'application/octet-stream';
150
+ const urlParts = thumbnailUrl.split('/');
151
+ const fileName = urlParts[urlParts.length - 1] || 'thumbnail';
152
+ const contentHolderReference = `${entityName}:${entityId}`;
153
+ const content = await new sdk_1.Content().create({
154
+ fileBuffer: buffer,
155
+ fileName,
156
+ contentType,
157
+ contentHolderReference,
158
+ });
159
+ return content;
160
+ }
161
+ async getPrimaryViewableUpdates(content) {
162
+ const updates = {
163
+ primaryViewableId: content.id,
164
+ contentType: content.contentType,
165
+ fileName: content.fileName,
166
+ primaryFileUrl: content.primaryFileUrl,
167
+ largeViewableDownloadUrl: content.largeViewableUrl || content.primaryFileUrl,
168
+ mediumLargeViewableDownloadUrl: content.mediumLargeViewableUrl || content.primaryFileUrl,
169
+ mediumViewableDownloadUrl: content.mediumViewableUrl || content.primaryFileUrl,
170
+ smallViewableDownloadUrl: content.smallViewableUrl || content.primaryFileUrl,
171
+ tinyViewableDownloadUrl: content.tinyViewableUrl || content.primaryFileUrl,
172
+ };
173
+ const customSizes = await this.getCustomSizes();
174
+ for (const size of customSizes) {
175
+ updates[`${size.slug}DownloadUrl`] = content[`${size.slug}Url`] || content.primaryFileUrl;
176
+ }
177
+ return updates;
178
+ }
179
+ async getClearPrimaryViewableUpdates() {
180
+ const updates = {
181
+ primaryViewableId: null,
182
+ contentType: null,
183
+ fileName: null,
184
+ primaryFileUrl: null,
185
+ largeViewableDownloadUrl: null,
186
+ mediumLargeViewableDownloadUrl: null,
187
+ mediumViewableDownloadUrl: null,
188
+ smallViewableDownloadUrl: null,
189
+ tinyViewableDownloadUrl: null,
190
+ };
191
+ const customSizes = await this.getCustomSizes();
192
+ for (const size of customSizes) {
193
+ updates[`${size.slug}DownloadUrl`] = null;
194
+ }
195
+ return updates;
196
+ }
197
+ }
198
+ exports.ThumbnailUtil = ThumbnailUtil;
199
+ ThumbnailUtil.NEW_THUMBNAIL_ID = 'NEW_THUMBNAIL_ID';
200
+ ThumbnailUtil.EXISTING_THUMBNAIL_ID = 'EXISTING_THUMBNAIL_ID';
201
+ ThumbnailUtil.REMOVE_THUMBNAIL = 'REMOVE_THUMBNAIL';
202
+ 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
+ });