-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ts
221 lines (167 loc) · 7.73 KB
/
update.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import Command, { Flags, Args, FLAG_LOAD_PARAMS, FLAG_SAVE_PARAMS } from '../../base'
import { clApi, clColor, clUtil } from '@commercelayer/cli-core'
import type { CommerceLayerClient, QueryParamsRetrieve } from '@commercelayer/sdk'
import { addRequestReader, isRequestInterrupted } from '../../lang'
import { mergeCommandParams } from '../../commands'
const OPERATION = 'update'
export default class ResourcesUpdate extends Command {
static description = 'update an existing resource'
static aliases = [OPERATION, 'ru', 'res:update', 'patch']
static examples = [
'$ commercelayer resources:update customers/<customerId> -a reference=referenceId',
'$ commercelayer res:update customers <customerId> -a reference_origin="Ref Origin"',
'$ cl update customers/<customerId> -m meta_key="meta value"',
'$ cl ru customers <customerId> -M meta_key="metadata overwrite',
'$ clayer update customers <customerId> -D /path/to/data/file/data.json',
'$ cl update order <orderId> -r billing_address=addresses/<addressId>',
'$ cl update customer <customerId> -r customer_group=<customerGroupId>'
]
static flags = {
...Command.flags,
attribute: Flags.string({
char: 'a',
description: 'define a resource attribute',
multiple: true,
}),
object: Flags.string({
char: 'O',
description: 'define a resource object attribute',
multiple: true,
}),
relationship: Flags.string({
char: 'r',
description: 'define a relationship with another resource',
multiple: true,
}),
metadata: Flags.string({
char: 'm',
description: 'define a metadata attribute and merge it with the metadata already present in the remote resource',
multiple: true,
exclusive: ['metadata-replace'],
}),
'metadata-replace': Flags.string({
char: 'M',
description: 'define a metadata attribute and replace every item already present in the remote resource',
multiple: true,
exclusive: ['metadata'],
}),
data: Flags.string({
char: 'D',
description: 'the data file to use as request body',
multiple: false,
exclusive: ['attribute', 'relationship', 'metadata', 'metadata-replace', 'doc', FLAG_LOAD_PARAMS, FLAG_SAVE_PARAMS],
}),
tags: Flags.string({
char: 't',
description: 'list of tags associated with the resource',
multiple: true
})
}
static args = {
...Command.args,
id: Args.string({ name: 'id', description: 'id of the resource to update', required: false }),
}
async run(): Promise<any> {
const { args, flags } = await this.parse(ResourcesUpdate)
const resId = this.checkResourceId(args.resource, args.id)
const resource = this.checkResource(resId.res, { singular: true })
const id = this.checkLastId(flags.organization, resource.api, resId.id)
const loadParams = flags[FLAG_LOAD_PARAMS]
const saveCmd = flags[FLAG_SAVE_PARAMS]
if (saveCmd) this.checkAlias(saveCmd, resource.api, OPERATION, this.config)
const showHeaders = flags.headers || flags['headers-only']
// Raw request
if (flags.data) {
try {
const baseUrl = clApi.baseURL('core', flags.organization, flags.domain)
const accessToken = flags.accessToken
const dataPath = clUtil.specialFolder(flags.data)
const rawData = clApi.request.readDataFile(dataPath)
if (!rawData?.data) this.error('Empty data file')
if (!rawData.data.type) rawData.data.type = resource.api
if (!rawData.data.id) rawData.data.id = id
const rawRes = await clApi.request.raw({ operation: clApi.Operation.Update, baseUrl, accessToken, resource: resource.api }, rawData, id)
const out = flags.raw ? rawRes : clApi.response.denormalize(rawRes)
this.printOutput(out, flags)
this.log(`\n${clColor.style.success('Successfully')} updated resource of type ${clColor.style.resource(resource.api)} with id ${clColor.style.id(rawRes.data.id)}\n`)
return out
} catch (error) {
this.printError(error)
}
}
const cl = this.initCommerceLayer(flags)
// Attributes flags
const attributes = this.attributeFlag(flags.attribute)
// Objects flags
const objects = this.objectFlag(flags.object)
// Relationships flags
const relationships = this.relationshipFlag(flags.relationship, flags.organization)
// Metadata flags
const metadata = this.metadataFlag(flags.metadata || flags['metadata-replace'], { fixTypes: true })
// Relationships
if (relationships && Object.keys(relationships).length > 0) Object.entries(relationships).forEach(([key, value]) => {
const relSdk: any = cl[value.type as keyof CommerceLayerClient]
const rel = relSdk.relationship(((value.id === null) || (value.id === 'null')) ? null : value)
attributes[key] = rel
})
// Objects
if (objects && (Object.keys(objects).length > 0)) {
for (const o of Object.keys(objects)) {
if (Object.keys(attributes).includes(o)) this.warn(`Object ${o} will overwrite attribute ${o}`)
else attributes[o] = objects[o]
}
}
// Metadata
if (metadata && (Object.keys(metadata).length > 0)) {
if (attributes.metadata) this.warn(`Attribute ${clColor.style.attribute('metadata')} will be overwritten by the content defined with the flags ${clColor.style.flag('-m/-M')}`)
attributes.metadata = metadata
}
// Tag flags
if (flags.tags) {
this.checkTag(resource.api)
const tags = this.tagFlag(flags.tags)
if (tags) attributes.tags = tags.map(t => cl.tags.relationship(t))
}
// Include flags
const include: string[] = this.includeFlag(flags.include, relationships, flags['force-include'])
// Fields flags
const fields = this.fieldsFlag(flags.fields, resource.api)
const rawReader = flags.raw ? cl.addRawResponseReader({ headers: showHeaders }) : undefined
const reqReader = flags.doc ? addRequestReader(cl) : undefined
const params: QueryParamsRetrieve = {}
try {
const resSdk: any = cl[resource.api as keyof CommerceLayerClient]
this.checkOperation(resSdk, OPERATION, attributes)
if (include && (include.length > 0)) params.include = include
if (fields && (Object.keys(fields).length > 0)) params.fields = fields
// Metadata attributes merge
if (flags.metadata) {
const params: QueryParamsRetrieve = { fields: { [resource.api]: ['metadata'] } }
const remRes = await resSdk.retrieve(id, params)
const remMeta: object = remRes.metadata
if (remMeta && (Object.keys(remMeta).length > 0)) attributes.metadata = { ...remMeta, ...metadata }
}
attributes.id = id
// Load saved command arguments
if (loadParams) {
const savedParams = this.loadParams(loadParams, resource.api, OPERATION)
if (savedParams) mergeCommandParams(params, savedParams)
}
const res = await resSdk.update(attributes, params)
// Save last resource id
if (res?.id) this.lastResources(flags.organization, { [res.type]: res.id })
const out = (flags.raw && rawReader) ? rawReader.rawResponse : res
this.printHeaders(rawReader?.headers, flags)
this.printOutput(out, flags)
this.log(`\n${clColor.style.success('Successfully')} updated resource of type ${clColor.style.resource(resource.api)} with id ${clColor.style.id(res.id)}\n`)
// Save command arguments
if (saveCmd) this.saveParams(saveCmd, { type: resource.api }, OPERATION, params)
return out
} catch (error) {
if (isRequestInterrupted(error) && reqReader) {
await this.showLiveDocumentation(reqReader.request, undefined, flags)
cl.removeInterceptor('request', reqReader.id)
} else this.printError(error, flags, args)
}
}
}