From c1cb9184e0ef9e97a99a44dbeedb56e9727f84c7 Mon Sep 17 00:00:00 2001 From: Gabriel Barros Date: Thu, 19 Oct 2023 10:34:03 -0300 Subject: [PATCH 1/2] Master Data Client Deletion --- CHANGELOG.md | 4 + manifest.json | 3 +- node/clients/index.ts | 13 ++- .../masterDataAggregationsFactory.ts | 12 +-- node/utils/withCustomSchemas.ts | 88 +++++++++++++++++++ 5 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 node/utils/withCustomSchemas.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 73b59dc..5f03c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- Master Data client deletion + ## [2.2.1] - 2022-11-03 ### Fixed diff --git a/manifest.json b/manifest.json index 5cb14ec..888bb31 100644 --- a/manifest.json +++ b/manifest.json @@ -12,8 +12,7 @@ "builders": { "node": "6.x", "docs": "0.x", - "graphql": "1.x", - "masterdata": "1.x" + "graphql": "1.x" }, "scripts": { "prereleasy": "bash lint.sh" diff --git a/node/clients/index.ts b/node/clients/index.ts index 5f94a3b..79fac20 100644 --- a/node/clients/index.ts +++ b/node/clients/index.ts @@ -10,27 +10,34 @@ import CheckoutExtended from './checkout' import { masterDataAggregateFor } from './masterDataAggegations/masterDataAggregationsFactory' import MessageCenterClient from './messageCenter' import { SpreadsheetEventBroadcasterClient } from './spreadsheetEventBroadcaster' +import { withCustomSchema } from '../utils/withCustomSchemas' // Extend the default IOClients implementation with our own custom clients. export class Clients extends IOClients { public get affiliatesOrders() { return this.getOrSet( 'affiliatesOrders', - masterDataFor('affiliatesOrders') + withCustomSchema( + '2.2.1', + masterDataFor('affiliatesOrders') + ) ) } public get commissionBySKU() { return this.getOrSet( 'commissionBySKU', - masterDataFor('commissionBySKU') + withCustomSchema( + '2.2.1', + masterDataFor('commissionBySKU') + ) ) } public get affiliatesOrdersAggregate() { return this.getOrSet( 'affiliatesOrdersAggregate', - masterDataAggregateFor('affiliatesOrders') + masterDataAggregateFor('affiliatesOrders', '2.2.1') ) } diff --git a/node/clients/masterDataAggegations/masterDataAggregationsFactory.ts b/node/clients/masterDataAggegations/masterDataAggregationsFactory.ts index ceb3034..0063245 100644 --- a/node/clients/masterDataAggegations/masterDataAggregationsFactory.ts +++ b/node/clients/masterDataAggegations/masterDataAggregationsFactory.ts @@ -13,7 +13,6 @@ export abstract class MasterDataEntity extends JanusClient { ): Promise } -const GLOBAL = '' /** * This is necessary since masterdata does not accept special characters on entity name * This function replaces `.` and `-` for `_` @@ -21,11 +20,9 @@ const GLOBAL = '' */ const normalizeEntityName = (str: string) => str.replace(/(\.)|-|:/gi, '_') -const versionDescriptor = (isProduction: boolean, workspace: string) => - isProduction ? GLOBAL : `-${workspace}` - export const masterDataAggregateFor = ( - entityName: string + entityName: string, + schemaVersion: string ): new (context: IOContext, options?: InstanceOptions) => MasterDataEntity => { return class extends MasterDataEntity { public dataEntity: string @@ -36,10 +33,7 @@ export const masterDataAggregateFor = ( const app = parseAppId(process.env.VTEX_APP_ID as string) this.inner = new MasterDataAggregations(ctx, options) - this.schema = `${app.version}${versionDescriptor( - ctx.production, - ctx.workspace - )}` + this.schema = `${schemaVersion}` this.dataEntity = normalizeEntityName(`${app.name}_${entityName}`) } diff --git a/node/utils/withCustomSchemas.ts b/node/utils/withCustomSchemas.ts new file mode 100644 index 0000000..c7dde80 --- /dev/null +++ b/node/utils/withCustomSchemas.ts @@ -0,0 +1,88 @@ +import type { InstanceOptions, IOContext } from '@vtex/api' +import type { PaginationArgs, WithMetadata } from '@vtex/clients' +import { MasterDataEntity } from '@vtex/clients' + +type ScrollInput = { + fields: Array | '_all'> + sort?: string + size?: number + mdToken?: string +} + +export const withCustomSchema = >( + schema: string, + _MasterdataClient: new ( + context: IOContext, + options?: InstanceOptions + ) => MasterDataEntity +): new ( + context: IOContext, + options?: InstanceOptions +) => MasterDataEntity => { + return class extends MasterDataEntity { + public dataEntity: string + public schema: string + public client: MasterDataEntity + + constructor(ctx: IOContext, options?: InstanceOptions) { + super(ctx, options) + this.client = new _MasterdataClient(ctx, options) + this.dataEntity = this.client.dataEntity + this.schema = schema + this.client.schema = schema + } + + public save(entity: TEntity) { + return this.client.save(entity) + } + + public update(id: string, fields: Partial) { + return this.client.update(id, fields) + } + + public saveOrUpdate(fields: TEntity & { id: string }) { + return this.client.saveOrUpdate(fields) + } + + public delete(id: string) { + return this.client.delete(id) + } + + // These es-lint disables are needed because I have to extend the class and its methods surpass the max params. + // eslint-disable-next-line max-params + public search>( + pagination: PaginationArgs, + fields: Array | '_all'>, + sort?: string, + where?: string + ): Promise, K>>> { + return this.client.search(pagination, fields, sort, where) + } + + // eslint-disable-next-line max-params + public searchRaw>( + pagination: PaginationArgs, + fields: Array | '_all'>, + sort?: string, + where?: string + ): Promise<{ + data: Array, K>> + pagination: { total: number; page: number; pageSize: number } + }> { + return this.client.searchRaw(pagination, fields, sort, where) + } + + public get>( + id: string, + fields: Array | '_all'> + ) { + return this.client.get(id, fields) + } + + public async scroll>( + input: ScrollInput + ) { + return this.client.scroll(input) + } + } +} From cf3124ceb6126c7e9f7920a5feb9e8def4b203a4 Mon Sep 17 00:00:00 2001 From: Gabriel Barros Date: Thu, 19 Oct 2023 17:00:06 -0300 Subject: [PATCH 2/2] Typings adjustments --- node/clients/index.ts | 5 +-- node/middlewares/validateChangedItems.ts | 2 +- node/package.json | 2 +- node/resolvers/affiliateOrders/index.ts | 2 +- node/resolvers/commissionsBySKU/index.ts | 2 +- node/services/ExportMDSheetService.ts | 7 +--- node/typings/commission.ts | 26 ++++++++++++- node/yarn.lock | 48 ++++++++++++------------ 8 files changed, 56 insertions(+), 38 deletions(-) diff --git a/node/clients/index.ts b/node/clients/index.ts index 79fac20..63a1f0a 100644 --- a/node/clients/index.ts +++ b/node/clients/index.ts @@ -1,10 +1,7 @@ import { IOClients } from '@vtex/api' import { masterDataFor, Catalog } from '@vtex/clients' -import type { - AffiliatesOrders, - CommissionBySKU, -} from 'vtex.affiliates-commission-service' +import type { AffiliatesOrders, CommissionBySKU } from '../typings/commission' import AuthenticationClient from './authenticationClient' import CheckoutExtended from './checkout' import { masterDataAggregateFor } from './masterDataAggegations/masterDataAggregationsFactory' diff --git a/node/middlewares/validateChangedItems.ts b/node/middlewares/validateChangedItems.ts index a12d2b8..8706520 100644 --- a/node/middlewares/validateChangedItems.ts +++ b/node/middlewares/validateChangedItems.ts @@ -1,6 +1,6 @@ import type { EventContext } from '@vtex/api' -import type { AffiliatesOrders } from 'vtex.affiliates-commission-service' +import type { AffiliatesOrders } from '../typings/commission' import type { Clients } from '../clients' import { CommissionBySKUService } from '../services/CommissionBySKUService' import type { diff --git a/node/package.json b/node/package.json index b1c008d..6b9ec49 100644 --- a/node/package.json +++ b/node/package.json @@ -14,7 +14,7 @@ "@types/jest": "^24.0.18", "@types/node": "^12.0.0", "@types/ramda": "types/npm-ramda#dist", - "@vtex/api": "6.45.12", + "@vtex/api": "6.45.24", "@vtex/test-tools": "^3.4.1", "@vtex/tsconfig": "^0.5.6", "typescript": "3.9.7", diff --git a/node/resolvers/affiliateOrders/index.ts b/node/resolvers/affiliateOrders/index.ts index 7b7cef8..4189c0e 100644 --- a/node/resolvers/affiliateOrders/index.ts +++ b/node/resolvers/affiliateOrders/index.ts @@ -1,11 +1,11 @@ import type { - AffiliatesOrders, QueryAffiliateOrderArgs, QueryAffiliateOrdersArgs, MutationExportAffiliateOrdersArgs, Affiliate, } from 'vtex.affiliates-commission-service' +import type { AffiliatesOrders } from '../../typings/commission' import { ExportMDSheetService } from '../../services/ExportMDSheetService' import { parseAffiliateOrdersFilters } from '../../utils/filters' import { totalizersProfileFieldResolver } from './totalizerProfileFieldResolver' diff --git a/node/resolvers/commissionsBySKU/index.ts b/node/resolvers/commissionsBySKU/index.ts index 75ce21a..8f720ac 100644 --- a/node/resolvers/commissionsBySKU/index.ts +++ b/node/resolvers/commissionsBySKU/index.ts @@ -1,11 +1,11 @@ import type { - CommissionBySKU, QueryCommissionsBySkuArgs, MutationExportCommissionsBySkuArgs, MutationUpdateCommissionArgs, MutationImportCommissionsBySkuArgs, } from 'vtex.affiliates-commission-service' +import type { CommissionBySKU } from '../../typings/commission' import { ExportMDSheetService } from '../../services/ExportMDSheetService' import { parseCommissionsBySKUFilters } from '../../utils/filters' import { ImportCommissionsService } from '../../services/ImportCommissionsService' diff --git a/node/services/ExportMDSheetService.ts b/node/services/ExportMDSheetService.ts index 0c63bb0..064ccf9 100644 --- a/node/services/ExportMDSheetService.ts +++ b/node/services/ExportMDSheetService.ts @@ -1,12 +1,9 @@ import { stream, utils } from 'xlsx' import { v4 as uuid } from 'uuid' import type { MasterDataEntity } from '@vtex/clients' -import type { - AffiliatesOrders, - CommissionBySKU, - Affiliate, -} from 'vtex.affiliates-commission-service' +import type { Affiliate } from 'vtex.affiliates-commission-service' +import type { AffiliatesOrders, CommissionBySKU } from '../typings/commission' import type { AffiliateOrderExportingRow, MDEntityForExporting, diff --git a/node/typings/commission.ts b/node/typings/commission.ts index 1958ccc..9a67c5c 100644 --- a/node/typings/commission.ts +++ b/node/typings/commission.ts @@ -1,5 +1,10 @@ import type { MasterDataEntity, WithMetadata } from '@vtex/clients' -import type { CommissionBySKU } from 'vtex.affiliates-commission-service' + +export interface CommissionBySKU { + commission: number + refId?: string + [k: string]: unknown +} export type CommissionClient = MasterDataEntity @@ -13,3 +18,22 @@ export type CommissionServiceErrors = Array<{ id: string message: string }> + +export interface AffiliatesOrders { + affiliateId: string + status?: string + userEmail: string + orderTotal?: number + orderTotalCommission?: number + orderDate?: string + orderItems: Array<{ + skuId: string + skuName: string + skuImageUrl?: string + price: number + quantity: number + commission: number + [k: string]: unknown + }> + [k: string]: unknown +} diff --git a/node/yarn.lock b/node/yarn.lock index 6757103..c648092 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -1678,10 +1678,10 @@ resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.3.tgz#781d360c282436494b32fe7d9f7f8e64b3118aa3" integrity sha512-fbF6oTd4sGGy0xjHPKAt+eS2CrxJ3+6gQ3FGcBoIJR2TLAyCkCyI8JqZNy+FeON0AhVgNJoUumVoZQjBFUqHkw== -"@vtex/api@6.45.12": - version "6.45.12" - resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.12.tgz#b13c04398b12f576263ea823369f09c970d57479" - integrity sha512-SVLKo+Q/TxQy+1UKzH8GswTI3F2OCRCLfgaNQOrVAVdbM6Ci4wzTeX8j/S4Q1aEEnqBFlH/wVpHf8I6NBa+g9A== +"@vtex/api@6.45.24": + version "6.45.24" + resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.24.tgz#5643d80de9b5ac9491c03a47be0329872896a6eb" + integrity sha512-BaNdncM2Jfqdu/DikxrDVH7fdek5vH02nvH4RCyNcZ3KSSYZaKk4QGr2EjEHI/rhkOIbJOlOAW5ol4uDcGbQjQ== dependencies: "@types/koa" "^2.11.0" "@types/koa-compose" "^3.2.3" @@ -1701,7 +1701,7 @@ fs-extra "^7.0.0" graphql "^14.5.8" graphql-tools "^4.0.6" - graphql-upload "^8.1.0" + graphql-upload "^13.0.0" jaeger-client "^3.18.0" js-base64 "^2.5.1" koa "^2.11.0" @@ -1712,7 +1712,7 @@ mime-types "^2.1.12" opentracing "^0.14.4" p-limit "^2.2.0" - prom-client "^12.0.0" + prom-client "^14.2.0" qs "^6.5.1" querystring "^0.2.0" ramda "^0.26.0" @@ -3230,10 +3230,10 @@ fresh@~0.5.2: resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7" integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac= -fs-capacitor@^2.0.4: - version "2.0.4" - resolved "https://registry.yarnpkg.com/fs-capacitor/-/fs-capacitor-2.0.4.tgz#5a22e72d40ae5078b4fe64fe4d08c0d3fc88ad3c" - integrity sha512-8S4f4WsCryNw2mJJchi46YgB6CR5Ze+4L1h8ewl9tEpL4SJ3ZO+c/bS4BWhB8bK+O3TMqhuZarTitd0S0eh2pA== +fs-capacitor@^6.2.0: + version "6.2.0" + resolved "https://registry.yarnpkg.com/fs-capacitor/-/fs-capacitor-6.2.0.tgz#fa79ac6576629163cb84561995602d8999afb7f5" + integrity sha512-nKcE1UduoSKX27NSZlg879LdQc94OtbOsEmKMN2MBNudXREvijRKx2GEBsTMTfws+BrbkJoEuynbGSVRSpauvw== fs-constants@^1.0.0: version "1.0.0" @@ -3359,15 +3359,15 @@ graphql-tools@^4.0.6: iterall "^1.1.3" uuid "^3.1.0" -graphql-upload@^8.1.0: - version "8.1.0" - resolved "https://registry.yarnpkg.com/graphql-upload/-/graphql-upload-8.1.0.tgz#6d0ab662db5677a68bfb1f2c870ab2544c14939a" - integrity sha512-U2OiDI5VxYmzRKw0Z2dmfk0zkqMRaecH9Smh1U277gVgVe9Qn+18xqf4skwr4YJszGIh7iQDZ57+5ygOK9sM/Q== +graphql-upload@^13.0.0: + version "13.0.0" + resolved "https://registry.yarnpkg.com/graphql-upload/-/graphql-upload-13.0.0.tgz#1a255b64d3cbf3c9f9171fa62a8fb0b9b59bb1d9" + integrity sha512-YKhx8m/uOtKu4Y1UzBFJhbBGJTlk7k4CydlUUiNrtxnwZv0WigbRHP+DVhRNKt7u7DXOtcKZeYJlGtnMXvreXA== dependencies: busboy "^0.3.1" - fs-capacitor "^2.0.4" - http-errors "^1.7.3" - object-path "^0.11.4" + fs-capacitor "^6.2.0" + http-errors "^1.8.1" + object-path "^0.11.8" graphql@^14.0.0, graphql@^14.5.8: version "14.7.0" @@ -3519,7 +3519,7 @@ http-errors@2.0.0: statuses "2.0.1" toidentifier "1.0.1" -http-errors@^1.3.1, http-errors@^1.6.3, http-errors@^1.7.3, http-errors@~1.8.0: +http-errors@^1.3.1, http-errors@^1.6.3, http-errors@^1.8.1, http-errors@~1.8.0: version "1.8.1" resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.8.1.tgz#7c3f28577cbc8a207388455dbd62295ed07bd68c" integrity sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g== @@ -4882,7 +4882,7 @@ object-keys@^1.1.1: resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e" integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA== -object-path@^0.11.4: +object-path@^0.11.8: version "0.11.8" resolved "https://registry.yarnpkg.com/object-path/-/object-path-0.11.8.tgz#ed002c02bbdd0070b78a27455e8ae01fc14d4742" integrity sha512-YJjNZrlXJFM42wTBn6zgOJVar9KFJvzx6sTWDte8sWZF//cnjl0BxHNpfZx+ZffXX63A9q0b1zsFiBX4g4X5KA== @@ -5143,10 +5143,10 @@ process@^0.10.0: resolved "https://registry.yarnpkg.com/process/-/process-0.10.1.tgz#842457cc51cfed72dc775afeeafb8c6034372725" integrity sha1-hCRXzFHP7XLcd1r+6vuMYDQ3JyU= -prom-client@^12.0.0: - version "12.0.0" - resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-12.0.0.tgz#9689379b19bd3f6ab88a9866124db9da3d76c6ed" - integrity sha512-JbzzHnw0VDwCvoqf8y1WDtq4wSBAbthMB1pcVI/0lzdqHGJI3KBJDXle70XK+c7Iv93Gihqo0a5LlOn+g8+DrQ== +prom-client@^14.2.0: + version "14.2.0" + resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.2.0.tgz#ca94504e64156f6506574c25fb1c34df7812cf11" + integrity sha512-sF308EhTenb/pDRPakm+WgiN+VdM/T1RaHj1x+MvAuT8UiQP8JmOEbxVqtkbfR4LrvOg5n7ic01kRBDGXjYikA== dependencies: tdigest "^0.1.1" @@ -5809,7 +5809,7 @@ static-extend@^0.1.1: define-property "^0.2.5" object-copy "^0.1.0" -stats-lite@vtex/node-stats-lite#dist: +"stats-lite@github:vtex/node-stats-lite#dist": version "2.2.0" resolved "https://codeload.github.com/vtex/node-stats-lite/tar.gz/1b0d39cc41ef7aaecfd541191f877887a2044797" dependencies: