@unchainedshop/utils 3.0.0-alpha7 → 3.0.0-rc10

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 (74) hide show
  1. package/lib/calculation.d.ts +0 -5
  2. package/lib/calculation.d.ts.map +1 -1
  3. package/lib/calculation.js +0 -20
  4. package/lib/calculation.js.map +1 -1
  5. package/lib/ch/addressToString.d.ts +12 -0
  6. package/lib/ch/addressToString.d.ts.map +1 -0
  7. package/lib/ch/addressToString.js +14 -0
  8. package/lib/ch/addressToString.js.map +1 -0
  9. package/lib/ch/index.d.ts +3 -0
  10. package/lib/ch/index.d.ts.map +1 -0
  11. package/lib/ch/index.js +4 -0
  12. package/lib/ch/index.js.map +1 -0
  13. package/lib/ch/priceToString.d.ts +6 -0
  14. package/lib/ch/priceToString.d.ts.map +1 -0
  15. package/lib/ch/priceToString.js +6 -0
  16. package/lib/ch/priceToString.js.map +1 -0
  17. package/lib/director/BaseDirector.js +1 -1
  18. package/lib/director/BaseDirector.js.map +1 -1
  19. package/lib/sha1.d.ts +2 -0
  20. package/lib/sha1.d.ts.map +1 -0
  21. package/lib/sha1.js +6 -0
  22. package/lib/sha1.js.map +1 -0
  23. package/lib/sha256.d.ts +2 -0
  24. package/lib/sha256.d.ts.map +1 -0
  25. package/lib/sha256.js +6 -0
  26. package/lib/sha256.js.map +1 -0
  27. package/lib/utils-index.d.ts +17 -7
  28. package/lib/utils-index.d.ts.map +1 -1
  29. package/lib/utils-index.js +3 -13
  30. package/lib/utils-index.js.map +1 -1
  31. package/package.json +8 -11
  32. package/src/calculation.test.ts +0 -43
  33. package/src/calculation.ts +0 -26
  34. package/src/ch/addressToString.ts +23 -0
  35. package/src/ch/index.ts +3 -0
  36. package/src/ch/priceToString.ts +6 -0
  37. package/src/director/BaseDirector.ts +1 -1
  38. package/src/sha1.ts +5 -0
  39. package/src/sha256.ts +5 -0
  40. package/src/utils-index.test.ts +1 -1
  41. package/src/utils-index.ts +19 -17
  42. package/tsconfig.json +4 -4
  43. package/lib/director/BaseDiscountAdapter.d.ts +0 -38
  44. package/lib/director/BaseDiscountAdapter.d.ts.map +0 -1
  45. package/lib/director/BaseDiscountAdapter.js +0 -48
  46. package/lib/director/BaseDiscountAdapter.js.map +0 -1
  47. package/lib/director/BaseDiscountDirector.d.ts +0 -13
  48. package/lib/director/BaseDiscountDirector.d.ts.map +0 -1
  49. package/lib/director/BaseDiscountDirector.js +0 -50
  50. package/lib/director/BaseDiscountDirector.js.map +0 -1
  51. package/lib/director/BasePricingAdapter.d.ts +0 -46
  52. package/lib/director/BasePricingAdapter.d.ts.map +0 -1
  53. package/lib/director/BasePricingAdapter.js +0 -25
  54. package/lib/director/BasePricingAdapter.js.map +0 -1
  55. package/lib/director/BasePricingDirector.d.ts +0 -16
  56. package/lib/director/BasePricingDirector.d.ts.map +0 -1
  57. package/lib/director/BasePricingDirector.js +0 -61
  58. package/lib/director/BasePricingDirector.js.map +0 -1
  59. package/lib/director/BasePricingSheet.d.ts +0 -47
  60. package/lib/director/BasePricingSheet.d.ts.map +0 -1
  61. package/lib/director/BasePricingSheet.js +0 -54
  62. package/lib/director/BasePricingSheet.js.map +0 -1
  63. package/lib/random-value-hex.d.ts +0 -2
  64. package/lib/random-value-hex.d.ts.map +0 -1
  65. package/lib/random-value-hex.js +0 -8
  66. package/lib/random-value-hex.js.map +0 -1
  67. package/src/director/BaseDiscountAdapter.ts +0 -89
  68. package/src/director/BaseDiscountDirector.ts +0 -77
  69. package/src/director/BasePricingAdapter.ts +0 -89
  70. package/src/director/BasePricingDirector.ts +0 -133
  71. package/src/director/BasePricingSheet.ts +0 -121
  72. package/src/director/base-discount-adapter.test.ts +0 -49
  73. package/src/director/base-price-sheet.test.ts +0 -100
  74. package/src/random-value-hex.ts +0 -8
