@foxtware/mineral 0.1.32 → 0.1.34
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/api/shopify/shopifyInventoryItemUpdate.js +116 -0
- package/api/utils.js +19 -4
- package/package.json +1 -1
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// https://shopify.dev/docs/api/admin-graphql/latest/mutations/inventoryitemupdate
|
|
2
|
+
|
|
3
|
+
const { credsValidator } = require('../validators');
|
|
4
|
+
const {
|
|
5
|
+
actionSingleOrMultiple,
|
|
6
|
+
everyIfArray,
|
|
7
|
+
ArgsWarden,
|
|
8
|
+
valueProvided,
|
|
9
|
+
} = require('../utils');
|
|
10
|
+
const { shopifyMutationDo } = require('../shopify/shopifyMutationDo');
|
|
11
|
+
|
|
12
|
+
const inventoryItemUpdatePayloadValidator = (updatePayload) => {
|
|
13
|
+
return valueProvided(updatePayload)
|
|
14
|
+
&& typeof updatePayload === 'object'
|
|
15
|
+
&& !Array.isArray(updatePayload);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const argsWarden = new ArgsWarden([
|
|
19
|
+
['credsPayload', credsValidator],
|
|
20
|
+
['inventoryItemId', (id) => everyIfArray(valueProvided, id)],
|
|
21
|
+
['updatePayload', (payload) => everyIfArray(inventoryItemUpdatePayloadValidator, payload)],
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const defaultReturnInventoryItemAttrs = 'id countryCodeOfOrigin harmonizedSystemCode';
|
|
25
|
+
|
|
26
|
+
const shopifyInventoryItemUpdateSingle = async (
|
|
27
|
+
credsPayload,
|
|
28
|
+
inventoryItemId,
|
|
29
|
+
updatePayload,
|
|
30
|
+
{
|
|
31
|
+
apiVersion,
|
|
32
|
+
returnInventoryItemAttrs = defaultReturnInventoryItemAttrs,
|
|
33
|
+
} = {},
|
|
34
|
+
) => {
|
|
35
|
+
|
|
36
|
+
return shopifyMutationDo(
|
|
37
|
+
credsPayload,
|
|
38
|
+
'inventoryItemUpdate',
|
|
39
|
+
{
|
|
40
|
+
mutationVariables: {
|
|
41
|
+
id: {
|
|
42
|
+
type: 'ID!',
|
|
43
|
+
value: `gid://shopify/InventoryItem/${ inventoryItemId }`,
|
|
44
|
+
},
|
|
45
|
+
input: {
|
|
46
|
+
type: 'InventoryItemInput!',
|
|
47
|
+
value: updatePayload,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
returnSchema: `inventoryItem { ${ returnInventoryItemAttrs } }`,
|
|
51
|
+
apiVersion,
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const shopifyInventoryItemUpdate = async (
|
|
57
|
+
credsPayload,
|
|
58
|
+
inventoryItemId,
|
|
59
|
+
updatePayload,
|
|
60
|
+
{
|
|
61
|
+
queueRunOptions,
|
|
62
|
+
apiVersion,
|
|
63
|
+
returnInventoryItemAttrs = defaultReturnInventoryItemAttrs,
|
|
64
|
+
} = {},
|
|
65
|
+
) => {
|
|
66
|
+
|
|
67
|
+
const rejectResponse = await argsWarden.responseIfRejectingArgs({
|
|
68
|
+
credsPayload,
|
|
69
|
+
inventoryItemId,
|
|
70
|
+
updatePayload,
|
|
71
|
+
});
|
|
72
|
+
if (rejectResponse) {
|
|
73
|
+
return rejectResponse;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return actionSingleOrMultiple(
|
|
77
|
+
[inventoryItemId, updatePayload],
|
|
78
|
+
shopifyInventoryItemUpdateSingle,
|
|
79
|
+
(inventoryItemIdItem, updatePayloadItem) => ({
|
|
80
|
+
args: [
|
|
81
|
+
credsPayload,
|
|
82
|
+
inventoryItemIdItem,
|
|
83
|
+
updatePayloadItem,
|
|
84
|
+
{
|
|
85
|
+
apiVersion,
|
|
86
|
+
returnInventoryItemAttrs,
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
}),
|
|
90
|
+
{
|
|
91
|
+
...(queueRunOptions ? { queueRunOptions } : {}),
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const funcApiConfig = {
|
|
97
|
+
argsWarden,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
module.exports = {
|
|
101
|
+
shopifyInventoryItemUpdate,
|
|
102
|
+
funcApiConfig,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/*
|
|
106
|
+
curl -X POST "http://localhost:8000/shopifyInventoryItemUpdate" \
|
|
107
|
+
-H "Content-Type: application/json" \
|
|
108
|
+
-d '{
|
|
109
|
+
"credsPayload": { "credsPath": "shopify.au" },
|
|
110
|
+
"inventoryItemId": "43729076",
|
|
111
|
+
"updatePayload": {
|
|
112
|
+
"countryCodeOfOrigin": "US",
|
|
113
|
+
"harmonizedSystemCode": "621710"
|
|
114
|
+
}
|
|
115
|
+
}'
|
|
116
|
+
*/
|
package/api/utils.js
CHANGED
|
@@ -43,18 +43,33 @@ const getWithLocalCachedFile = async (
|
|
|
43
43
|
const directory = `${ getWorkspace() }/api/_temp`;
|
|
44
44
|
const filepath = `${ directory }/${ filename }`;
|
|
45
45
|
|
|
46
|
+
let cachedFile;
|
|
46
47
|
try {
|
|
47
|
-
|
|
48
|
-
return JSON.parse(cachedFile);
|
|
48
|
+
cachedFile = await fs.readFile(filepath, 'utf8');
|
|
49
49
|
} catch (error) {
|
|
50
|
-
if (error.code
|
|
50
|
+
if (error.code === 'ENOENT') {
|
|
51
|
+
cachedFile = null;
|
|
52
|
+
} else {
|
|
51
53
|
throw error;
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
if (cachedFile) {
|
|
58
|
+
try {
|
|
59
|
+
return JSON.parse(cachedFile);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.warn('Invalid local cache, refetching', {
|
|
62
|
+
filename,
|
|
63
|
+
error: error.message,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
55
68
|
const response = await getFunction();
|
|
56
69
|
await fs.mkdir(directory, { recursive: true });
|
|
57
|
-
|
|
70
|
+
const temporaryFilepath = `${ filepath }.tmp-${ process.pid }-${ Date.now() }`;
|
|
71
|
+
await fs.writeFile(temporaryFilepath, JSON.stringify(response, null, 2), 'utf8');
|
|
72
|
+
await fs.rename(temporaryFilepath, filepath);
|
|
58
73
|
|
|
59
74
|
return response;
|
|
60
75
|
};
|