@reactionary/provider-fake 0.3.15 → 0.3.17

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 CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@reactionary/provider-fake",
3
- "version": "0.3.15",
3
+ "version": "0.3.17",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.3.15",
7
+ "@reactionary/core": "0.3.17",
8
8
  "zod": "4.1.9",
9
9
  "@faker-js/faker": "^9.8.0"
10
10
  },
@@ -19,106 +19,70 @@ import {
19
19
  success
20
20
  } from "@reactionary/core";
21
21
  import { base, en, Faker } from "@faker-js/faker";
22
+ import { calcSeed } from "../utilities/seed.js";
22
23
  class FakePriceProvider extends PriceProvider {
23
24
  constructor(config, cache, context) {
24
25
  super(cache, context);
25
26
  this.config = config;
27
+ this.faker = new Faker({
28
+ locale: [en, base]
29
+ });
26
30
  }
27
- async getListPrice(payload) {
28
- if (payload.variant.sku === "unknown-sku") {
29
- return success(this.createEmptyPriceResult(payload.variant.sku));
31
+ createPrice(variantSku, mode) {
32
+ const seed = calcSeed(variantSku);
33
+ this.faker.seed(seed);
34
+ let price = this.faker.number.int({ min: 300, max: 1e5 }) / 100;
35
+ let onSale = false;
36
+ if (mode === "customer") {
37
+ onSale = this.faker.datatype.boolean({ probability: 0.1 });
38
+ if (onSale) {
39
+ price = price * this.faker.number.float({ min: 0.5, max: 0.9 });
40
+ }
30
41
  }
31
- let hash = 0;
32
- const skuString = payload.variant.sku;
33
- for (let i = 0; i < skuString.length; i++) {
34
- hash = (hash << 5) - hash + skuString.charCodeAt(i);
35
- hash = hash & hash;
42
+ const tiers = [];
43
+ if (variantSku.includes("with-tiers")) {
44
+ tiers.push({
45
+ minimumQuantity: this.faker.number.int({ min: 2, max: 5 }),
46
+ price: {
47
+ value: price * 0.8,
48
+ // 20% discount for tier 1
49
+ currency: this.context.languageContext.currencyCode
50
+ }
51
+ });
52
+ tiers.push({
53
+ minimumQuantity: this.faker.number.int({ min: 6, max: 10 }),
54
+ price: {
55
+ value: price * 0.6,
56
+ // 40% discount for tier 2
57
+ currency: this.context.languageContext.currencyCode
58
+ }
59
+ });
36
60
  }
37
- const generator = new Faker({
38
- seed: hash || 42,
39
- locale: [en, base]
40
- });
41
- const model = {
61
+ return {
42
62
  identifier: {
43
- variant: payload.variant
63
+ variant: {
64
+ sku: variantSku
65
+ }
44
66
  },
45
67
  unitPrice: {
46
- value: generator.number.int({ min: 300, max: 1e5 }) / 100,
68
+ value: price,
47
69
  currency: this.context.languageContext.currencyCode
48
- }
70
+ },
71
+ onSale,
72
+ tieredPrices: tiers
49
73
  };
50
- if (skuString.includes("with-tiers")) {
51
- const unitPrice = model.unitPrice?.value || 0;
52
- const tier1Price = unitPrice * 0.8;
53
- const tier2Price = tier1Price * 0.8;
54
- model.tieredPrices = [
55
- {
56
- minimumQuantity: generator.number.int({ min: 2, max: 5 }),
57
- price: {
58
- value: tier1Price,
59
- currency: this.context.languageContext.currencyCode
60
- }
61
- },
62
- {
63
- minimumQuantity: generator.number.int({ min: 6, max: 10 }),
64
- price: {
65
- value: tier2Price,
66
- currency: this.context.languageContext.currencyCode
67
- }
68
- }
69
- ];
70
- } else {
71
- model.tieredPrices = [];
74
+ }
75
+ async getListPrice(payload) {
76
+ if (payload.variant.sku === "unknown-sku") {
77
+ return success(this.createEmptyPriceResult(payload.variant.sku));
72
78
  }
73
- return success(model);
79
+ return success(this.createPrice(payload.variant.sku, "list"));
74
80
  }
75
81
  async getCustomerPrice(payload) {
76
82
  if (payload.variant.sku === "unknown-sku") {
77
83
  return success(this.createEmptyPriceResult(payload.variant.sku));
78
84
  }
79
- let hash = 0;
80
- const skuString = payload.variant.sku;
81
- for (let i = 0; i < skuString.length; i++) {
82
- hash = (hash << 5) - hash + skuString.charCodeAt(i);
83
- hash = hash & hash;
84
- }
85
- const generator = new Faker({
86
- seed: hash || 42,
87
- locale: [en, base]
88
- });
89
- const model = {
90
- identifier: {
91
- variant: payload.variant
92
- },
93
- unitPrice: {
94
- value: generator.number.int({ min: 300, max: 1e5 }) / 100,
95
- currency: this.context.languageContext.currencyCode
96
- }
97
- };
98
- if (skuString.includes("with-tiers")) {
99
- const unitPrice = model.unitPrice?.value || 0;
100
- const tier1Price = unitPrice * 0.8;
101
- const tier2Price = tier1Price * 0.8;
102
- model.tieredPrices = [
103
- {
104
- minimumQuantity: generator.number.int({ min: 2, max: 5 }),
105
- price: {
106
- value: tier1Price,
107
- currency: this.context.languageContext.currencyCode
108
- }
109
- },
110
- {
111
- minimumQuantity: generator.number.int({ min: 6, max: 10 }),
112
- price: {
113
- value: tier2Price,
114
- currency: this.context.languageContext.currencyCode
115
- }
116
- }
117
- ];
118
- } else {
119
- model.tieredPrices = [];
120
- }
121
- return success(model);
85
+ return success(this.createPrice(payload.variant.sku, "customer"));
122
86
  }
123
87
  }
124
88
  __decorateClass([
@@ -1,8 +1,11 @@
1
1
  import { type RequestContext, type Cache, PriceProvider, type CustomerPriceQuery, type ListPriceQuery, type Price, type Result } from '@reactionary/core';
2
2
  import type { FakeConfiguration } from '../schema/configuration.schema.js';
3
+ import { Faker } from '@faker-js/faker';
3
4
  export declare class FakePriceProvider extends PriceProvider {
4
5
  protected config: FakeConfiguration;
6
+ protected faker: Faker;
5
7
  constructor(config: FakeConfiguration, cache: Cache, context: RequestContext);
8
+ protected createPrice(variantSku: string, mode: 'list' | 'customer'): Price;
6
9
  getListPrice(payload: ListPriceQuery): Promise<Result<Price>>;
7
10
  getCustomerPrice(payload: CustomerPriceQuery): Promise<Result<Price>>;
8
11
  }