@things-factory/product-base 8.0.6 → 9.0.0-beta.12

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.
Files changed (63) hide show
  1. package/dist-server/service/product/product-mutation.js +1 -1
  2. package/dist-server/service/product/product-mutation.js.map +1 -1
  3. package/dist-server/service/product/product-query.d.ts +1 -1
  4. package/dist-server/service/product/product-query.js +5 -7
  5. package/dist-server/service/product/product-query.js.map +1 -1
  6. package/dist-server/service/product/product-types.d.ts +2 -8
  7. package/dist-server/service/product/product-types.js +2 -26
  8. package/dist-server/service/product/product-types.js.map +1 -1
  9. package/dist-server/service/product/product.d.ts +2 -7
  10. package/dist-server/service/product/product.js +1 -24
  11. package/dist-server/service/product/product.js.map +1 -1
  12. package/dist-server/tsconfig.tsbuildinfo +1 -1
  13. package/package.json +5 -5
  14. package/server/constants/index.ts +0 -1
  15. package/server/constants/product.ts +0 -24
  16. package/server/controllers/index.ts +0 -0
  17. package/server/index.ts +0 -2
  18. package/server/middlewares/index.ts +0 -0
  19. package/server/migrations/index.ts +0 -9
  20. package/server/service/index.ts +0 -61
  21. package/server/service/product/index.ts +0 -6
  22. package/server/service/product/product-mutation.ts +0 -407
  23. package/server/service/product/product-query.ts +0 -338
  24. package/server/service/product/product-types.ts +0 -470
  25. package/server/service/product/product.ts +0 -562
  26. package/server/service/product/validate-product.ts +0 -42
  27. package/server/service/product-bundle/index.ts +0 -6
  28. package/server/service/product-bundle/product-bundle-mutation.ts +0 -140
  29. package/server/service/product-bundle/product-bundle-query.ts +0 -104
  30. package/server/service/product-bundle/product-bundle-types.ts +0 -51
  31. package/server/service/product-bundle/product-bundle.ts +0 -102
  32. package/server/service/product-bundle-setting/index.ts +0 -6
  33. package/server/service/product-bundle-setting/product-bundle-setting-mutation.ts +0 -168
  34. package/server/service/product-bundle-setting/product-bundle-setting-query.ts +0 -139
  35. package/server/service/product-bundle-setting/product-bundle-setting-types.ts +0 -41
  36. package/server/service/product-bundle-setting/product-bundle-setting.ts +0 -45
  37. package/server/service/product-combination/index.ts +0 -6
  38. package/server/service/product-combination/product-combination-mutation.ts +0 -148
  39. package/server/service/product-combination/product-combination-query.ts +0 -48
  40. package/server/service/product-combination/product-combination-type.ts +0 -50
  41. package/server/service/product-combination/product-combination.ts +0 -116
  42. package/server/service/product-combination-setting/index.ts +0 -6
  43. package/server/service/product-combination-setting/product-combination-setting-mutation.ts +0 -248
  44. package/server/service/product-combination-setting/product-combination-setting-query.ts +0 -176
  45. package/server/service/product-combination-setting/product-combination-setting-type.ts +0 -44
  46. package/server/service/product-combination-setting/product-combination-setting.ts +0 -77
  47. package/server/service/product-detail/index.ts +0 -6
  48. package/server/service/product-detail/product-detail-mutation.ts +0 -238
  49. package/server/service/product-detail/product-detail-query.ts +0 -213
  50. package/server/service/product-detail/product-detail-types.ts +0 -280
  51. package/server/service/product-detail/product-detail.ts +0 -388
  52. package/server/service/product-detail-bizplace-setting/index.ts +0 -6
  53. package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting-mutation.ts +0 -118
  54. package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting-query.ts +0 -90
  55. package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting-types.ts +0 -62
  56. package/server/service/product-detail-bizplace-setting/product-detail-bizplace-setting.ts +0 -104
  57. package/server/service/product-set/index.ts +0 -6
  58. package/server/service/product-set/product-set-mutation.ts +0 -149
  59. package/server/service/product-set/product-set-query.ts +0 -114
  60. package/server/service/product-set/product-set-types.ts +0 -45
  61. package/server/service/product-set/product-set.ts +0 -95
  62. package/server/utils/index.ts +0 -1
  63. package/server/utils/product-util.ts +0 -11
