-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSP_GetAmzProductBySku.ts
110 lines (98 loc) · 2.14 KB
/
SP_GetAmzProductBySku.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
*@NApiVersion 2.1
*@NScriptType Restlet
*/
import { EntryPoints } from 'N/types';
import * as log from 'N/log';
import * as error from 'N/error';
import * as search from 'N/search';
/**
* A RESTlet to search for a product by SKU at the Amazon location.
*/
type PostContext = {
sku: string;
};
function doValidation(args: any, argNames: any, methodName: any) {
for (let i = 0; i < args.length; i++)
if (!args[i] && args[i] !== 0)
throw error.create({
name: 'MISSING_REQ_ARG',
message:
'Missing a required argument: [' +
argNames[i] +
'] for method: ' +
methodName,
});
}
function searchProductBySku(sku: string) {
const productSearch = search.create({
type: 'item',
columns: [
{
name: 'internalid',
},
{
name: 'custitem_sp_item_sku',
},
{
name: 'displayname',
},
{
name: 'upccode',
},
{
name: 'inventorylocation',
},
{
name: 'locationquantityavailable',
},
],
// new
filters: [
{
name: 'isinactive',
operator: search.Operator.IS,
values: 'F',
},
{
name: 'inventorylocation',
operator: search.Operator.ANYOF,
values: '22',
},
],
});
const productSearchFilters = productSearch.filters;
const skuFilter = {
name: 'custitem_sp_item_sku',
operator: search.Operator.IS,
values: sku,
join: null,
formula: null,
summary: null,
};
productSearchFilters.push(skuFilter);
productSearch.filters = productSearchFilters;
const resultSet = productSearch.run();
const results = resultSet.getRange({ start: 0, end: 1000 });
log.debug({
title: 'RESULTS',
details: results,
});
return results;
}
export const post: EntryPoints.RESTlet.post = (context: PostContext) => {
log.debug({
title: 'CONTEXT',
details: context,
});
doValidation([context.sku], ['sku'], 'POST');
if (!context.sku) {
return {
error: 'No SKU provided',
};
}
const result = searchProductBySku(context.sku);
return {
data: result,
};
};