@rxdrag/website-lib-core 0.1.2 → 0.1.4

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@rxdrag/website-lib-core",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.ts"
@@ -23,9 +23,9 @@
23
23
  "@types/react-dom": "^19.1.0",
24
24
  "eslint": "^9.39.2",
25
25
  "typescript": "^5",
26
- "@rxdrag/tiptap-preview": "0.1.0",
26
+ "@rxdrag/tsconfig": "0.3.0",
27
27
  "@rxdrag/eslint-config-custom": "0.3.0",
28
- "@rxdrag/tsconfig": "0.3.0"
28
+ "@rxdrag/tiptap-preview": "0.1.0"
29
29
  },
30
30
  "dependencies": {
31
31
  "@iconify/utils": "^3.0.2",
@@ -11,30 +11,119 @@ import { EnvVariables } from "../types";
11
11
  import { ListResult } from "@rxdrag/entify-lib";
12
12
  import { TProduct } from "../view-model";
13
13
  import { queryEntityList } from "./queryEntityList";
14
+ import { queryProductCategories } from "./queryProductCategories";
15
+ import { queryProducts } from "./queryProducts";
16
+
17
+ const PAGE_SIZE = 500;
14
18
 
15
19
  export async function queryAllProducts(envVariables: EnvVariables, langAbbr: string) {
16
- const queryOptions = new ProductQueryOptions(
17
- [ProductFields.id, ProductFields.slug],
18
- {
19
- where: {
20
- [ProductFields.status]: {
21
- _eq: PublishableStatus.published,
20
+ const products = new Map<string, TProduct>();
21
+ const result = await queryPagedProducts(envVariables, langAbbr);
22
+ addProducts(products, result?.items);
23
+
24
+ const categories = flattenCategories(await queryProductCategories(envVariables, langAbbr));
25
+ for (const category of categories) {
26
+ if (!category.slug) continue;
27
+
28
+ const categoryResult = await queryPagedProducts(envVariables, langAbbr, String(category.slug));
29
+ addProducts(products, categoryResult?.items);
30
+ }
31
+
32
+ return {
33
+ ...result,
34
+ total: products.size,
35
+ items: Array.from(products.values()),
36
+ } as ListResult<TProduct>;
37
+ }
38
+
39
+ async function queryPagedProducts(
40
+ envVariables: EnvVariables,
41
+ langAbbr: string,
42
+ category?: string,
43
+ ) {
44
+ const items: TProduct[] = [];
45
+ let offset = 0;
46
+ let lastResult: ListResult<TProduct> | undefined;
47
+
48
+ while (true) {
49
+ const result = category
50
+ ? await queryProducts(
51
+ {
52
+ category,
53
+ page: offset / PAGE_SIZE + 1,
54
+ pageSize: PAGE_SIZE,
22
55
  },
23
- lang: {
24
- abbr: {
25
- _eq: langAbbr,
26
- },
56
+ undefined,
57
+ envVariables,
58
+ ["slug"],
59
+ langAbbr,
60
+ )
61
+ : await queryProductSlugPage(envVariables, langAbbr, offset);
62
+
63
+ lastResult = result;
64
+ const pageItems = result?.items || [];
65
+ items.push(...pageItems);
66
+
67
+ if (pageItems.length < PAGE_SIZE) {
68
+ return {
69
+ ...lastResult,
70
+ items,
71
+ } as ListResult<TProduct>;
72
+ }
73
+
74
+ offset += PAGE_SIZE;
75
+ }
76
+ }
77
+
78
+ async function queryProductSlugPage(
79
+ envVariables: EnvVariables,
80
+ langAbbr: string,
81
+ offset: number,
82
+ ) {
83
+ const queryOptions = new ProductQueryOptions([ProductFields.id, ProductFields.slug], {
84
+ offset,
85
+ limit: PAGE_SIZE,
86
+ where: {
87
+ [ProductFields.status]: {
88
+ _eq: PublishableStatus.published,
89
+ },
90
+ _or: [
91
+ { [ProductFields.isDeleted]: { _eq: false } },
92
+ { [ProductFields.isDeleted]: { _isNull: true } },
93
+ ],
94
+ lang: {
95
+ abbr: {
96
+ _eq: langAbbr,
27
97
  },
28
98
  },
29
- orderBy: [{ [ProductFields.seqValue]: "asc" }],
30
- }
31
- );
99
+ },
100
+ orderBy: [
101
+ { [ProductFields.seqValue]: "asc" },
102
+ { [ProductFields.updatedAt]: "desc" },
103
+ ],
104
+ });
32
105
 
33
- const result = await queryEntityList<
106
+ return await queryEntityList<
34
107
  Product,
35
108
  ProductBoolExp,
36
109
  ProductOrderBy,
37
110
  ProductDistinctExp
38
- >(queryOptions, envVariables);
39
- return result as ListResult<TProduct> | undefined;
111
+ >(queryOptions, envVariables) as ListResult<TProduct> | undefined;
112
+ }
113
+
114
+ function addProducts(products: Map<string, TProduct>, items?: TProduct[]) {
115
+ items?.forEach((product) => {
116
+ if (product.slug) {
117
+ products.set(product.slug, product);
118
+ }
119
+ });
120
+ }
121
+
122
+ function flattenCategories(categories: unknown): Array<{ slug?: string | null; children?: unknown }> {
123
+ if (!Array.isArray(categories)) return [];
124
+
125
+ return categories.flatMap((category) => [
126
+ category,
127
+ ...flattenCategories(category?.children),
128
+ ]);
40
129
  }