@@ -1,338 +0,0 @@
1
- import { Arg, Args, Ctx, Directive, FieldResolver, Query, Resolver, Root } from 'type-graphql'
2
- import { Brackets, In, SelectQueryBuilder } from 'typeorm'
3
-
4
- import { Attachment } from '@things-factory/attachment-base'
5
- import { User } from '@things-factory/auth-base'
6
- import {
7
- Bizplace,
8
- getCompaniesBizplaces,
9
- getCompanyBizplace,
10
- getMyBizplace,
11
- getPartnersCompanyBizplaces,
12
- getPermittedBizplaceIds
13
- } from '@things-factory/biz-base'
14
- import { logger } from '@things-factory/env'
15
- import { buildCondition, Domain, getQueryBuilderFromListParams, getRepository, ListParam } from '@things-factory/shell'
16
-
17
- import { ProductDetail } from '../product-detail/product-detail'
18
- import { Product } from './product'
19
- import { ProductList } from './product-types'
20
-
21
- const debug = require('debug')('things-factory:product-base:product')
22
-
23
- @Resolver(Product)
24
- export class ProductQuery {
25
- @Directive('@privilege(category: "order", privilege: "query", domainOwnerGranted: true)')
26
- @Query(returns => Product)
27
- async product(@Arg('sku') sku: string, @Ctx() context: ResolverContext): Promise<Product> {
28
- const { domain, user, bizplace } = context.state
29
-
30
- if (bizplace) {
31
- return await getRepository(Product).findOne({
32
- where: {
33
- domain: { id: domain.id },
34
- sku,
35
- bizplace: { id: bizplace.id }
36
- },
37
- relations: ['domain', 'bizplace', 'productRef', 'creator', 'updater']
38
- })
39
- } else {
40
- return await getRepository(Product).findOne({
41
- where: {
42
- domain: { id: domain.id },
43
- sku,
44
- bizplace: { id: In(await getPermittedBizplaceIds(domain, user)) }
45
- },
46
- relations: ['domain', 'bizplace', 'productRef', 'creator', 'updater']
47
- })
48
- }
49
- }
50
-
51
- @Directive('@privilege(category: "order", privilege: "query", domainOwnerGranted: true)')
52
- @Query(returns => Product)
53
- async productById(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<Product> {
54
- const { domain, user } = context.state
55
- return await getRepository(Product).findOne({
56
- where: { domain: { id: domain.id }, id },
57
- relations: ['domain', 'bizplace', 'productRef', 'creator', 'updater']
58
- })
59
- }
60
-
61
- @Directive('@privilege(category: "order", privilege: "query", domainOwnerGranted: true)')
62
- @Query(returns => ProductList)
63
- async products(@Args(type => ListParam) params: ListParam, @Ctx() context: ResolverContext): Promise<ProductList> {
64
- const { domain, user } = context.state
65
- const { domains }: { domains: Domain[] } = user as any /* TODO clarify this */
66
- let bizplaces: Bizplace[]
67
-
68
- if (!params.filters.find((filter: any) => filter.name === 'bizplace_id')) {
69
- const warehouseDomain: Domain = domain
70
- const companiesBizplaces: Bizplace[] = await getCompaniesBizplaces(domains, warehouseDomain)
71
- const partnersCompanyBizplaces: Bizplace[] = await getPartnersCompanyBizplaces(domain, user)
72
-
73
- bizplaces = [...companiesBizplaces, ...partnersCompanyBizplaces]
74
- } else {
75
- const bizplaceId: string[] = params.filters.filter(param => param.name == 'bizplace_id').map(item => item.value)
76
-
77
- const foundBizplace: Bizplace = await getRepository(Bizplace).findOne({
78
- where: { id: bizplaceId[0] },
79
- relations: ['company', 'company.domain']
80
- })
81
-
82
- if (foundBizplace) {
83
- const companyDomain: Domain = foundBizplace.company?.domain
84
- const companyBizplace: Bizplace = await getRepository(Bizplace).findOne({
85
- where: { domain: { id: companyDomain.id } }
86
- })
87
-
88
- bizplaces = [companyBizplace, foundBizplace]
89
- }
90
- }
91
-
92
- const qb: SelectQueryBuilder<Product> = buildCustomConditions(params, context, bizplaces)
93
-
94
- let [items, total] = await qb
95
- .leftJoinAndSelect('Product.productDetails', 'ProductDetails')
96
- .leftJoinAndSelect('ProductDetails.childProductDetail', 'ChildProductDetail')
97
- .getManyAndCount()
98
-
99
- return { items, total }
100
- }
101
-
102
- @Directive('@privilege(category: "order", privilege: "query", domainOwnerGranted: true)')
103
- @Query(returns => ProductList)
104
- async myBizplaceProducts(
105
- @Args(type => ListParam) params: ListParam,
106
- @Ctx() context: ResolverContext
107
- ): Promise<ProductList> {
108
- try {
109
- const { domain, user }: { domain: Domain; user: User } = context.state
110
- const bizplaceFilter = params.filters.find(filter => filter.name === 'bizplace_id')
111
-
112
- let bizplaces: Bizplace[] = []
113
- if (!bizplaceFilter) {
114
- bizplaces.push(await getRepository(Bizplace).findOne({ where: { domain: { id: domain.id } } }))
115
- }
116
-
117
- const qb: SelectQueryBuilder<Product> = buildCustomConditions(params, context, bizplaces)
118
-
119
- let [items, total] = await qb
120
- .leftJoinAndSelect('Product.productDetails', 'ProductDetails')
121
- .leftJoinAndSelect('Product.routing', 'Routing')
122
- .leftJoinAndSelect('Product.deliverTo', 'deliverTo')
123
- .leftJoinAndSelect('Product.manufacturedBy', 'manufacturedBy')
124
- .getManyAndCount()
125
-
126
- items = items.map(itm => {
127
- let defaultProductDetail = itm.productDetails.find(itm => itm.isDefault)
128
- return {
129
- ...itm,
130
- ...new ProductDetail(defaultProductDetail, true)
131
- }
132
- })
133
-
134
- return { items, total }
135
- } catch (error) {
136
- logger.error(`product-query[myBizplaceProducts]: ${error}`)
137
- throw error
138
- }
139
- }
140
-
141
- @Directive('@privilege(category: "order", privilege: "query", domainOwnerGranted: true)')
142
- @Query(returns => ProductList)
143
- async productsByBizplace(
144
- @Args(type => ListParam) params: ListParam,
145
- @Ctx() context: ResolverContext
146
- ): Promise<ProductList> {
147
- const { domain, user } = context.state
148
- const bizplaceFilter = params.filters.find(x => x.name == 'bizplace')
149
-
150
- try {
151
- let bizplaces: Bizplace[]
152
- if (!bizplaceFilter || bizplaceFilter.value == '') {
153
- bizplaces = [await getMyBizplace(domain, user), await getCompanyBizplace(domain, user)]
154
- } else {
155
- let myBizplace: Bizplace = await getRepository(Bizplace).findOne({
156
- where: { id: bizplaceFilter.value },
157
- relations: ['company', 'company.domain']
158
- })
159
-
160
- bizplaces = [
161
- myBizplace,
162
- await getRepository(Bizplace).findOne({
163
- where: { domain: { id: myBizplace.company.domain.id } }
164
- })
165
- ]
166
- }
167
-
168
- const qb: SelectQueryBuilder<Product> = buildCustomConditions(params, context, bizplaces)
169
-
170
- let [items, total] = await qb.leftJoinAndSelect('Product.productDetails', 'ProductDetails').getManyAndCount()
171
-
172
- items = items.map(itm => {
173
- let defaultProductDetail = itm.productDetails?.find(itm => itm.isDefault)
174
- return {
175
- ...itm,
176
- ...new ProductDetail(defaultProductDetail, true)
177
- }
178
- })
179
-
180
- return { items, total }
181
- } catch (error) {
182
- throw error
183
- }
184
- }
185
-
186
- /**
187
- * @description this resolver was specially made to return huge products data
188
- * without dropping off performance due to typegraphql limitations
189
- * @returns pure product data only. there will be no other table joined to avoid
190
- * ORM lagging issue (converting to object extremely withdraws the performance)
191
- * */
192
- @Directive('@privilege(category: "order", privilege: "query", domainOwnerGranted: true)')
193
- @Query(returns => ProductList)
194
- async pureProductsByBizplace(
195
- @Args(type => ListParam) params: ListParam,
196
- @Ctx() context: ResolverContext
197
- ): Promise<ProductList> {
198
- const { domain, user } = context.state
199
- const bizplaceFilter = params.filters.find(x => x.name == 'bizplace')
200
-
201
- try {
202
- let bizplaces: Bizplace[]
203
- if (!bizplaceFilter || bizplaceFilter.value == '') {
204
- bizplaces = [await getMyBizplace(domain, user), await getCompanyBizplace(domain, user)]
205
- } else {
206
- let myBizplace: Bizplace = await getRepository(Bizplace).findOne({
207
- where: { id: bizplaceFilter.value },
208
- relations: ['company', 'company.domain']
209
- })
210
-
211
- bizplaces = [
212
- myBizplace,
213
- await getRepository(Bizplace).findOne({
214
- where: { domain: { id: myBizplace.company.domain.id } }
215
- })
216
- ]
217
- }
218
-
219
- const qb: SelectQueryBuilder<Product> = buildCustomConditions(params, context, bizplaces)
220
- let items = await (await qb.getRawMany()).map(item => new Product(item))
221
- let total = await qb.getCount()
222
-
223
- return { items, total }
224
- } catch (error) {
225
- throw error
226
- }
227
- }
228
-
229
- @FieldResolver(type => Domain)
230
- async domain(@Root() product: Product): Promise<Domain> {
231
- if (product.domainId) return await getRepository(Domain).findOneBy({ id: product.domainId })
232
- else return null
233
- }
234
-
235
- @FieldResolver(type => Bizplace)
236
- async bizplace(@Root() product: Product): Promise<Bizplace> {
237
- if (product.bizplaceId) return await getRepository(Bizplace).findOneBy({ id: product.bizplaceId })
238
- else return null
239
- }
240
-
241
- @FieldResolver(type => User)
242
- async creator(@Root() product: Product): Promise<User> {
243
- if (product.creatorId) return await getRepository(User).findOneBy({ id: product.creatorId })
244
- else return null
245
- }
246
-
247
- @FieldResolver(type => User)
248
- async updater(@Root() product: Product): Promise<User> {
249
- if (product.updaterId) return await getRepository(User).findOneBy({ id: product.updaterId })
250
- else return null
251
- }
252
-
253
- @FieldResolver(type => Product)
254
- async parentProductRef(@Root() product: Product): Promise<Product> {
255
- if (product.parentProductRefId) return await getRepository(Product).findOneBy({ id: product.parentProductRefId })
256
- else return null
257
- }
258
-
259
- @FieldResolver(type => Product)
260
- async productRef(@Root() product: Product): Promise<Product> {
261
- if (product.productRefId) return await getRepository(Product).findOneBy({ id: product.productRefId })
262
- else return null
263
- }
264
-
265
- @FieldResolver(type => String)
266
- async thumbnail(@Root() product: Product): Promise<string | undefined> {
267
- const attachment: Attachment = await getRepository(Attachment).findOne({
268
- where: {
269
- domain: { id: product.domainId },
270
- refBy: product.id
271
- }
272
- })
273
-
274
- return attachment?.fullpath
275
- }
276
-
277
- @FieldResolver(type => String)
278
- productInformation(@Root() product: Product): String {
279
- return `[${product.sku}] ${product.name}${product.description ? ` - ${product.description}` : ''}`
280
- }
281
-
282
- @FieldResolver(type => String, { nullable: true })
283
- async warehouseName(@Root() product: Product): Promise<String | undefined> {
284
- if (!product.warehouseId) return null
285
-
286
- const items = await getRepository(Product).query(`
287
- SELECT w.name as "warehouseName" FROM products p LEFT JOIN warehouses w ON p.warehouse_id::uuid = w.id and w.domain_id = p.domain_id WHERE p.id = '${product.id}'
288
- `)
289
-
290
- return items[0].warehouseName || null
291
- }
292
- }
293
-
294
- function buildCustomConditions(params, context, bizplaces): SelectQueryBuilder<Product> {
295
- const deletedFilter = params.filters.find(x => x.name == 'deleted')
296
- const productFilters = params.filters.find(x => x.name == 'product_info')
297
- const productFilterColumns = ['sku', 'brandSku', 'name', 'description', 'brand', 'subBrand']
298
-
299
- try {
300
- params.filters = [
301
- ...params.filters.filter(x => ['product_info', 'bizplace', 'bizplaceId', 'deleted'].indexOf(x.name) == -1),
302
- { name: 'deletedAt', operator: deletedFilter?.value ? 'is_not_null' : 'is_null' },
303
- { name: 'bizplaceId', operator: 'in', value: bizplaces.map(bizplace => bizplace.id) }
304
- ]
305
- } catch (err) {
306
- // error with no bizplace.
307
- debug('[WARN] params.filters', err.message)
308
- }
309
-
310
- const qb = getQueryBuilderFromListParams({
311
- repository: getRepository(Product),
312
- alias: 'Product',
313
- params,
314
- domain: context.domain,
315
- searchables: ['sku', 'name', 'description']
316
- })
317
-
318
- if (productFilters) {
319
- qb.andWhere(
320
- new Brackets(qb2 => {
321
- productFilterColumns.forEach(filter => {
322
- const condition = buildCondition(
323
- qb.alias,
324
- filter,
325
- productFilters.operator,
326
- productFilters.value,
327
- productFilters.relation,
328
- Object.keys(qb.getParameters()).length
329
- )
330
-
331
- qb2.orWhere(condition.clause, condition.parameters)
332
- })
333
- })
334
- )
335
- }
336
-
337
- return qb
338
- }