@@ -1,48 +0,0 @@
1
- import { log, LogLevel } from '@unchainedshop/logger';
2
- export const BaseDiscountAdapter = {
3
- orderIndex: 0,
4
- isManualAdditionAllowed: async () => {
5
- return false;
6
- },
7
- // return true if a discount is allowed to get removed manually by a user
8
- isManualRemovalAllowed: async () => {
9
- return false;
10
- },
11
- actions: async () => ({
12
- // return true if a discount is valid to be part of the order
13
- // without input of a user. that could be a time based global discount
14
- // like a 10% discount day
15
- // if you return false, this discount will
16
- // get removed from the order before any price calculation
17
- // takes place.
18
- // eslint-disable-next-line
19
- isValidForSystemTriggering: async () => {
20
- return false;
21
- },
22
- // return an arbitrary JSON serializable object with reservation data
23
- // this method is called when a discount is added through a manual code and let's
24
- // you manually deduct expendable discounts (coupon balances for ex.) before checkout
25
- reserve: async () => {
26
- return {};
27
- },
28
- // return void, allows you to free up any reservations in backend systems
29
- release: async () => {
30
- return null;
31
- },
32
- // return true if a discount is valid to be part of the order.
33
- // if you return false, this discount will
34
- // get removed from the order before any price calculation
35
- // takes place.
36
- isValidForCodeTriggering: async () => {
37
- return false;
38
- },
39
- // returns the appropriate discount context for a calculation adapter
40
- discountForPricingAdapterKey() {
41
- return null;
42
- },
43
- }),
44
- log(message, { level = LogLevel.Debug, ...options } = {}) {
45
- return log(message, { level, ...options });
46
- },
47
- };
48
- //# sourceMappingURL=BaseDiscountAdapter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseDiscountAdapter.js","sourceRoot":"","sources":["../../src/director/BaseDiscountAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAmCtD,MAAM,CAAC,MAAM,mBAAmB,GAAiE;IAC/F,UAAU,EAAE,CAAC;IAEb,uBAAuB,EAAE,KAAK,IAAI,EAAE;QAClC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,yEAAyE;IACzE,sBAAsB,EAAE,KAAK,IAAI,EAAE;QACjC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC;QACpB,6DAA6D;QAC7D,sEAAsE;QACtE,0BAA0B;QAC1B,0CAA0C;QAC1C,0DAA0D;QAC1D,eAAe;QACf,2BAA2B;QAC3B,0BAA0B,EAAE,KAAK,IAAI,EAAE;YACrC,OAAO,KAAK,CAAC;QACf,CAAC;QAED,qEAAqE;QACrE,iFAAiF;QACjF,qFAAqF;QACrF,OAAO,EAAE,KAAK,IAAI,EAAE;YAClB,OAAO,EAAE,CAAC;QACZ,CAAC;QAED,yEAAyE;QACzE,OAAO,EAAE,KAAK,IAAI,EAAE;YAClB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,8DAA8D;QAC9D,0CAA0C;QAC1C,0DAA0D;QAC1D,eAAe;QACf,wBAAwB,EAAE,KAAK,IAAI,EAAE;YACnC,OAAO,KAAK,CAAC;QACf,CAAC;QAED,qEAAqE;QACrE,4BAA4B;YAC1B,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAC;IAEF,GAAG,CAAC,OAAe,EAAE,EAAE,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,GAAG,OAAO,EAAE,GAAG,EAAE;QAC9D,OAAO,GAAG,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAC7C,CAAC;CACF,CAAC"}
@@ -1,13 +0,0 @@
1
- import { IBaseDirector } from './BaseDirector.js';
2
- import { DiscountContext, IDiscountAdapter } from './BaseDiscountAdapter.js';
3
- import { UnchainedCore } from '@unchainedshop/core';
4
- export type IDiscountDirector<DiscountConfiguration> = IBaseDirector<IDiscountAdapter<DiscountConfiguration>> & {
5
- actions: (discountContext: DiscountContext, unchainedAPI: UnchainedCore) => Promise<{
6
- resolveDiscountKeyFromStaticCode: (params: {
7
- code: string;
8
- }) => Promise<string | null>;
9
- findSystemDiscounts: () => Promise<Array<string>>;
10
- }>;
11
- };
12
- export declare const BaseDiscountDirector: <DiscountConfigurationType>(directorName: string) => IDiscountDirector<DiscountConfigurationType>;
13
- //# sourceMappingURL=BaseDiscountDirector.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseDiscountDirector.d.ts","sourceRoot":"","sources":["../../src/director/BaseDiscountDirector.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,MAAM,iBAAiB,CAAC,qBAAqB,IAAI,aAAa,CAClE,gBAAgB,CAAC,qBAAqB,CAAC,CACxC,GAAG;IACF,OAAO,EAAE,CACP,eAAe,EAAE,eAAe,EAChC,YAAY,EAAE,aAAa,KACxB,OAAO,CAAC;QACX,gCAAgC,EAAE,CAAC,MAAM,EAAE;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;QACvF,mBAAmB,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;KACnD,CAAC,CAAC;CACJ,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,yBAAyB,gBAC9C,MAAM,KACnB,iBAAiB,CAAC,yBAAyB,CAyD7C,CAAC"}
@@ -1,50 +0,0 @@
1
- import { log } from '@unchainedshop/logger';
2
- import { BaseDirector } from './BaseDirector.js';
3
- export const BaseDiscountDirector = (directorName) => {
4
- const baseDirector = BaseDirector(directorName, {
5
- adapterSortKey: 'orderIndex',
6
- });
7
- return {
8
- ...baseDirector,
9
- actions: async (discountContext, unchainedAPI) => {
10
- const context = { ...discountContext, ...unchainedAPI };
11
- return {
12
- resolveDiscountKeyFromStaticCode: async (options) => {
13
- if (!context.order)
14
- return null;
15
- log(`DiscountDirector -> Find user discount for static code ${options?.code}`);
16
- const discounts = await Promise.all(baseDirector
17
- .getAdapters()
18
- .filter((Adapter) => Adapter.isManualAdditionAllowed(options?.code))
19
- .map(async (Adapter) => {
20
- const adapter = await Adapter.actions({ context });
21
- return {
22
- key: Adapter.key,
23
- isValid: await adapter.isValidForCodeTriggering(options),
24
- };
25
- }));
26
- return discounts.find(({ isValid }) => isValid === true)?.key;
27
- },
28
- async findSystemDiscounts() {
29
- if (!context.order)
30
- return [];
31
- const discounts = await Promise.all(baseDirector.getAdapters().map(async (Adapter) => {
32
- const adapter = await Adapter.actions({ context });
33
- return {
34
- key: Adapter.key,
35
- isValid: await adapter.isValidForSystemTriggering(),
36
- };
37
- }));
38
- const validDiscounts = discounts
39
- .filter(({ isValid }) => isValid === true)
40
- .map(({ key }) => key);
41
- if (validDiscounts.length > 0) {
42
- log(`DiscountDirector -> Found ${validDiscounts.length} system discounts`);
43
- }
44
- return validDiscounts;
45
- },
46
- };
47
- },
48
- };
49
- };
50
- //# sourceMappingURL=BaseDiscountDirector.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseDiscountDirector.js","sourceRoot":"","sources":["../../src/director/BaseDiscountDirector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAiB,MAAM,mBAAmB,CAAC;AAgBhE,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAClC,YAAoB,EAC0B,EAAE;IAChD,MAAM,YAAY,GAAG,YAAY,CAA8C,YAAY,EAAE;QAC3F,cAAc,EAAE,YAAY;KAC7B,CAAC,CAAC;IAEH,OAAO;QACL,GAAG,YAAY;QAEf,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,EAAE;YAC/C,MAAM,OAAO,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,YAAY,EAAE,CAAC;YAExD,OAAO;gBACL,gCAAgC,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;oBAClD,IAAI,CAAC,OAAO,CAAC,KAAK;wBAAE,OAAO,IAAI,CAAC;oBAEhC,GAAG,CAAC,0DAA0D,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;oBAE/E,MAAM,SAAS,G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AC;oBAEzB,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;wBAC9B,GAAG,CAAC,6BAA6B,cAAc,CAAC,MAAM,mBAAmB,CAAC,CAAC;oBAC7E,CAAC;oBACD,OAAO,cAAc,CAAC;gBACxB,CAAC;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
@@ -1,46 +0,0 @@
1
- import { UnchainedCore } from '@unchainedshop/core';
2
- import { User } from '@unchainedshop/core-users';
3
- import { IPricingSheet, PricingCalculation } from './BasePricingSheet.js';
4
- import { IBaseAdapter } from './BaseAdapter.js';
5
- import { Discount } from './BasePricingDirector.js';
6
- type Order = {
7
- _id?: string;
8
- };
9
- type OrderDiscount = {
10
- _id?: string;
11
- };
12
- interface IDiscountableItem {
13
- _id?: string;
14
- orderId?: string;
15
- }
16
- export interface BasePricingAdapterContext extends UnchainedCore {
17
- order?: Order;
18
- user: User;
19
- discounts: Array<OrderDiscount>;
20
- }
21
- export type BasePricingContext = {
22
- order?: Order;
23
- user?: User;
24
- discounts?: Array<OrderDiscount>;
25
- } | {
26
- item: IDiscountableItem;
27
- };
28
- export interface IPricingAdapterActions<Calculation extends PricingCalculation, PricingAdapterContext extends BasePricingAdapterContext> {
29
- calculate: () => Promise<Array<Calculation>>;
30
- getCalculation: () => Array<Calculation>;
31
- getContext: () => PricingAdapterContext;
32
- }
33
- export type IPricingAdapter<PricingAdapterContext extends BasePricingAdapterContext, Calculation extends PricingCalculation, Sheet extends IPricingSheet<Calculation>, DiscountConfiguration = unknown> = IBaseAdapter & {
34
- orderIndex: number;
35
- isActivatedFor: (context: PricingAdapterContext) => boolean;
36
- actions: (params: {
37
- context: PricingAdapterContext;
38
- calculationSheet: Sheet;
39
- discounts: Array<Discount<DiscountConfiguration>>;
40
- }) => IPricingAdapterActions<Calculation, PricingAdapterContext> & {
41
- resultSheet: () => Sheet;
42
- };
43
- };
44
- export declare const BasePricingAdapter: <PricingAdapterContext extends BasePricingAdapterContext, Calculation extends PricingCalculation>() => IPricingAdapter<PricingAdapterContext, Calculation, IPricingSheet<Calculation>>;
45
- export {};
46
- //# sourceMappingURL=BasePricingAdapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BasePricingAdapter.d.ts","sourceRoot":"","sources":["../../src/director/BasePricingAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,MAAM,2BAA2B,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAEpD,KAAK,KAAK,GAAG;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAC9B,KAAK,aAAa,GAAG;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtC,UAAU,iBAAiB;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,yBAA0B,SAAQ,aAAa;IAC9D,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,IAAI,EAAE,IAAI,CAAC;IACX,SAAS,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC;CACjC;AAED,MAAM,MAAM,kBAAkB,GAC1B;IACE,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,SAAS,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC;CAClC,GACD;IACE,IAAI,EAAE,iBAAiB,CAAC;CACzB,CAAC;AAEN,MAAM,WAAW,sBAAsB,CACrC,WAAW,SAAS,kBAAkB,EACtC,qBAAqB,SAAS,yBAAyB;IAEvD,SAAS,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC;IAC7C,cAAc,EAAE,MAAM,KAAK,CAAC,WAAW,CAAC,CAAC;IACzC,UAAU,EAAE,MAAM,qBAAqB,CAAC;CACzC;AAED,MAAM,MAAM,eAAe,CACzB,qBAAqB,SAAS,yBAAyB,EACvD,WAAW,SAAS,kBAAkB,EACtC,KAAK,SAAS,aAAa,CAAC,WAAW,CAAC,EACxC,qBAAqB,GAAG,OAAO,IAC7B,YAAY,GAAG;IACjB,UAAU,EAAE,MAAM,CAAC;IAEnB,cAAc,EAAE,CAAC,OAAO,EAAE,qBAAqB,KAAK,OAAO,CAAC;IAE5D,OAAO,EAAE,CAAC,MAAM,EAAE;QAChB,OAAO,EAAE,qBAAqB,CAAC;QAC/B,gBAAgB,EAAE,KAAK,CAAC;QACxB,SAAS,EAAE,KAAK,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC,CAAC;KACnD,KAAK,sBAAsB,CAAC,WAAW,EAAE,qBAAqB,CAAC,GAAG;QAAE,WAAW,EAAE,MAAM,KAAK,CAAA;KAAE,CAAC;CACjG,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAC7B,qBAAqB,SAAS,yBAAyB,EACvD,WAAW,SAAS,kBAAkB,OACnC,eAAe,CAAC,qBAAqB,EAAE,WAAW,EAAE,aAAa,CAAC,WAAW,CAAC,CA4BjF,CAAC"}
@@ -1,25 +0,0 @@
1
- import { log, LogLevel } from '@unchainedshop/logger';
2
- export const BasePricingAdapter = () => ({
3
- key: '',
4
- label: '',
5
- version: '',
6
- orderIndex: 0,
7
- isActivatedFor: () => {
8
- return false;
9
- },
10
- actions: (params) => {
11
- const calculation = [];
12
- const actions = {
13
- calculate: async () => {
14
- return [];
15
- },
16
- getCalculation: () => calculation,
17
- getContext: () => params.context,
18
- };
19
- return actions;
20
- },
21
- log(message, { level = LogLevel.Debug, ...options } = {}) {
22
- return log(message, { level, ...options });
23
- },
24
- });
25
- //# sourceMappingURL=BasePricingAdapter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BasePricingAdapter.js","sourceRoot":"","sources":["../../src/director/BasePricingAdapter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAuDtD,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAGmD,EAAE,CAAC,CAAC;IACvF,GAAG,EAAE,EAAE;IACP,KAAK,EAAE,EAAE;IACT,OAAO,EAAE,EAAE;IACX,UAAU,EAAE,CAAC;IAEb,cAAc,EAAE,GAAG,EAAE;QACnB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE;QAClB,MAAM,WAAW,GAAG,EAAE,CAAC;QACvB,MAAM,OAAO,GAA+D;YAC1E,SAAS,EAAE,KAAK,IAAI,EAAE;gBACpB,OAAO,EAAE,CAAC;YACZ,CAAC;YACD,cAAc,EAAE,GAAG,EAAE,CAAC,WAAW;YACjC,UAAU,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO;SACjC,CAAC;QAEF,OAAO,OAEN,CAAC;IACJ,CAAC;IAED,GAAG,CAAC,OAAe,EAAE,EAAE,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,GAAG,OAAO,EAAE,GAAG,EAAE;QAC9D,OAAO,GAAG,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;IAC7C,CAAC;CACF,CAAC,CAAC"}
@@ -1,16 +0,0 @@
1
- import { IBaseDirector } from './BaseDirector.js';
2
- import { UnchainedCore } from '@unchainedshop/core';
3
- import { BasePricingAdapterContext, BasePricingContext, IPricingAdapter, IPricingAdapterActions } from './BasePricingAdapter.js';
4
- import { IPricingSheet, PricingCalculation } from './BasePricingSheet.js';
5
- export interface Discount<DiscountConfiguration> {
6
- discountId: string;
7
- configuration: DiscountConfiguration;
8
- }
9
- export type IPricingDirector<PricingContext extends BasePricingContext, Calculation extends PricingCalculation, PricingAdapterContext extends BasePricingAdapterContext, PricingAdapterSheet extends IPricingSheet<Calculation>, Adapter extends IPricingAdapter<PricingAdapterContext, Calculation, PricingAdapterSheet>> = IBaseDirector<Adapter> & {
10
- buildPricingContext: (context: PricingContext, unchainedAPI: UnchainedCore) => Promise<PricingAdapterContext>;
11
- actions: (pricingContext: PricingContext, unchainedAPI: UnchainedCore, buildPricingContext?: (pricingCtx: PricingContext, _unchainedAPI: UnchainedCore) => Promise<PricingAdapterContext>) => Promise<IPricingAdapterActions<Calculation, PricingAdapterContext> & {
12
- calculationSheet: () => PricingAdapterSheet;
13
- }>;
14
- };
15
- export declare const BasePricingDirector: <DirectorContext extends BasePricingContext, AdapterContext extends BasePricingAdapterContext, Calculation extends PricingCalculation, PricingAdapter extends IPricingAdapter<AdapterContext, Calculation, IPricingSheet<Calculation>>>(directorName: string) => IPricingDirector<DirectorContext, Calculation, AdapterContext, IPricingSheet<Calculation>, PricingAdapter>;
16
- //# sourceMappingURL=BasePricingDirector.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BasePricingDirector.d.ts","sourceRoot":"","sources":["../../src/director/BasePricingDirector.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,sBAAsB,EACvB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC1E,MAAM,WAAW,QAAQ,CAAC,qBAAqB;IAC7C,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,qBAAqB,CAAC;CACtC;AAED,MAAM,MAAM,gBAAgB,CAC1B,cAAc,SAAS,kBAAkB,EACzC,WAAW,SAAS,kBAAkB,EACtC,qBAAqB,SAAS,yBAAyB,EACvD,mBAAmB,SAAS,aAAa,CAAC,WAAW,CAAC,EACtD,OAAO,SAAS,eAAe,CAAC,qBAAqB,EAAE,WAAW,EAAE,mBAAmB,CAAC,IACtF,aAAa,CAAC,OAAO,CAAC,GAAG;IAC3B,mBAAmB,EAAE,CACnB,OAAO,EAAE,cAAc,EACvB,YAAY,EAAE,aAAa,KACxB,OAAO,CAAC,qBAAqB,CAAC,CAAC;IACpC,OAAO,EAAE,CACP,cAAc,EAAE,cAAc,EAC9B,YAAY,EAAE,aAAa,EAC3B,mBAAmB,CAAC,EAAE,CACpB,UAAU,EAAE,cAAc,EAC1B,aAAa,EAAE,aAAa,KACzB,OAAO,CAAC,qBAAqB,CAAC,KAChC,OAAO,CACV,sBAAsB,CAAC,WAAW,EAAE,qBAAqB,CAAC,GAAG;QAC3D,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;KAC7C,CACF,CAAC;CACH,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAC9B,eAAe,SAAS,kBAAkB,EAC1C,cAAc,SAAS,yBAAyB,EAChD,WAAW,SAAS,kBAAkB,EACtC,cAAc,SAAS,eAAe,CAAC,cAAc,EAAE,WAAW,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,gBAEjF,MAAM,KACnB,gBAAgB,CACjB,eAAe,EACf,WAAW,EACX,cAAc,EACd,aAAa,CAAC,WAAW,CAAC,EAC1B,cAAc,CAgFf,CAAC"}
@@ -1,61 +0,0 @@
1
- import { log, LogLevel } from '@unchainedshop/logger';
2
- import { BaseDirector } from './BaseDirector.js';
3
- export const BasePricingDirector = (directorName) => {
4
- const baseDirector = BaseDirector(directorName, {
5
- adapterSortKey: 'orderIndex',
6
- });
7
- const director = {
8
- ...baseDirector,
9
- buildPricingContext: async () => {
10
- return {};
11
- },
12
- actions: async (pricingContext, unchainedAPI, buildPricingContext) => {
13
- const context = await buildPricingContext(pricingContext, unchainedAPI);
14
- let calculation = [];
15
- const actions = {
16
- async calculate() {
17
- const Adapters = baseDirector.getAdapters({
18
- adapterFilter: (Adapter) => {
19
- return Adapter.isActivatedFor(context);
20
- },
21
- });
22
- calculation = await Adapters.reduce(async (previousPromise, Adapter) => {
23
- const resolvedCalculation = await previousPromise;
24
- if (!resolvedCalculation)
25
- return null;
26
- const discounts = await Promise.all(context.discounts.map(async (discount) => ({
27
- discountId: discount._id,
28
- configuration: await context.modules.orders.discounts.configurationForPricingAdapterKey(discount, Adapter.key, this.calculationSheet(), context),
29
- })));
30
- try {
31
- const adapter = Adapter.actions({
32
- context,
33
- calculationSheet: this.calculationSheet(),
34
- discounts: discounts.filter(({ configuration }) => configuration !== null),
35
- });
36
- const nextCalculationResult = await adapter.calculate();
37
- if (!nextCalculationResult)
38
- return null;
39
- calculation = resolvedCalculation.concat(nextCalculationResult);
40
- return calculation;
41
- }
42
- catch (error) {
43
- log(error, { level: LogLevel.Error });
44
- }
45
- return resolvedCalculation;
46
- }, Promise.resolve([]));
47
- return calculation;
48
- },
49
- getCalculation() {
50
- return calculation;
51
- },
52
- getContext() {
53
- return context;
54
- },
55
- };
56
- return actions;
57
- },
58
- };
59
- return director;
60
- };
61
- //# sourceMappingURL=BasePricingDirector.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BasePricingDirector.js","sourceRoot":"","sources":["../../src/director/BasePricingDirector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAiB,MAAM,mBAAmB,CAAC;AAuChE,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAMjC,YAAoB,EAOpB,EAAE;IACF,MAAM,YAAY,GAAG,YAAY,CAAiB,YAAY,EAAE;QAC9D,cAAc,EAAE,YAAY;KAC7B,CAAC,CAAC;IAEH,MAAM,QAAQ,GAMV;QACF,GAAG,YAAY;QACf,mBAAmB,EAAE,KAAK,IAAI,EAAE;YAC9B,OAAO,EAAoB,CAAC;QAC9B,CAAC;QACD,OAAO,EAAE,KAAK,EAAE,cAAc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AAM,OAAO,CAAC,SAAS,EAAE,CAAC;4BACxD,IAAI,CAAC,qBAAqB;gCAAE,OAAO,IAAI,CAAC;4BACxC,WAAW,GAAG,mBAAmB,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC;4BAChE,OAAO,WAAW,CAAC;wBACrB,CAAC;wBAAC,OAAO,KAAK,EAAE,CAAC;4BACf,GAAG,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;wBACxC,CAAC;wBACD,OAAO,mBAAmB,CAAC;oBAC7B,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;oBAExB,OAAO,WAAW,CAAC;gBACrB,CAAC;gBACD,cAAc;oBACZ,OAAO,WAAW,CAAC;gBACrB,CAAC;gBACD,UAAU;oBACR,OAAO,OAAO,CAAC;gBACjB,CAAC;aACF,CAAC;YAEF,OAAO,OAEN,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC"}
@@ -1,47 +0,0 @@
1
- export interface PricingCalculation {
2
- category: string;
3
- amount: number;
4
- baseCategory?: string;
5
- meta?: any;
6
- }
7
- export interface PricingDiscount {
8
- discountId: string;
9
- amount: number;
10
- currency: string;
11
- }
12
- export interface IPricingSheet<Calculation extends PricingCalculation> extends IBasePricingSheet<Calculation> {
13
- discountPrices: (discountId?: string) => Array<PricingDiscount>;
14
- addDiscount: (params: {
15
- amount: number;
16
- discountId: string;
17
- meta?: any;
18
- }) => void;
19
- }
20
- export interface IBasePricingSheet<Calculation extends PricingCalculation> {
21
- calculation: Array<Calculation>;
22
- currency?: string;
23
- quantity?: number;
24
- getRawPricingSheet: () => Array<Calculation>;
25
- filterBy: (filter?: Partial<Calculation>) => Array<Calculation>;
26
- isValid: () => boolean;
27
- gross: () => number;
28
- net: () => number;
29
- sum: (filter?: Partial<Calculation>) => number;
30
- total: (params?: {
31
- category?: string;
32
- discountId?: string;
33
- useNetPrice?: boolean;
34
- }) => {
35
- amount: number;
36
- currency: string;
37
- };
38
- taxSum: (filter?: Partial<Calculation>) => number;
39
- resetCalculation: (sheetToInvert: IBasePricingSheet<Calculation>) => Array<Calculation>;
40
- }
41
- export interface PricingSheetParams<Calculation extends PricingCalculation> {
42
- calculation?: Array<Calculation>;
43
- currency?: string;
44
- quantity?: number;
45
- }
46
- export declare const BasePricingSheet: <Calculation extends PricingCalculation>(params: PricingSheetParams<Calculation>) => IBasePricingSheet<Calculation>;
47
- //# sourceMappingURL=BasePricingSheet.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BasePricingSheet.d.ts","sourceRoot":"","sources":["../../src/director/BasePricingSheet.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AACD,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,aAAa,CAAC,WAAW,SAAS,kBAAkB,CACnE,SAAQ,iBAAiB,CAAC,WAAW,CAAC;IACtC,cAAc,EAAE,CAAC,UAAU,CAAC,EAAE,MAAM,KAAK,KAAK,CAAC,eAAe,CAAC,CAAC;IAChE,WAAW,EAAE,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,GAAG,CAAA;KAAE,KAAK,IAAI,CAAC;CACnF;AAED,MAAM,WAAW,iBAAiB,CAAC,WAAW,SAAS,kBAAkB;IACvE,WAAW,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,kBAAkB,EAAE,MAAM,KAAK,CAAC,WAAW,CAAC,CAAC;IAC7C,QAAQ,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,KAAK,KAAK,CAAC,WAAW,CAAC,CAAC;IAChE,OAAO,EAAE,MAAM,OAAO,CAAC;IAEvB,KAAK,EAAE,MAAM,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,MAAM,CAAC;IAClB,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,KAAK,MAAM,CAAC;IAC/C,KAAK,EAAE,CAAC,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,OAAO,CAAA;KAAE,KAAK;QACrF,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;IAEF,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,KAAK,MAAM,CAAC;IAElD,gBAAgB,EAAE,CAAC,aAAa,EAAE,iBAAiB,CAAC,WAAW,CAAC,KAAK,KAAK,CAAC,WAAW,CAAC,CAAC;CACzF;AAED,MAAM,WAAW,kBAAkB,CAAC,WAAW,SAAS,kBAAkB;IACxE,WAAW,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,eAAO,MAAM,gBAAgB,GAAI,WAAW,SAAS,kBAAkB,UAC7D,kBAAkB,CAAC,WAAW,CAAC,KACtC,iBAAiB,CAAC,WAAW,CAwE/B,CAAC"}
@@ -1,54 +0,0 @@
1
- export const BasePricingSheet = (params) => {
2
- const pricingSheet = {
3
- calculation: params.calculation || [],
4
- currency: params.currency,
5
- quantity: params.quantity,
6
- getRawPricingSheet() {
7
- return this.calculation;
8
- },
9
- isValid() {
10
- return this.calculation.length > 0;
11
- },
12
- sum(filter) {
13
- return this.filterBy(filter)
14
- .filter(Boolean)
15
- .reduce((sum, calculationRow) => sum + calculationRow.amount, 0);
16
- },
17
- taxSum() {
18
- return 0;
19
- },
20
- gross() {
21
- // Sum contains taxes, thus it's gross when just suming everything
22
- return this.sum();
23
- },
24
- net() {
25
- // Remove all taxes from gross to get net
26
- return this.sum() - this.taxSum();
27
- },
28
- total({ category, useNetPrice, discountId } = { useNetPrice: false }) {
29
- const amount = this.sum({ category, discountId });
30
- const taxAmountForCategory = this.taxSum({ baseCategory: category, discountId });
31
- // Sum does not contain taxes when filtering by category, it's net in that case and gross if there is no category
32
- const netAmount = !category ? amount - taxAmountForCategory : amount;
33
- return {
34
- amount: Math.round(useNetPrice ? netAmount : netAmount + taxAmountForCategory),
35
- currency: this.currency,
36
- };
37
- },
38
- filterBy(filter) {
39
- const filteredCalculation = Object.keys(filter || {}).reduce((oldCalculation, filterKey) => oldCalculation.filter((row) => !!row && (filter[filterKey] === undefined || row[filterKey] === filter[filterKey])), this.calculation);
40
- return filteredCalculation;
41
- },
42
- resetCalculation(calculationSheet) {
43
- calculationSheet.filterBy().forEach(({ amount, ...row }) => {
44
- this.calculation.push({
45
- ...row,
46
- amount: amount * -1,
47
- });
48
- });
49
- return this.calculation;
50
- },
51
- };
52
- return pricingSheet;
53
- };
54
- //# sourceMappingURL=BasePricingSheet.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BasePricingSheet.js","sourceRoot":"","sources":["../../src/director/BasePricingSheet.ts"],"names":[],"mappings":"AA8CA,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,MAAuC,EACP,EAAE;IAClC,MAAM,YAAY,GAAmC;QACnD,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,EAAE;QACrC,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,QAAQ,EAAE,MAAM,CAAC,QA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Q,CAAC,MAAM;YACb,MAAM,mBAAmB,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,MAAM,CAC1D,CAAC,cAAc,EAAE,SAAS,EAAE,EAAE,CAC5B,cAAc,CAAC,MAAM,CACnB,CAAC,GAAgB,EAAE,EAAE,CACnB,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,SAAS,IAAI,GAAG,CAAC,SAAS,CAAC,KAAK,MAAM,CAAC,SAAS,CAAC,CAAC,CACrF,EACH,IAAI,CAAC,WAAW,CACjB,CAAC;YAEF,OAAO,mBAAmB,CAAC;QAC7B,CAAC;QAED,gBAAgB,CAAC,gBAAgB;YAC/B,gBAAgB,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,GAAG,EAAe,EAAE,EAAE;gBACtE,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;oBACpB,GAAG,GAAG;oBACN,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC;iBACL,CAAC,CAAC;YACpB,CAAC,CAAC,CAAC;YACH,OAAO,IAAI,CAAC,WAAW,CAAC;QAC1B,CAAC;KACF,CAAC;IAEF,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC"}
@@ -1,2 +0,0 @@
1
- export default function randomValueHex(len: number): string;
2
- //# sourceMappingURL=random-value-hex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"random-value-hex.d.ts","sourceRoot":"","sources":["../src/random-value-hex.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAK1D"}
@@ -1,8 +0,0 @@
1
- import crypto from 'crypto';
2
- export default function randomValueHex(len) {
3
- return crypto
4
- .randomBytes(Math.ceil(len / 2))
5
- .toString('hex') // convert to hexadecimal format
6
- .slice(0, len); // return required number of characters
7
- }
8
- //# sourceMappingURL=random-value-hex.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"random-value-hex.js","sourceRoot":"","sources":["../src/random-value-hex.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,GAAW;IAChD,OAAO,MAAM;SACV,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;SAC/B,QAAQ,CAAC,KAAK,CAAC,CAAC,gCAAgC;SAChD,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,uCAAuC;AAC3D,CAAC"}
@@ -1,89 +0,0 @@
1
- import { log, LogLevel } from '@unchainedshop/logger';
2
- import { UnchainedCore } from '@unchainedshop/core';
3
- import { IBaseAdapter } from './BaseAdapter.js';
4
- import { IPricingSheet, PricingCalculation } from './BasePricingSheet.js';
5
-
6
- export interface DiscountContext {
7
- code?: string;
8
- orderDiscount?: { _id?: string; orderId: string };
9
- order?: { _id?: string; userId: string };
10
- }
11
-
12
- export type IDiscountAdapter<DiscountConfiguration> = IBaseAdapter & {
13
- orderIndex: number;
14
-
15
- isManualAdditionAllowed: (code: string) => Promise<boolean>;
16
- isManualRemovalAllowed: () => Promise<boolean>;
17
-
18
- actions: (params: {
19
- context: DiscountContext & UnchainedCore;
20
- }) => Promise<DiscountAdapterActions<DiscountConfiguration>>;
21
- };
22
-
23
- export interface DiscountAdapterActions<DiscountConfiguration> {
24
- isValidForSystemTriggering: () => Promise<boolean>;
25
- isValidForCodeTriggering: (params: { code: string }) => Promise<boolean>;
26
-
27
- discountForPricingAdapterKey: (params: {
28
- pricingAdapterKey: string;
29
- calculationSheet: IPricingSheet<PricingCalculation>;
30
- }) => DiscountConfiguration;
31
-
32
- reserve: (params: { code: string }) => Promise<any>;
33
- release: () => Promise<void>;
34
- }
35
-
36
- export const BaseDiscountAdapter: Omit<IDiscountAdapter<unknown>, 'key' | 'label' | 'version'> = {
37
- orderIndex: 0,
38
-
39
- isManualAdditionAllowed: async () => {
40
- return false;
41
- },
42
-
43
- // return true if a discount is allowed to get removed manually by a user
44
- isManualRemovalAllowed: async () => {
45
- return false;
46
- },
47
-
48
- actions: async () => ({
49
- // return true if a discount is valid to be part of the order
50
- // without input of a user. that could be a time based global discount
51
- // like a 10% discount day
52
- // if you return false, this discount will
53
- // get removed from the order before any price calculation
54
- // takes place.
55
- // eslint-disable-next-line
56
- isValidForSystemTriggering: async () => {
57
- return false;
58
- },
59
-
60
- // return an arbitrary JSON serializable object with reservation data
61
- // this method is called when a discount is added through a manual code and let's
62
- // you manually deduct expendable discounts (coupon balances for ex.) before checkout
63
- reserve: async () => {
64
- return {};
65
- },
66
-
67
- // return void, allows you to free up any reservations in backend systems
68
- release: async () => {
69
- return null;
70
- },
71
-
72
- // return true if a discount is valid to be part of the order.
73
- // if you return false, this discount will
74
- // get removed from the order before any price calculation
75
- // takes place.
76
- isValidForCodeTriggering: async () => {
77
- return false;
78
- },
79
-
80
- // returns the appropriate discount context for a calculation adapter
81
- discountForPricingAdapterKey() {
82
- return null;
83
- },
84
- }),
85
-
86
- log(message: string, { level = LogLevel.Debug, ...options } = {}) {
87
- return log(message, { level, ...options });
88
- },
89
- };
@@ -1,77 +0,0 @@
1
- import { log } from '@unchainedshop/logger';
2
- import { BaseDirector, IBaseDirector } from './BaseDirector.js';
3
- import { DiscountContext, IDiscountAdapter } from './BaseDiscountAdapter.js';
4
- import { UnchainedCore } from '@unchainedshop/core';
5
-
6
- export type IDiscountDirector<DiscountConfiguration> = IBaseDirector<
7
- IDiscountAdapter<DiscountConfiguration>
8
- > & {
9
- actions: (
10
- discountContext: DiscountContext,
11
- unchainedAPI: UnchainedCore,
12
- ) => Promise<{
13
- resolveDiscountKeyFromStaticCode: (params: { code: string }) => Promise<string | null>;
14
- findSystemDiscounts: () => Promise<Array<string>>;
15
- }>;
16
- };
17
-
18
- export const BaseDiscountDirector = <DiscountConfigurationType>(
19
- directorName: string,
20
- ): IDiscountDirector<DiscountConfigurationType> => {
21
- const baseDirector = BaseDirector<IDiscountAdapter<DiscountConfigurationType>>(directorName, {
22
- adapterSortKey: 'orderIndex',
23
- });
24
-
25
- return {
26
- ...baseDirector,
27
-
28
- actions: async (discountContext, unchainedAPI) => {
29
- const context = { ...discountContext, ...unchainedAPI };
30
-
31
- return {
32
- resolveDiscountKeyFromStaticCode: async (options) => {
33
- if (!context.order) return null;
34
-
35
- log(`DiscountDirector -> Find user discount for static code ${options?.code}`);
36
-
37
- const discounts = await Promise.all(
38
- baseDirector
39
- .getAdapters()
40
- .filter((Adapter) => Adapter.isManualAdditionAllowed(options?.code))
41
- .map(async (Adapter) => {
42
- const adapter = await Adapter.actions({ context });
43
- return {
44
- key: Adapter.key,
45
- isValid: await adapter.isValidForCodeTriggering(options),
46
- };
47
- }),
48
- );
49
-
50
- return discounts.find(({ isValid }) => isValid === true)?.key;
51
- },
52
-
53
- async findSystemDiscounts() {
54
- if (!context.order) return [];
55
- const discounts = await Promise.all(
56
- baseDirector.getAdapters().map(async (Adapter) => {
57
- const adapter = await Adapter.actions({ context });
58
- return {
59
- key: Adapter.key,
60
- isValid: await adapter.isValidForSystemTriggering(),
61
- };
62
- }),
63
- );
64
-
65
- const validDiscounts = discounts
66
- .filter(({ isValid }) => isValid === true)
67
- .map(({ key }) => key);
68
-
69
- if (validDiscounts.length > 0) {
70
- log(`DiscountDirector -> Found ${validDiscounts.length} system discounts`);
71
- }
72
- return validDiscounts;
73
- },
74
- };
75
- },
76
- };
77
- };