Skip to content

Commit 71dc947

Browse files
committed
fix: test and update
1 parent f58f18e commit 71dc947

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/catalog/update.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,30 @@ export async function handleProductSaveRequest(ctx, config, request) {
5757
const product = await putProduct(ctx, config, requestBody);
5858
const products = [product];
5959

60-
const { base: _, ...otherPatterns } = config.confEnvMap[config.env];
60+
const { base: _ = undefined, ...otherPatterns } = (config.env in config.confEnvMap)
61+
? config.confEnvMap[config.env]
62+
: {};
6163
const matchedPathPatterns = Object.keys(otherPatterns);
6264

63-
for (const purgeProduct of products) {
64-
for (const pattern of matchedPathPatterns) {
65-
let path = pattern.replace('{{sku}}', purgeProduct.sku);
65+
if (matchedPathPatterns.length !== 0) {
66+
for (const purgeProduct of products) {
67+
for (const pattern of matchedPathPatterns) {
68+
let path = pattern.replace('{{sku}}', purgeProduct.sku);
6669

67-
if (path.includes('{{urlkey}}') && purgeProduct.urlKey) {
68-
path = path.replace('{{urlkey}}', purgeProduct.urlKey);
69-
}
70+
if (path.includes('{{urlkey}}') && purgeProduct.urlKey) {
71+
path = path.replace('{{urlkey}}', purgeProduct.urlKey);
72+
}
7073

71-
for (const op of ['preview', 'live']) {
72-
const response = await callAdmin(config, op, path, { method: 'post' });
73-
if (!response.ok) {
74-
return errorResponse(400, `failed to ${op} product`);
74+
for (const op of ['preview', 'live']) {
75+
const response = await callAdmin(config, op, path, { method: 'post' });
76+
if (!response.ok) {
77+
return errorResponse(400, `failed to ${op} product`);
78+
}
7579
}
7680
}
7781
}
7882
}
83+
7984
return new Response(undefined, { status: 201 });
8085
} catch (e) {
8186
if (e.response) {

test/catalog/update.test.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,25 @@ describe('Product Save Tests', () => {
106106
assert.equal(callAdminStub.callCount, 4);
107107
});
108108

109-
it('should skip calling callAdmin when confMap has no matching keys', async () => {
109+
it('should skip calling callAdmin when confMap has no matching env', async () => {
110110
const config = {
111111
sku: '1234',
112112
confEnvMap: {
113-
'other-env': {
113+
prod: {
114+
base: { },
114115
'/path/to/{{sku}}': { env: 'other-env' },
115116
},
116117
},
117-
env: 'test',
118+
env: 'dev',
118119
};
119120
const ctx = { log: { error: sinon.stub() } };
120121
const request = { json: sinon.stub().resolves({ sku: '1234' }) };
121122

123+
saveProductsStub.resolves();
122124
const response = await handleProductSaveRequest(ctx, config, request);
123125

124-
assert.equal(response.status, 201);
125-
assert(saveProductsStub.calledOnce);
126126
assert(callAdminStub.notCalled);
127+
assert.equal(response.status, 201);
127128
});
128129

129130
it('should return error when callAdmin fails', async () => {

0 commit comments

Comments
 (0)