@things-factory/product-base 8.0.0-beta.0 → 8.0.0-beta.2
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/package.json +5 -5
- package/server/constants/index.ts +0 -1
- package/server/constants/product.ts +0 -24
- package/server/controllers/index.ts +0 -0
- package/server/index.ts +0 -2
- package/server/middlewares/index.ts +0 -0
- package/server/migrations/index.ts +0 -9
- package/server/service/index.ts +0 -61
- package/server/service/product/index.ts +0 -6
- package/server/service/product/product-mutation.ts +0 -407
- package/server/service/product/product-query.ts +0 -336
- package/server/service/product/product-types.ts +0 -450
- package/server/service/product/product.ts +0 -543
- package/server/service/product/validate-product.ts +0 -42
- package/server/service/product-bundle/index.ts +0 -6
- package/server/service/product-bundle/product-bundle-mutation.ts +0 -140
- package/server/service/product-bundle/product-bundle-query.ts +0 -104
- package/server/service/product-bundle/product-bundle-types.ts +0 -51
- package/server/service/product-bundle/product-bundle.ts +0 -102
- package/server/service/product-bundle-setting/index.ts +0 -6
- package/server/service/product-bundle-setting/product-bundle-setting-mutation.ts +0 -168
- package/server/service/product-bundle-setting/product-bundle-setting-query.ts +0 -139
- package/server/service/product-bundle-setting/product-bundle-setting-types.ts +0 -41
- package/server/service/product-bundle-setting/product-bundle-setting.ts +0 -45
- package/server/service/product-combination/index.ts +0 -6
- package/server/service/product-combination/product-combination-mutation.ts +0 -148
- package/server/service/product-combination/product-combination-query.ts +0 -48
- package/server/service/product-combination/product-combination-type.ts +0 -50
- package/server/service/product-combination/product-combination.ts +0 -116
- package/server/service/product-combination-setting/index.ts +0 -6
- package/server/service/product-combination-setting/product-combination-setting-mutation.ts +0 -248
- package/server/service/product-combination-setting/product-combination-setting-query.ts +0 -176
- package/server/service/product-combination-setting/product-combination-setting-type.ts +0 -44
- package/server/service/product-combination-setting/product-combination-setting.ts +0 -77
- package/server/service/product-detail/index.ts +0 -6
- package/server/service/product-detail/product-detail-mutation.ts +0 -238
- package/server/service/product-detail/product-detail-query.ts +0 -213
- package/server/service/product-detail/product-detail-types.ts +0 -280
- package/server/service/product-detail/product-detail.ts +0 -388
- package/server/service/product-detail-bizplace-setting/index.ts +0 -6
- package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting-mutation.ts +0 -118
- package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting-query.ts +0 -90
- package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting-types.ts +0 -62
- package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting.ts +0 -104
- package/server/service/product-set/index.ts +0 -6
- package/server/service/product-set/product-set-mutation.ts +0 -149
- package/server/service/product-set/product-set-query.ts +0 -114
- package/server/service/product-set/product-set-types.ts +0 -45
- package/server/service/product-set/product-set.ts +0 -95
- package/server/utils/index.ts +0 -1
- package/server/utils/product-util.ts +0 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@things-factory/product-base",
|
|
3
|
-
"version": "8.0.0-beta.
|
|
3
|
+
"version": "8.0.0-beta.2",
|
|
4
4
|
"main": "dist-server/index.js",
|
|
5
5
|
"browser": "client/index.js",
|
|
6
6
|
"things-factory": true,
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"migration:create": "node ../../node_modules/typeorm/cli.js migration:create ./server/migrations/migration"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@things-factory/biz-base": "^8.0.0-beta.
|
|
28
|
-
"@things-factory/env": "^8.0.0-beta.
|
|
29
|
-
"@things-factory/routing-base": "^8.0.0-beta.
|
|
27
|
+
"@things-factory/biz-base": "^8.0.0-beta.2",
|
|
28
|
+
"@things-factory/env": "^8.0.0-beta.2",
|
|
29
|
+
"@things-factory/routing-base": "^8.0.0-beta.2"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "f03431a09435511b2595515658f9cb8f78ba4ebb"
|
|
32
32
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './product'
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export const PRODUCT_STATUS = {
|
|
2
|
-
ACTIVE: 'ACTIVE',
|
|
3
|
-
DELETED: 'DELETED',
|
|
4
|
-
INACTIVE: 'INACTIVE'
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export const PRODUCT_SET_STATUS = {
|
|
8
|
-
ACTIVE: 'ACTIVE',
|
|
9
|
-
DELETED: 'DELETED',
|
|
10
|
-
INACTIVE: 'INACTIVE'
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const PRODUCT_BUNDLE_STATUS = {
|
|
14
|
-
ACTIVE: 'ACTIVE',
|
|
15
|
-
DELETED: 'DELETED',
|
|
16
|
-
INACTIVE: 'INACTIVE'
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const PRODUCT_COMBINATION_STATUS = {
|
|
20
|
-
ACTIVE: 'ACTIVE',
|
|
21
|
-
ACTIVATED: 'ACTIVATED',
|
|
22
|
-
DELETED: 'DELETED',
|
|
23
|
-
INACTIVE: 'INACTIVE'
|
|
24
|
-
}
|
|
File without changes
|
package/server/index.ts
DELETED
|
File without changes
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
const glob = require('glob')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
|
|
4
|
-
export var migrations = []
|
|
5
|
-
|
|
6
|
-
glob.sync(path.resolve(__dirname, '.', '**', '*.js')).forEach(function(file) {
|
|
7
|
-
if (file.indexOf('index.js') !== -1) return
|
|
8
|
-
migrations = migrations.concat(Object.values(require(path.resolve(file))) || [])
|
|
9
|
-
})
|
package/server/service/index.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { entities as ProductEntities, resolvers as ProductResolvers } from './product'
|
|
2
|
-
import { entities as ProductBundleEntities, resolvers as ProductBundleResolvers } from './product-bundle'
|
|
3
|
-
import {
|
|
4
|
-
entities as ProductBundleSettingEntities,
|
|
5
|
-
resolvers as ProductBundleSettingResolvers
|
|
6
|
-
} from './product-bundle-setting'
|
|
7
|
-
import { entities as ProductDetailEntities, resolvers as ProductDetailResolvers } from './product-detail'
|
|
8
|
-
import {
|
|
9
|
-
entities as ProductDetailBizplaceSettingEntities,
|
|
10
|
-
resolvers as ProductDetailBizplaceSettingResolver
|
|
11
|
-
} from './product-detail-bizplace-setting'
|
|
12
|
-
import { entities as ProductSetEntities, resolvers as ProductSetResolvers } from './product-set'
|
|
13
|
-
import { entities as ProductCombinationEntities, resolvers as ProductCombinationResolvers } from './product-combination'
|
|
14
|
-
import {
|
|
15
|
-
entities as ProductCombinationSettingEntities,
|
|
16
|
-
resolvers as ProductCombinationSettingResolvers
|
|
17
|
-
} from './product-combination-setting'
|
|
18
|
-
|
|
19
|
-
/* EXPORT ENTITY TYPES */
|
|
20
|
-
export * from './product-combination-setting/product-combination-setting'
|
|
21
|
-
export * from './product-combination/product-combination'
|
|
22
|
-
export * from './product/product'
|
|
23
|
-
export * from './product-detail/product-detail'
|
|
24
|
-
export * from './product-bundle/product-bundle'
|
|
25
|
-
export * from './product-bundle-setting/product-bundle-setting'
|
|
26
|
-
export * from './product-detail-bizplace-setting/product-detail-bizplace-setting'
|
|
27
|
-
export * from './product-set/product-set'
|
|
28
|
-
|
|
29
|
-
/* EXPORT TYPES */
|
|
30
|
-
export * from './product-combination/product-combination-type'
|
|
31
|
-
export * from './product-combination-setting/product-combination-setting-type'
|
|
32
|
-
export * from './product/product-types'
|
|
33
|
-
export * from './product-detail/product-detail-types'
|
|
34
|
-
export * from './product-bundle/product-bundle-types'
|
|
35
|
-
export * from './product-bundle-setting/product-bundle-setting-types'
|
|
36
|
-
export * from './product-detail-bizplace-setting/product-detail-bizplace-setting-types'
|
|
37
|
-
export * from './product-set/product-set-types'
|
|
38
|
-
|
|
39
|
-
export const entities = [
|
|
40
|
-
...ProductEntities,
|
|
41
|
-
...ProductDetailEntities,
|
|
42
|
-
...ProductBundleEntities,
|
|
43
|
-
...ProductBundleSettingEntities,
|
|
44
|
-
...ProductDetailBizplaceSettingEntities,
|
|
45
|
-
...ProductSetEntities,
|
|
46
|
-
...ProductCombinationEntities,
|
|
47
|
-
...ProductCombinationSettingEntities
|
|
48
|
-
]
|
|
49
|
-
|
|
50
|
-
export const schema = {
|
|
51
|
-
resolverClasses: [
|
|
52
|
-
...ProductResolvers,
|
|
53
|
-
...ProductDetailResolvers,
|
|
54
|
-
...ProductBundleResolvers,
|
|
55
|
-
...ProductBundleSettingResolvers,
|
|
56
|
-
...ProductDetailBizplaceSettingResolver,
|
|
57
|
-
...ProductSetResolvers,
|
|
58
|
-
...ProductCombinationResolvers,
|
|
59
|
-
...ProductCombinationSettingResolvers
|
|
60
|
-
]
|
|
61
|
-
}
|
|
@@ -1,407 +0,0 @@
|
|
|
1
|
-
import promisesAll from 'promises-all'
|
|
2
|
-
import { Arg, Ctx, Directive, Mutation, Resolver } from 'type-graphql'
|
|
3
|
-
import { In, Repository } from 'typeorm'
|
|
4
|
-
|
|
5
|
-
import { createAttachment, deleteAttachmentsByRef } from '@things-factory/attachment-base'
|
|
6
|
-
import { Bizplace } from '@things-factory/biz-base'
|
|
7
|
-
import { logger } from '@things-factory/env'
|
|
8
|
-
import { getRepository } from '@things-factory/shell'
|
|
9
|
-
|
|
10
|
-
import { PRODUCT_STATUS } from '../../constants'
|
|
11
|
-
import { ProductDetail } from '../product-detail/product-detail'
|
|
12
|
-
import {
|
|
13
|
-
createProductDetail,
|
|
14
|
-
updateMultipleProductDetail,
|
|
15
|
-
updateProductDetail
|
|
16
|
-
} from '../product-detail/product-detail-mutation'
|
|
17
|
-
import { ProductSet } from '../product-set/product-set'
|
|
18
|
-
import { Product } from './product'
|
|
19
|
-
import { NewProduct, ProductPatch } from './product-types'
|
|
20
|
-
import { validateProduct } from './validate-product'
|
|
21
|
-
|
|
22
|
-
@Resolver(Product)
|
|
23
|
-
export class ProductMutation {
|
|
24
|
-
@Directive('@transaction')
|
|
25
|
-
@Mutation(returns => Product)
|
|
26
|
-
async createProduct(@Arg('product') product: NewProduct, @Ctx() context: ResolverContext): Promise<Product> {
|
|
27
|
-
return await createProduct(product, context)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@Directive('@transaction')
|
|
31
|
-
@Mutation(returns => [Product])
|
|
32
|
-
async createProducts(
|
|
33
|
-
@Arg('products', type => [NewProduct]) products: NewProduct[],
|
|
34
|
-
@Ctx() context: ResolverContext
|
|
35
|
-
): Promise<Product[]> {
|
|
36
|
-
return await createProducts(products, context)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
@Directive('@transaction')
|
|
40
|
-
@Mutation(returns => Product)
|
|
41
|
-
async updateProduct(
|
|
42
|
-
@Arg('id') id: string,
|
|
43
|
-
@Arg('patch') patch: ProductPatch,
|
|
44
|
-
@Ctx() context: ResolverContext
|
|
45
|
-
): Promise<Product> {
|
|
46
|
-
return await updateProduct(id, patch, context)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
@Directive('@transaction')
|
|
50
|
-
@Mutation(returns => [Product])
|
|
51
|
-
async importMultipleProduct(
|
|
52
|
-
@Arg('patches', type => [ProductPatch]) patches: ProductPatch[],
|
|
53
|
-
@Ctx() context: ResolverContext
|
|
54
|
-
): Promise<Product[]> {
|
|
55
|
-
try {
|
|
56
|
-
let results = []
|
|
57
|
-
|
|
58
|
-
//// 1. Group and massage data based on sku for Product and Product Details.
|
|
59
|
-
let massagedPatch = patches.reduce(function (acc, currentObj) {
|
|
60
|
-
let existingParent = acc.find(itm => itm.sku == currentObj.sku)
|
|
61
|
-
if (existingParent) {
|
|
62
|
-
existingParent.productDetails = [
|
|
63
|
-
...existingParent.productDetails,
|
|
64
|
-
new ProductDetail({ ...currentObj, childProductDetail: currentObj.childGtin })
|
|
65
|
-
]
|
|
66
|
-
return acc
|
|
67
|
-
} else {
|
|
68
|
-
let newProduct = {
|
|
69
|
-
...currentObj,
|
|
70
|
-
productDetails: [
|
|
71
|
-
new ProductDetail({ ...currentObj, childProductDetail: currentObj.childGtin, isDefault: true })
|
|
72
|
-
]
|
|
73
|
-
}
|
|
74
|
-
return [...acc, newProduct]
|
|
75
|
-
}
|
|
76
|
-
}, [])
|
|
77
|
-
|
|
78
|
-
//// 2. Update all Grouped Product data and Product Details Data
|
|
79
|
-
for (let i = 0; i < massagedPatch.length; i++) {
|
|
80
|
-
const patch: Product = massagedPatch[i]
|
|
81
|
-
const result = await createProduct(patch, context)
|
|
82
|
-
results.push({ ...result, cuFlag: '+' })
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return results
|
|
86
|
-
} catch (error) {
|
|
87
|
-
throw error
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
@Directive('@transaction')
|
|
92
|
-
@Mutation(returns => [Product])
|
|
93
|
-
async updateMultipleProduct(
|
|
94
|
-
@Arg('patches', type => [ProductPatch]) patches: ProductPatch[],
|
|
95
|
-
@Ctx() context: ResolverContext
|
|
96
|
-
): Promise<Product[]> {
|
|
97
|
-
try {
|
|
98
|
-
let results = []
|
|
99
|
-
const _createRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === '+')
|
|
100
|
-
const _updateRecords = patches.filter((patch: any) => patch.cuFlag.toUpperCase() === 'M')
|
|
101
|
-
|
|
102
|
-
if (_createRecords.length > 0) {
|
|
103
|
-
for (let i = 0; i < _createRecords.length; i++) {
|
|
104
|
-
const patch: NewProduct = _createRecords[i]
|
|
105
|
-
const result = await createProduct(patch, context)
|
|
106
|
-
results.push({ ...result, cuFlag: '+' })
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (_updateRecords.length > 0) {
|
|
111
|
-
for (let i = 0; i < _updateRecords.length; i++) {
|
|
112
|
-
const patch: ProductPatch = _updateRecords[i]
|
|
113
|
-
const result = await updateProduct(patch.id, patch, context)
|
|
114
|
-
results.push({ ...result, cuFlag: 'M' })
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return results
|
|
119
|
-
} catch (error) {
|
|
120
|
-
throw error
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
@Directive('@privilege(category: "order", privilege: "mutation") ')
|
|
125
|
-
@Directive('@transaction')
|
|
126
|
-
@Mutation(returns => Boolean)
|
|
127
|
-
async linkProduct(
|
|
128
|
-
@Arg('productSetId') productSetId: string,
|
|
129
|
-
@Arg('patches', type => [ProductPatch]) patches: ProductPatch[],
|
|
130
|
-
@Ctx() context: ResolverContext
|
|
131
|
-
): Promise<Boolean> {
|
|
132
|
-
const { tx } = context.state
|
|
133
|
-
|
|
134
|
-
let productSet = await tx.getRepository(ProductSet).findOne({
|
|
135
|
-
where: { id: productSetId },
|
|
136
|
-
relations: ['domain', 'creator', 'updater', 'product']
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
let existingProductLink: any = productSet.product
|
|
140
|
-
|
|
141
|
-
let newProductLink: Product[] = patches.reduce((acc, curr) => {
|
|
142
|
-
if (!existingProductLink.find(itm => itm.id == curr.id)) {
|
|
143
|
-
acc.push({ id: curr.id })
|
|
144
|
-
}
|
|
145
|
-
return acc
|
|
146
|
-
}, [])
|
|
147
|
-
|
|
148
|
-
let removeProductLink: Product[] = existingProductLink.reduce((acc, curr) => {
|
|
149
|
-
if (!patches.find(itm => itm.id == curr.id)) {
|
|
150
|
-
acc.push({ id: curr.id })
|
|
151
|
-
}
|
|
152
|
-
return acc
|
|
153
|
-
}, [])
|
|
154
|
-
|
|
155
|
-
if (newProductLink.length > 0) {
|
|
156
|
-
for (let i = 0; i < newProductLink.length; i++) {
|
|
157
|
-
const patch: Product = { ...newProductLink[i], productSet }
|
|
158
|
-
await updateProduct(newProductLink[i].id, patch, context)
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (removeProductLink.length > 0) {
|
|
163
|
-
for (let i = 0; i < removeProductLink.length; i++) {
|
|
164
|
-
const patch: Product = { ...removeProductLink[i], productSet: null }
|
|
165
|
-
await updateProduct(removeProductLink[i].id, patch, context)
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return true
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
@Directive('@privilege(category: "order", privilege: "mutation") ')
|
|
173
|
-
@Directive('@transaction')
|
|
174
|
-
@Mutation(returns => Boolean)
|
|
175
|
-
async deleteProduct(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<Boolean> {
|
|
176
|
-
const { tx, user, domain } = context.state
|
|
177
|
-
const productSetRepo: Repository<Product> = tx.getRepository(Product)
|
|
178
|
-
const productSet = await productSetRepo.findOneBy({ domain: { id: domain.id }, id })
|
|
179
|
-
|
|
180
|
-
await productSetRepo.save({
|
|
181
|
-
...productSet,
|
|
182
|
-
status: PRODUCT_STATUS.DELETED,
|
|
183
|
-
updater: user
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
return true
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
@Directive('@privilege(category: "order", privilege: "mutation") ')
|
|
190
|
-
@Directive('@transaction')
|
|
191
|
-
@Mutation(returns => Boolean)
|
|
192
|
-
async deleteProducts(@Arg('ids', type => [String]) ids: string[], @Ctx() context: ResolverContext): Promise<Boolean> {
|
|
193
|
-
const { tx, user } = context.state
|
|
194
|
-
const productRepo = tx.getRepository(Product)
|
|
195
|
-
|
|
196
|
-
await productRepo.update(
|
|
197
|
-
{
|
|
198
|
-
id: In(ids)
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
deletedAt: new Date()
|
|
202
|
-
}
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
return true
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
@Directive('@privilege(category: "order", privilege: "mutation") ')
|
|
209
|
-
@Directive('@transaction')
|
|
210
|
-
@Mutation(returns => Boolean)
|
|
211
|
-
async undeleteProducts(
|
|
212
|
-
@Arg('ids', type => [String]) ids: string[],
|
|
213
|
-
@Ctx() context: ResolverContext
|
|
214
|
-
): Promise<Boolean> {
|
|
215
|
-
const { tx, user } = context.state
|
|
216
|
-
const productRepo = tx.getRepository(Product)
|
|
217
|
-
|
|
218
|
-
await productRepo.update(
|
|
219
|
-
{
|
|
220
|
-
id: In(ids)
|
|
221
|
-
},
|
|
222
|
-
{
|
|
223
|
-
deletedAt: null
|
|
224
|
-
}
|
|
225
|
-
)
|
|
226
|
-
|
|
227
|
-
return true
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export async function createProduct(product: any, context: ResolverContext) {
|
|
232
|
-
try {
|
|
233
|
-
const { domain, user, tx } = context.state
|
|
234
|
-
|
|
235
|
-
const productRepo: Repository<Product> = tx.getRepository(Product) || getRepository(Product)
|
|
236
|
-
const bizplaceRepo: Repository<Bizplace> = tx.getRepository(Bizplace) || getRepository(Bizplace)
|
|
237
|
-
|
|
238
|
-
let bizplace: Bizplace
|
|
239
|
-
if (product.bizplace?.id) {
|
|
240
|
-
bizplace = await bizplaceRepo.findOneBy({ id: product.bizplace.id })
|
|
241
|
-
} else if (context.state?.bizplace) {
|
|
242
|
-
bizplace = context.state.bizplace
|
|
243
|
-
} else {
|
|
244
|
-
bizplace = await bizplaceRepo.findOne({ where: { domain: { id: domain.id } } })
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (product?.productRef?.id) {
|
|
248
|
-
product.productRef = await productRepo.findOneBy({ id: product.productRef.id })
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (!product?.id) delete product.id
|
|
252
|
-
|
|
253
|
-
await validateProduct(
|
|
254
|
-
{
|
|
255
|
-
...product,
|
|
256
|
-
bizplace,
|
|
257
|
-
domain
|
|
258
|
-
},
|
|
259
|
-
context
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
if (product.uom) {
|
|
263
|
-
product.primaryUnit = product.uom
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (product.uomValue) {
|
|
267
|
-
product.primaryValue = product.uomValue
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
const result = await productRepo.save({
|
|
271
|
-
...product,
|
|
272
|
-
bizplace,
|
|
273
|
-
domain,
|
|
274
|
-
creator: user,
|
|
275
|
-
updater: user
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
let productDetails = product.productDetails
|
|
279
|
-
if (!productDetails) {
|
|
280
|
-
productDetails = [new ProductDetail({ ...product, product: result, isDefault: true })]
|
|
281
|
-
if (productDetails[0].gtin == null || productDetails[0] == undefined) {
|
|
282
|
-
productDetails[0].gtin = product.sku
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
await updateMultipleProductDetail(result.id, productDetails, context)
|
|
287
|
-
|
|
288
|
-
if (product.thumbnail) {
|
|
289
|
-
await createAttachment(
|
|
290
|
-
null,
|
|
291
|
-
{
|
|
292
|
-
attachment: {
|
|
293
|
-
file: product.thumbnail,
|
|
294
|
-
refType: Product.name,
|
|
295
|
-
refBy: result.id
|
|
296
|
-
}
|
|
297
|
-
},
|
|
298
|
-
context
|
|
299
|
-
)
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return result
|
|
303
|
-
} catch (error) {
|
|
304
|
-
throw error
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
export async function createProducts(products, context: ResolverContext) {
|
|
309
|
-
const { resolve, reject } = await promisesAll.all(products.map(product => createProduct(product, context)))
|
|
310
|
-
|
|
311
|
-
if (reject.length) {
|
|
312
|
-
reject.forEach(({ name, message }) => logger.error(`${name}: ${message}`))
|
|
313
|
-
|
|
314
|
-
return reject
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
return resolve
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
export async function updateProduct(id: string, patch, context) {
|
|
321
|
-
try {
|
|
322
|
-
const { user, tx, domain } = context.state
|
|
323
|
-
|
|
324
|
-
const bizplaceRepo: Repository<Bizplace> = tx.getRepository(Bizplace)
|
|
325
|
-
const productRepo: Repository<Product> = tx.getRepository(Product)
|
|
326
|
-
const productDetailRepo: Repository<ProductDetail> = tx.getRepository(ProductDetail)
|
|
327
|
-
|
|
328
|
-
let product = await productRepo.findOne({ where: { id }, relations: ['domain'] })
|
|
329
|
-
|
|
330
|
-
let productDetail = await productDetailRepo.findOne({
|
|
331
|
-
where: { product: { id }, isDefault: true },
|
|
332
|
-
relations: ['domain']
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
if (patch.productRef && patch.productRef.id) {
|
|
336
|
-
patch.productRef = await productRepo.findOne(patch.productRef.id)
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
let bizplace: Bizplace
|
|
340
|
-
if (product.bizplace?.id) {
|
|
341
|
-
bizplace = await bizplaceRepo.findOneBy({ id: product.bizplace.id })
|
|
342
|
-
} else {
|
|
343
|
-
bizplace = await bizplaceRepo.findOne({ where: { name: domain.name } })
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
await validateProduct(
|
|
347
|
-
{
|
|
348
|
-
...product,
|
|
349
|
-
...patch,
|
|
350
|
-
bizplace,
|
|
351
|
-
domain
|
|
352
|
-
},
|
|
353
|
-
context
|
|
354
|
-
)
|
|
355
|
-
|
|
356
|
-
if (patch.uom) {
|
|
357
|
-
product.primaryUnit = patch.uom
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
if (patch.uomValue) {
|
|
361
|
-
product.primaryValue = patch.uomValue
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
product = await productRepo.save({
|
|
365
|
-
...product,
|
|
366
|
-
...patch,
|
|
367
|
-
domain,
|
|
368
|
-
updater: user
|
|
369
|
-
})
|
|
370
|
-
|
|
371
|
-
if (productDetail) {
|
|
372
|
-
if (product.gtin) {
|
|
373
|
-
delete product.gtin
|
|
374
|
-
}
|
|
375
|
-
productDetail = await updateProductDetail(
|
|
376
|
-
productDetail.id,
|
|
377
|
-
new ProductDetail({ ...product, ...patch, product }),
|
|
378
|
-
context
|
|
379
|
-
)
|
|
380
|
-
} else {
|
|
381
|
-
productDetail = await createProductDetail(
|
|
382
|
-
new ProductDetail({ ...product, ...patch, product, isDefault: true }),
|
|
383
|
-
context
|
|
384
|
-
)
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
const productId = id
|
|
388
|
-
if (patch.thumbnail) {
|
|
389
|
-
await deleteAttachmentsByRef(null, { refBys: [productId] }, context)
|
|
390
|
-
await createAttachment(
|
|
391
|
-
null,
|
|
392
|
-
{
|
|
393
|
-
attachment: {
|
|
394
|
-
file: patch.thumbnail,
|
|
395
|
-
refType: Product.name,
|
|
396
|
-
refBy: product.id
|
|
397
|
-
}
|
|
398
|
-
},
|
|
399
|
-
context
|
|
400
|
-
)
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
return product
|
|
404
|
-
} catch (error) {
|
|
405
|
-
throw error
|
|
406
|
-
}
|
|
407
|
-
}
|