@contrail/cli 3.6.0-alpha-8-alpha.82b70ea → 3.6.0-alpha.5ecc188
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.
- package/README.md +26 -181
- package/dist/api/types.d.ts +0 -35
- package/dist/api/types.js +1 -49
- package/dist/auth/login.d.ts +4 -0
- package/dist/auth/login.js +136 -56
- package/dist/auth/login.spec.js +117 -3
- package/dist/auth/sign-in-error.d.ts +7 -0
- package/dist/auth/sign-in-error.js +24 -0
- package/dist/auth/sign-in-error.spec.js +22 -0
- package/dist/auth/sso.d.ts +38 -0
- package/dist/auth/sso.js +184 -0
- package/dist/auth/sso.spec.js +191 -0
- package/dist/basecommand.d.ts +3 -0
- package/dist/basecommand.js +10 -3
- package/dist/commands/app/getApiKey.js +2 -2
- package/dist/commands/login.d.ts +12 -0
- package/dist/commands/login.js +25 -0
- package/dist/common.d.ts +1 -4
- package/dist/common.js +1 -18
- package/dist/helpers/config/config.d.ts +3 -3
- package/dist/helpers/config/config.js +3 -3
- package/dist/helpers/file-helpers.d.ts +0 -1
- package/dist/helpers/file-helpers.js +3 -20
- package/dist/logger.js +1 -4
- package/node_modules/@contrail/entity-compare/package.json +3 -6
- package/node_modules/@contrail/loader/lib/common/conditional-columns.js +1 -1
- package/node_modules/@contrail/loader/lib/constants.d.ts +8 -0
- package/node_modules/@contrail/loader/lib/constants.js +9 -1
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.d.ts +4 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.js +57 -0
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.spec.js +492 -34
- package/node_modules/@contrail/loader/lib/pipeline/validate.js +2 -52
- package/node_modules/@contrail/loader/lib/pipeline/validate.spec.js +0 -85
- package/node_modules/@contrail/loader/package.json +2 -2
- package/node_modules/@contrail/sdk/.github/workflows/publish-to-npm.yml +4 -1
- package/node_modules/@contrail/sdk/lib/auth/login.js +1 -1
- package/node_modules/@contrail/sdk/lib/config/config.d.ts +0 -3
- package/node_modules/@contrail/sdk/lib/config/config.js +7 -13
- package/node_modules/@contrail/sdk/lib/core/request.js +2 -6
- package/node_modules/@contrail/sdk/lib/entities/entities.client.d.ts +0 -2
- package/node_modules/@contrail/sdk/lib/entities/entities.client.js +1 -3
- package/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.js +0 -1
- package/node_modules/@contrail/sdk/lib/index.d.ts +0 -1
- package/node_modules/@contrail/sdk/lib/index.js +0 -1
- package/node_modules/@contrail/sdk/package.json +1 -1
- package/oclif.manifest.json +50 -449
- package/package.json +12 -5
- package/dist/commands/entities/create.d.ts +0 -50
- package/dist/commands/entities/create.js +0 -640
- package/dist/commands/entities/create.spec.js +0 -583
- package/dist/commands/entities/delete.d.ts +0 -20
- package/dist/commands/entities/delete.js +0 -140
- package/dist/commands/entities/get.d.ts +0 -25
- package/dist/commands/entities/get.js +0 -276
- package/dist/commands/entities/grammar.spec.js +0 -79
- package/dist/commands/entities/output.spec.d.ts +0 -1
- package/dist/commands/entities/output.spec.js +0 -72
- package/dist/commands/entities/update.d.ts +0 -32
- package/dist/commands/entities/update.js +0 -161
- package/dist/helpers/entities/assortment-items.d.ts +0 -11
- package/dist/helpers/entities/assortment-items.js +0 -76
- package/dist/helpers/entities/batchFetch.d.ts +0 -8
- package/dist/helpers/entities/batchFetch.js +0 -49
- package/dist/helpers/entities/index.d.ts +0 -3
- package/dist/helpers/entities/index.js +0 -6
- package/dist/helpers/entities/items.d.ts +0 -8
- package/dist/helpers/entities/items.js +0 -88
- package/dist/helpers/entities/items.spec.d.ts +0 -1
- package/dist/helpers/entities/items.spec.js +0 -19
- package/dist/helpers/entities/project-items.d.ts +0 -2
- package/dist/helpers/entities/project-items.js +0 -44
- package/dist/helpers/propertySpecifications/dateRanges.d.ts +0 -34
- package/dist/helpers/propertySpecifications/dateRanges.js +0 -83
- package/dist/helpers/propertySpecifications/dateRanges.spec.d.ts +0 -1
- package/dist/helpers/propertySpecifications/dateRanges.spec.js +0 -91
- package/dist/helpers/propertySpecifications/propertySpecifications.d.ts +0 -40
- package/dist/helpers/propertySpecifications/propertySpecifications.js +0 -104
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.d.ts +0 -1
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.js +0 -387
- package/dist/logger.spec.d.ts +0 -1
- package/dist/logger.spec.js +0 -12
- package/node_modules/@contrail/sdk/lib/image-search/image-search.d.ts +0 -34
- package/node_modules/@contrail/sdk/lib/image-search/image-search.js +0 -42
- package/node_modules/@contrail/sdk/lib/image-search/index.d.ts +0 -1
- package/node_modules/@contrail/sdk/lib/image-search/index.js +0 -17
- package/node_modules/@contrail/sdk/scripts/replicate-bug.js +0 -69
- /package/dist/{commands/entities/create.spec.d.ts → auth/sign-in-error.spec.d.ts} +0 -0
- /package/dist/{commands/entities/grammar.spec.d.ts → auth/sso.spec.d.ts} +0 -0
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.summarizeSettledUpdates = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const p_limit_1 = tslib_1.__importDefault(require("p-limit"));
|
|
6
|
-
const core_1 = require("@oclif/core");
|
|
7
|
-
const basecommand_1 = require("../../basecommand");
|
|
8
|
-
const sdk_1 = require("@contrail/sdk");
|
|
9
|
-
const login_1 = require("../../auth/login");
|
|
10
|
-
const common_1 = require("../../common");
|
|
11
|
-
const propertySpecifications_1 = require("../../helpers/propertySpecifications/propertySpecifications");
|
|
12
|
-
const file_helpers_1 = require("../../helpers/file-helpers");
|
|
13
|
-
class UpdateEntity extends basecommand_1.BaseCommand {
|
|
14
|
-
static description = 'Update an entity';
|
|
15
|
-
static examples = [
|
|
16
|
-
'contrail entities update color 123 -- name=obsidian hex=000000',
|
|
17
|
-
'contrail entities update item id1 id2 id3 -- status=archived # apply the same change to several entities',
|
|
18
|
-
'contrail entities update color 123 -- description= notes=\'""\' # Unset description, set notes to empty string',
|
|
19
|
-
];
|
|
20
|
-
static flags = {
|
|
21
|
-
...basecommand_1.BaseCommand.baseFlags,
|
|
22
|
-
skipHydrateRelationships: core_1.Flags.boolean({
|
|
23
|
-
char: 'S',
|
|
24
|
-
description: 'Skip hydrating relationships when updating the entity',
|
|
25
|
-
required: false,
|
|
26
|
-
default: false,
|
|
27
|
-
}),
|
|
28
|
-
dryRun: core_1.Flags.boolean({
|
|
29
|
-
char: 'd',
|
|
30
|
-
description: 'If true, does not update the entities but simulates the update',
|
|
31
|
-
required: false,
|
|
32
|
-
default: false,
|
|
33
|
-
}),
|
|
34
|
-
inputFile: core_1.Flags.string({
|
|
35
|
-
char: 'i',
|
|
36
|
-
description: 'A file with a JSON array of {id, ...} objects; each object is one entity to update.',
|
|
37
|
-
required: false,
|
|
38
|
-
}),
|
|
39
|
-
suffix: core_1.Flags.string({
|
|
40
|
-
description: 'Append a sub-route to the entity endpoint (e.g. --suffix=synchronous targets /<entities>/synchronous)',
|
|
41
|
-
required: false,
|
|
42
|
-
}),
|
|
43
|
-
apiVersion: core_1.Flags.string({
|
|
44
|
-
description: 'Api-Version header to send; selects the V1 or V2 variant of the endpoint',
|
|
45
|
-
options: ['1', '2'],
|
|
46
|
-
required: false,
|
|
47
|
-
}),
|
|
48
|
-
};
|
|
49
|
-
static args = {
|
|
50
|
-
entityName: core_1.Args.string({
|
|
51
|
-
description: 'The name of the entity type to update (type paths are not supported)',
|
|
52
|
-
required: true,
|
|
53
|
-
}),
|
|
54
|
-
ids: core_1.Args.string({
|
|
55
|
-
description: 'One or more entity ids to update',
|
|
56
|
-
required: false,
|
|
57
|
-
}),
|
|
58
|
-
};
|
|
59
|
-
static strict = false;
|
|
60
|
-
async init() {
|
|
61
|
-
(0, common_1.routeConsoleLogToStderr)();
|
|
62
|
-
await super.init();
|
|
63
|
-
}
|
|
64
|
-
async run() {
|
|
65
|
-
await (0, login_1.authUser)();
|
|
66
|
-
const { args, argv } = await this.parse(UpdateEntity);
|
|
67
|
-
const { entityName } = args;
|
|
68
|
-
const { dryRun: isDryRun, inputFile: inputFilename, skipHydrateRelationships, suffix, apiVersion, } = this.flags;
|
|
69
|
-
if (entityName.includes(':')) {
|
|
70
|
-
this.error(`update takes an entity name, not a type path. Use: contrail entities update ${entityName.split(':')[0]} <ID...>`);
|
|
71
|
-
}
|
|
72
|
-
const { ids: positionalIds, specs } = (0, propertySpecifications_1.partitionIdsAndSpecs)(argv.slice(1));
|
|
73
|
-
const propertySpec = (0, propertySpecifications_1.parsePropertySpecificationsIntoMap)(specs);
|
|
74
|
-
if (propertySpec.has('id') || propertySpec.has('ids')) {
|
|
75
|
-
this.error('The -- payload is values to write. Pass ids positionally: contrail entities update <typePath> <ID...> -- slug=value');
|
|
76
|
-
}
|
|
77
|
-
if (inputFilename && positionalIds.length > 0) {
|
|
78
|
-
this.error('You cannot use --inputFile and positional ids together. Please specify one or the other.');
|
|
79
|
-
}
|
|
80
|
-
if (!inputFilename && positionalIds.length === 0) {
|
|
81
|
-
this.error('You must provide one or more ids or an input file (-i) of entities to update.');
|
|
82
|
-
}
|
|
83
|
-
if (positionalIds.length > 0 && propertySpec.size === 0) {
|
|
84
|
-
this.error('Nothing to update: pass values to write after --, e.g. -- name=jet');
|
|
85
|
-
}
|
|
86
|
-
const entitiesToUpdate = inputFilename
|
|
87
|
-
? (0, file_helpers_1.parseFileAsJsonArray)(inputFilename)
|
|
88
|
-
: positionalIds.map((id) => ({ id }));
|
|
89
|
-
const skipped = entitiesToUpdate.filter(({ id }) => !id).length;
|
|
90
|
-
const entitiesWithId = entitiesToUpdate.filter(({ id }) => id);
|
|
91
|
-
// will be applied to all entities
|
|
92
|
-
// JSON.stringify drops undefined keys, so an unset has to travel as an explicit null
|
|
93
|
-
const fixedValues = Object.fromEntries([...propertySpec.entries()].map(([slug, value]) => [slug, value ?? null]));
|
|
94
|
-
async function updateEntity(entity) {
|
|
95
|
-
const newEntityState = { ...entity, ...fixedValues };
|
|
96
|
-
const id = entity.id;
|
|
97
|
-
return isDryRun
|
|
98
|
-
? mockUpdateById(entityName, id, newEntityState, { skipHydrateRelationships })
|
|
99
|
-
: updateEntityById(entityName, id, newEntityState, {
|
|
100
|
-
skipHydrateRelationships,
|
|
101
|
-
suffix,
|
|
102
|
-
apiVersion: apiVersion,
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
try {
|
|
106
|
-
const limit = (0, p_limit_1.default)(20);
|
|
107
|
-
const settled = await Promise.allSettled(entitiesWithId.map((entity) => limit(() => updateEntity(entity))));
|
|
108
|
-
const summary = summarizeSettledUpdates(entitiesWithId, settled, skipped);
|
|
109
|
-
if (isDryRun) {
|
|
110
|
-
console.error('Dry run. Update skipped. Changes would be:');
|
|
111
|
-
}
|
|
112
|
-
this.log(JSON.stringify(summary, null, 2));
|
|
113
|
-
if (summary.failed.length > 0) {
|
|
114
|
-
console.error(`${summary.failed.length} of ${entitiesWithId.length} updates failed.`);
|
|
115
|
-
process.exitCode = 1;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
catch (e) {
|
|
119
|
-
this.error(`Error updating entities: ${e.message}`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
exports.default = UpdateEntity;
|
|
124
|
-
function summarizeSettledUpdates(inputs, settled, skipped) {
|
|
125
|
-
const updated = [];
|
|
126
|
-
const failed = [];
|
|
127
|
-
settled.forEach((result, index) => {
|
|
128
|
-
if (result.status === 'fulfilled') {
|
|
129
|
-
updated.push(result.value);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
failed.push({
|
|
133
|
-
id: inputs[index].id,
|
|
134
|
-
error: (0, common_1.sdkErrorMessage)(result.reason),
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
return { updated, failed, skipped };
|
|
139
|
-
}
|
|
140
|
-
exports.summarizeSettledUpdates = summarizeSettledUpdates;
|
|
141
|
-
async function updateEntityById(entityName, id, changes, options = {}) {
|
|
142
|
-
const client = new sdk_1.Entities();
|
|
143
|
-
const { skipHydrateRelationships, suffix, apiVersion } = options;
|
|
144
|
-
return client.update({
|
|
145
|
-
entityName,
|
|
146
|
-
id,
|
|
147
|
-
object: changes,
|
|
148
|
-
skipHydrateRelationships,
|
|
149
|
-
suffix,
|
|
150
|
-
apiVersion,
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
async function mockUpdateById(entityName, id, changes, options = {}) {
|
|
154
|
-
const client = new sdk_1.Entities();
|
|
155
|
-
const { skipHydrateRelationships } = options;
|
|
156
|
-
const entity = await client.get({ entityName, id, skipHydrateRelationships });
|
|
157
|
-
if (!entity) {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
return { ...entity, ...changes };
|
|
161
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Item } from '@contrail/entity-types';
|
|
2
|
-
import { API_VERSION } from '@contrail/sdk';
|
|
3
|
-
export declare function createAssortmentItem(assortmentItem: {
|
|
4
|
-
assortmentId: string;
|
|
5
|
-
itemId: string;
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
}, options?: {
|
|
8
|
-
skipHydrateRelationships?: boolean;
|
|
9
|
-
apiVersion?: API_VERSION;
|
|
10
|
-
}): Promise<any>;
|
|
11
|
-
export declare function findItemsNotInAssortment(assortmentId: string, count?: number): Promise<Item[]>;
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findItemsNotInAssortment = exports.createAssortmentItem = void 0;
|
|
4
|
-
const sdk_1 = require("@contrail/sdk");
|
|
5
|
-
const batchFetch_1 = require("./batchFetch");
|
|
6
|
-
const items_1 = require("./items");
|
|
7
|
-
async function createAssortmentItem(assortmentItem, options = {}) {
|
|
8
|
-
const client = new sdk_1.Entities();
|
|
9
|
-
const { assortmentId, itemId } = assortmentItem;
|
|
10
|
-
const { skipHydrateRelationships = false, apiVersion } = options;
|
|
11
|
-
// add item to assortment
|
|
12
|
-
await client.create({
|
|
13
|
-
entityName: 'assortment',
|
|
14
|
-
id: assortmentId,
|
|
15
|
-
relation: 'items',
|
|
16
|
-
object: { itemIds: [itemId] },
|
|
17
|
-
apiVersion,
|
|
18
|
-
});
|
|
19
|
-
// Update assortment item with properties
|
|
20
|
-
const updated = await client.update({
|
|
21
|
-
entityName: 'assortment-item',
|
|
22
|
-
id: `${assortmentId}:${itemId}`,
|
|
23
|
-
object: assortmentItem,
|
|
24
|
-
skipHydrateRelationships,
|
|
25
|
-
apiVersion,
|
|
26
|
-
});
|
|
27
|
-
return updated;
|
|
28
|
-
}
|
|
29
|
-
exports.createAssortmentItem = createAssortmentItem;
|
|
30
|
-
// Methods to find an item not in the assortment
|
|
31
|
-
const ASSORTMENT_ITEMS_CACHE_BY_ASSORTMENT_ID = new Map();
|
|
32
|
-
const ASSORTMENT_ITEMS_CACHE_BY_ITEM_ID = new Map();
|
|
33
|
-
async function cacheAssortmentItemsByAssortmentId(assortmentId) {
|
|
34
|
-
if (!assortmentId) {
|
|
35
|
-
throw new Error('Assortment ID is required to cache assortment items');
|
|
36
|
-
}
|
|
37
|
-
if (ASSORTMENT_ITEMS_CACHE_BY_ASSORTMENT_ID.has(assortmentId)) {
|
|
38
|
-
return; // Already cached
|
|
39
|
-
}
|
|
40
|
-
const assortmentItems = await (0, batchFetch_1.batchFetch)('assortment-item', {
|
|
41
|
-
criteria: { assortmentId },
|
|
42
|
-
customParams: { propertySlugs: 'id,assortmentId,itemId' },
|
|
43
|
-
});
|
|
44
|
-
ASSORTMENT_ITEMS_CACHE_BY_ASSORTMENT_ID.set(assortmentId, assortmentItems);
|
|
45
|
-
assortmentItems.forEach((item) => {
|
|
46
|
-
if (!ASSORTMENT_ITEMS_CACHE_BY_ITEM_ID.has(item.itemId)) {
|
|
47
|
-
ASSORTMENT_ITEMS_CACHE_BY_ITEM_ID.set(item.itemId, []);
|
|
48
|
-
}
|
|
49
|
-
ASSORTMENT_ITEMS_CACHE_BY_ITEM_ID.get(item.itemId).push(item);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
async function getAssortmentItemsByAssortmentId(assortmentId) {
|
|
53
|
-
if (!assortmentId) {
|
|
54
|
-
throw new Error('Assortment ID is required to get assortment items');
|
|
55
|
-
}
|
|
56
|
-
await cacheAssortmentItemsByAssortmentId(assortmentId);
|
|
57
|
-
const assortmentItems = ASSORTMENT_ITEMS_CACHE_BY_ASSORTMENT_ID.get(assortmentId);
|
|
58
|
-
if (!assortmentItems) {
|
|
59
|
-
throw new Error(`No assortment items found for assortment ID ${assortmentId}`);
|
|
60
|
-
}
|
|
61
|
-
return assortmentItems;
|
|
62
|
-
}
|
|
63
|
-
async function findItemsNotInAssortment(assortmentId, count) {
|
|
64
|
-
if (!assortmentId) {
|
|
65
|
-
throw new Error('Assortment ID is required to find items not in assortment');
|
|
66
|
-
}
|
|
67
|
-
const assortmentItemsInAssortment = await getAssortmentItemsByAssortmentId(assortmentId);
|
|
68
|
-
const items = await (0, items_1.getManyItems)(count + assortmentItemsInAssortment.length);
|
|
69
|
-
const assortmentItemIds = new Set(assortmentItemsInAssortment.map((ai) => ai.itemId));
|
|
70
|
-
const itemsNotInAssortment = items.filter((item) => !assortmentItemIds.has(item.id));
|
|
71
|
-
if (itemsNotInAssortment.length === 0) {
|
|
72
|
-
throw new Error(`All items are already in assortment ${assortmentId}`);
|
|
73
|
-
}
|
|
74
|
-
return itemsNotInAssortment.sort((a, b) => a.id.localeCompare(b.id)); // sorting ensures consistent order of response
|
|
75
|
-
}
|
|
76
|
-
exports.findItemsNotInAssortment = findItemsNotInAssortment;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { AssortmentItem, ProjectItem } from '@contrail/entity-types';
|
|
2
|
-
import { EntitiesClientGetOptions } from '@contrail/sdk';
|
|
3
|
-
export declare function canBatchFetchEntity(entityName: string): entityName is keyof BatchFetchableEntityMap;
|
|
4
|
-
export interface BatchFetchableEntityMap {
|
|
5
|
-
'project-item': ProjectItem;
|
|
6
|
-
'assortment-item': AssortmentItem;
|
|
7
|
-
}
|
|
8
|
-
export declare function batchFetch<K extends keyof BatchFetchableEntityMap>(entityName: K, options: EntitiesClientGetOptions): Promise<BatchFetchableEntityMap[K][]>;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.batchFetch = exports.canBatchFetchEntity = void 0;
|
|
4
|
-
const sdk_1 = require("@contrail/sdk");
|
|
5
|
-
const util_1 = require("@contrail/util");
|
|
6
|
-
// Maps entity names to their types for batch fetching.
|
|
7
|
-
// This allows for type-safe batch fetching of entities
|
|
8
|
-
// such that the Type returned always matches the entity name.
|
|
9
|
-
const BATCH_FETCHABLE_ENTITIES = ['assortment-item', 'project-item'];
|
|
10
|
-
function canBatchFetchEntity(entityName) {
|
|
11
|
-
return BATCH_FETCHABLE_ENTITIES.includes(entityName);
|
|
12
|
-
}
|
|
13
|
-
exports.canBatchFetchEntity = canBatchFetchEntity;
|
|
14
|
-
async function batchFetch(entityName, options) {
|
|
15
|
-
const response = await requestBatchFetch(entityName, options);
|
|
16
|
-
return parseBatchFetchResponse(response);
|
|
17
|
-
}
|
|
18
|
-
exports.batchFetch = batchFetch;
|
|
19
|
-
async function requestBatchFetch(entityName, options) {
|
|
20
|
-
const client = new sdk_1.Entities();
|
|
21
|
-
// NOTE: There is probably a helper somewhere else that manages this conversion
|
|
22
|
-
const optionsClone = { ...options };
|
|
23
|
-
if (optionsClone.criteria) {
|
|
24
|
-
optionsClone.where = optionsClone.criteria;
|
|
25
|
-
}
|
|
26
|
-
return client.create({
|
|
27
|
-
entityName: entityName,
|
|
28
|
-
suffix: 'batch-fetch',
|
|
29
|
-
object: optionsClone,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
async function parseBatchFetchResponse(response) {
|
|
33
|
-
if ('results' in response) {
|
|
34
|
-
return response.results;
|
|
35
|
-
}
|
|
36
|
-
else if ('resultsCompressedString' in response) {
|
|
37
|
-
const compressedString = response.resultsCompressedString;
|
|
38
|
-
const jsonData = (0, util_1.decompressFromString)(compressedString);
|
|
39
|
-
return jsonData;
|
|
40
|
-
}
|
|
41
|
-
else if ('resultsDownloadUrl' in response) {
|
|
42
|
-
const downloadUrl = response.resultsDownloadUrl;
|
|
43
|
-
const data = await fetch(downloadUrl).then((res) => res.json());
|
|
44
|
-
return data;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
throw new Error('Invalid batch fetch response format');
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./assortment-items"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./items"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./project-items"), exports);
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Item } from '@contrail/entity-types';
|
|
2
|
-
export type SparseItem = Pick<Item, 'id' | 'itemFamilyId' | 'roles' | 'name'>;
|
|
3
|
-
export declare function getAllItemsUsingCacheFallback(): Promise<SparseItem[]>;
|
|
4
|
-
export declare function getManyItems(maxCount?: number): Promise<SparseItem[]>;
|
|
5
|
-
export declare function getItemFamilies(count: number): Promise<SparseItem[] | undefined>;
|
|
6
|
-
export declare function getFamilyName(item: SparseItem): Promise<string[] | undefined>;
|
|
7
|
-
export declare const Roles: readonly ["option", "family"];
|
|
8
|
-
export type Role = (typeof Roles)[number];
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Roles = exports.getFamilyName = exports.getItemFamilies = exports.getManyItems = exports.getAllItemsUsingCacheFallback = void 0;
|
|
4
|
-
const sdk_1 = require("@contrail/sdk");
|
|
5
|
-
const ITEMS_CACHE_BY_ID = new Map();
|
|
6
|
-
async function getAllItemsUsingCacheFallback() {
|
|
7
|
-
if (ITEMS_CACHE_BY_ID.size > 0) {
|
|
8
|
-
return Array.from(ITEMS_CACHE_BY_ID.values());
|
|
9
|
-
}
|
|
10
|
-
const items = await getManyItems();
|
|
11
|
-
items.forEach((item) => {
|
|
12
|
-
ITEMS_CACHE_BY_ID.set(item.id, item);
|
|
13
|
-
});
|
|
14
|
-
return items;
|
|
15
|
-
}
|
|
16
|
-
exports.getAllItemsUsingCacheFallback = getAllItemsUsingCacheFallback;
|
|
17
|
-
async function getManyItems(maxCount = 10000) {
|
|
18
|
-
const client = new sdk_1.Entities();
|
|
19
|
-
// paginate to get all items
|
|
20
|
-
// Note: This will only return the first 100 assortments by default.
|
|
21
|
-
const items = [];
|
|
22
|
-
let nextPageKey = undefined;
|
|
23
|
-
while (true) {
|
|
24
|
-
const response = await client.get({
|
|
25
|
-
entityName: 'item',
|
|
26
|
-
apiVersion: sdk_1.API_VERSION.V2,
|
|
27
|
-
// customParams: { propertySlugs: 'id,familyId,roles,name' }, // not compatible with V2 for some reason
|
|
28
|
-
nextPageKey,
|
|
29
|
-
});
|
|
30
|
-
nextPageKey = response.nextPageKey;
|
|
31
|
-
if (response.results.length === 0) {
|
|
32
|
-
break; // No more assortments to fetch
|
|
33
|
-
}
|
|
34
|
-
items.push(...response.results);
|
|
35
|
-
if (!nextPageKey) {
|
|
36
|
-
break; // an absent cursor restarts pagination from page one
|
|
37
|
-
}
|
|
38
|
-
if (items.length >= maxCount) {
|
|
39
|
-
console.warn(`Reached maximum item limit of ${maxCount}. Some items may not be included.`);
|
|
40
|
-
break; // Stop if we reach the maximum limit
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return items;
|
|
44
|
-
}
|
|
45
|
-
exports.getManyItems = getManyItems;
|
|
46
|
-
async function getItemFamilies(count) {
|
|
47
|
-
return getItems({ roles: ['family'] }, count);
|
|
48
|
-
}
|
|
49
|
-
exports.getItemFamilies = getItemFamilies;
|
|
50
|
-
async function getFamilyName(item) {
|
|
51
|
-
if (!item.roles.includes('option')) {
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
if (!item.itemFamilyId) {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
await ensureItemCacheIsPopulated();
|
|
58
|
-
const family = ITEMS_CACHE_BY_ID.get(item.itemFamilyId);
|
|
59
|
-
return family ? [family.name] : ['placeholder']; // placeholder if family not found
|
|
60
|
-
}
|
|
61
|
-
exports.getFamilyName = getFamilyName;
|
|
62
|
-
async function ensureItemCacheIsPopulated() {
|
|
63
|
-
if (ITEMS_CACHE_BY_ID.size === 0) {
|
|
64
|
-
await getAllItemsUsingCacheFallback();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
exports.Roles = ['option', 'family'];
|
|
68
|
-
async function getItems(criteria, count = 100) {
|
|
69
|
-
const client = new sdk_1.Entities();
|
|
70
|
-
const items = [];
|
|
71
|
-
let nextPageKey = undefined;
|
|
72
|
-
do {
|
|
73
|
-
const response = await client.get({
|
|
74
|
-
entityName: 'item',
|
|
75
|
-
criteria,
|
|
76
|
-
nextPageKey,
|
|
77
|
-
apiVersion: sdk_1.API_VERSION.V2,
|
|
78
|
-
});
|
|
79
|
-
nextPageKey = response.nextPageKey;
|
|
80
|
-
if (response.results.length === 0) {
|
|
81
|
-
break; // No more items to fetch
|
|
82
|
-
}
|
|
83
|
-
const numItemsCanBeAdded = count - items.length;
|
|
84
|
-
const itemsToAdd = response.results.slice(0, numItemsCanBeAdded);
|
|
85
|
-
items.push(...itemsToAdd);
|
|
86
|
-
} while (nextPageKey && items.length < count);
|
|
87
|
-
return items;
|
|
88
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const items_1 = require("./items");
|
|
4
|
-
const mockGet = jest.fn();
|
|
5
|
-
jest.mock('@contrail/sdk', () => ({
|
|
6
|
-
Entities: jest.fn().mockImplementation(() => ({ get: (...args) => mockGet(...args) })),
|
|
7
|
-
API_VERSION: { V2: '2' },
|
|
8
|
-
}));
|
|
9
|
-
describe('getManyItems', () => {
|
|
10
|
-
beforeEach(() => jest.clearAllMocks());
|
|
11
|
-
it('stops on the last page rather than refetching the first', async () => {
|
|
12
|
-
mockGet.mockImplementation(async ({ nextPageKey }) => nextPageKey === 'page-2'
|
|
13
|
-
? { results: [{ id: 'b' }], nextPageKey: undefined }
|
|
14
|
-
: { results: [{ id: 'a' }], nextPageKey: 'page-2' });
|
|
15
|
-
const items = await (0, items_1.getManyItems)(10);
|
|
16
|
-
expect(mockGet).toHaveBeenCalledTimes(2);
|
|
17
|
-
expect(items.map((item) => item.id)).toEqual(['a', 'b']);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findItemsNotInProject = void 0;
|
|
4
|
-
const batchFetch_1 = require("./batchFetch");
|
|
5
|
-
const items_1 = require("./items");
|
|
6
|
-
const PROJECT_ITEMS_CACHE_BY_PROJECT_ID = new Map();
|
|
7
|
-
const PROJECT_ITEMS_CACHE_BY_ITEM_ID = new Map();
|
|
8
|
-
async function cacheProjectItemsByProjectId(projectId) {
|
|
9
|
-
if (PROJECT_ITEMS_CACHE_BY_PROJECT_ID.has(projectId)) {
|
|
10
|
-
return; // Already cached
|
|
11
|
-
}
|
|
12
|
-
const projectItems = await (0, batchFetch_1.batchFetch)('project-item', {
|
|
13
|
-
criteria: { projectId },
|
|
14
|
-
customParams: { propertySlugs: 'id,projectId,assortmentId' },
|
|
15
|
-
});
|
|
16
|
-
PROJECT_ITEMS_CACHE_BY_PROJECT_ID.set(projectId, projectItems);
|
|
17
|
-
projectItems.forEach((item) => {
|
|
18
|
-
if (!PROJECT_ITEMS_CACHE_BY_ITEM_ID.has(item.itemId)) {
|
|
19
|
-
PROJECT_ITEMS_CACHE_BY_ITEM_ID.set(item.itemId, []);
|
|
20
|
-
}
|
|
21
|
-
PROJECT_ITEMS_CACHE_BY_ITEM_ID.get(item.itemId).push(item);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
async function getProjectItemsByProjectId(projectId) {
|
|
25
|
-
await cacheProjectItemsByProjectId(projectId);
|
|
26
|
-
const projectItems = PROJECT_ITEMS_CACHE_BY_PROJECT_ID.get(projectId);
|
|
27
|
-
if (!projectItems) {
|
|
28
|
-
throw new Error(`No project items found for project ID ${projectId}`);
|
|
29
|
-
}
|
|
30
|
-
return projectItems;
|
|
31
|
-
}
|
|
32
|
-
async function findItemsNotInProject(projectId) {
|
|
33
|
-
const [projectItemsInProject, allItems] = await Promise.all([
|
|
34
|
-
getProjectItemsByProjectId(projectId),
|
|
35
|
-
(0, items_1.getManyItems)(),
|
|
36
|
-
]);
|
|
37
|
-
const projectItemIds = new Set(projectItemsInProject.map((item) => item.itemId));
|
|
38
|
-
const itemsNotInProject = allItems.filter((item) => !projectItemIds.has(item.id));
|
|
39
|
-
if (itemsNotInProject.length === 0) {
|
|
40
|
-
throw new Error(`All items are already in project ${projectId}`);
|
|
41
|
-
}
|
|
42
|
-
return itemsNotInProject.sort((a, b) => a.id.localeCompare(b.id)); // sorting ensures consistent order of response
|
|
43
|
-
}
|
|
44
|
-
exports.findItemsNotInProject = findItemsNotInProject;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Date Range Parsing Utilities
|
|
3
|
-
*
|
|
4
|
-
* Handles parsing date range syntax like:
|
|
5
|
-
* - createdOn=2025-09-15..2025-09-16 (date range)
|
|
6
|
-
* - createdOn=2025-09-15T04:00:00Z..2025-09-16T03:59:59Z (timestamp range)
|
|
7
|
-
* - createdOn=2025-09-15.. (from date onwards)
|
|
8
|
-
* - createdOn=..2025-09-16 (until date)
|
|
9
|
-
*/
|
|
10
|
-
export type DateRange = {
|
|
11
|
-
start?: Date;
|
|
12
|
-
end?: Date;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Checks if a criteria value uses date range syntax (..)
|
|
16
|
-
*/
|
|
17
|
-
export declare function isDateRangeValue(value: unknown): value is string;
|
|
18
|
-
/**
|
|
19
|
-
* Parses a date range value into its bounds
|
|
20
|
-
* @param rangeValue - The value (e.g., "2025-09-15..2025-09-16")
|
|
21
|
-
*/
|
|
22
|
-
export declare function parseDateRange(rangeValue: string): DateRange;
|
|
23
|
-
/**
|
|
24
|
-
* Parses a date string, handling both date-only and full timestamp formats
|
|
25
|
-
* @param dateStr - Date string (e.g., "2025-09-15" or "2025-09-15T04:00:00Z")
|
|
26
|
-
* @param boundType - Whether this is the 'start' or 'end' of a range (affects date-only handling)
|
|
27
|
-
*/
|
|
28
|
-
export declare function parseDate(dateStr: string, boundType?: 'start' | 'end'): Date;
|
|
29
|
-
/**
|
|
30
|
-
* Converts a date range to JSURL BETWEEN format
|
|
31
|
-
* @param range - The date range object
|
|
32
|
-
* @param isEndOfRange - Whether this date represents the end of a range (affects date-only handling)
|
|
33
|
-
*/
|
|
34
|
-
export declare function convertDateRangeToJSURL(range: DateRange): string;
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Date Range Parsing Utilities
|
|
4
|
-
*
|
|
5
|
-
* Handles parsing date range syntax like:
|
|
6
|
-
* - createdOn=2025-09-15..2025-09-16 (date range)
|
|
7
|
-
* - createdOn=2025-09-15T04:00:00Z..2025-09-16T03:59:59Z (timestamp range)
|
|
8
|
-
* - createdOn=2025-09-15.. (from date onwards)
|
|
9
|
-
* - createdOn=..2025-09-16 (until date)
|
|
10
|
-
*/
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.convertDateRangeToJSURL = exports.parseDate = exports.parseDateRange = exports.isDateRangeValue = void 0;
|
|
13
|
-
/**
|
|
14
|
-
* Checks if a criteria value uses date range syntax (..)
|
|
15
|
-
*/
|
|
16
|
-
function isDateRangeValue(value) {
|
|
17
|
-
return typeof value === 'string' && value.includes('..');
|
|
18
|
-
}
|
|
19
|
-
exports.isDateRangeValue = isDateRangeValue;
|
|
20
|
-
/**
|
|
21
|
-
* Parses a date range value into its bounds
|
|
22
|
-
* @param rangeValue - The value (e.g., "2025-09-15..2025-09-16")
|
|
23
|
-
*/
|
|
24
|
-
function parseDateRange(rangeValue) {
|
|
25
|
-
if (!rangeValue.includes('..')) {
|
|
26
|
-
throw new Error(`Invalid date range: ${rangeValue}`);
|
|
27
|
-
}
|
|
28
|
-
const [startStr, endStr] = rangeValue.split('..');
|
|
29
|
-
const range = {
|
|
30
|
-
start: startStr.trim() ? parseDate(startStr.trim(), 'start') : undefined,
|
|
31
|
-
end: endStr.trim() ? parseDate(endStr.trim(), 'end') : undefined,
|
|
32
|
-
};
|
|
33
|
-
// At least one bound must be specified
|
|
34
|
-
if (!range.start && !range.end) {
|
|
35
|
-
throw new Error(`Date range must have at least one bound: ${rangeValue}`);
|
|
36
|
-
}
|
|
37
|
-
return range;
|
|
38
|
-
}
|
|
39
|
-
exports.parseDateRange = parseDateRange;
|
|
40
|
-
/**
|
|
41
|
-
* Parses a date string, handling both date-only and full timestamp formats
|
|
42
|
-
* @param dateStr - Date string (e.g., "2025-09-15" or "2025-09-15T04:00:00Z")
|
|
43
|
-
* @param boundType - Whether this is the 'start' or 'end' of a range (affects date-only handling)
|
|
44
|
-
*/
|
|
45
|
-
function parseDate(dateStr, boundType = 'start') {
|
|
46
|
-
if (!dateStr.includes('T')) {
|
|
47
|
-
if (boundType === 'start') {
|
|
48
|
-
dateStr = `${dateStr}T00:00:00.000Z`;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
dateStr = `${dateStr}T23:59:59.999Z`;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const date = new Date(dateStr);
|
|
55
|
-
if (isNaN(date.getTime())) {
|
|
56
|
-
throw new Error(`Invalid date format: ${dateStr}`);
|
|
57
|
-
}
|
|
58
|
-
return date;
|
|
59
|
-
}
|
|
60
|
-
exports.parseDate = parseDate;
|
|
61
|
-
/**
|
|
62
|
-
* Converts a date range to JSURL BETWEEN format
|
|
63
|
-
* @param range - The date range object
|
|
64
|
-
* @param isEndOfRange - Whether this date represents the end of a range (affects date-only handling)
|
|
65
|
-
*/
|
|
66
|
-
function convertDateRangeToJSURL(range) {
|
|
67
|
-
if (!range.start && !range.end) {
|
|
68
|
-
throw new Error('Date range must have at least one bound');
|
|
69
|
-
}
|
|
70
|
-
if (range.start && range.end) {
|
|
71
|
-
const startISO = range.start.toISOString();
|
|
72
|
-
const endISO = range.end.toISOString();
|
|
73
|
-
return `BETWEEN ${startISO} and ${endISO}`;
|
|
74
|
-
}
|
|
75
|
-
else if (range.start) {
|
|
76
|
-
return `>= ${range.start.toISOString()}`;
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
const endISO = range.end.toISOString();
|
|
80
|
-
return `<= ${endISO}`;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
exports.convertDateRangeToJSURL = convertDateRangeToJSURL;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|