@rxdrag/website-lib-core 0.1.3 → 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.3",
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,47 +11,62 @@ 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";
14
16
 
15
17
  const PAGE_SIZE = 500;
16
18
 
17
19
  export async function queryAllProducts(envVariables: EnvVariables, langAbbr: string) {
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
+ ) {
18
44
  const items: TProduct[] = [];
19
45
  let offset = 0;
46
+ let lastResult: ListResult<TProduct> | undefined;
20
47
 
21
48
  while (true) {
22
- const queryOptions = new ProductQueryOptions([ProductFields.id, ProductFields.slug], {
23
- offset,
24
- limit: PAGE_SIZE,
25
- where: {
26
- [ProductFields.status]: {
27
- _eq: PublishableStatus.published,
49
+ const result = category
50
+ ? await queryProducts(
51
+ {
52
+ category,
53
+ page: offset / PAGE_SIZE + 1,
54
+ pageSize: PAGE_SIZE,
28
55
  },
29
- _or: [
30
- { [ProductFields.isDeleted]: { _eq: false } },
31
- { [ProductFields.isDeleted]: { _isNull: true } },
32
- ],
33
- lang: {
34
- abbr: {
35
- _eq: langAbbr,
36
- },
37
- },
38
- },
39
- orderBy: [{ [ProductFields.seqValue]: "asc" }],
40
- });
41
-
42
- const result = await queryEntityList<
43
- Product,
44
- ProductBoolExp,
45
- ProductOrderBy,
46
- ProductDistinctExp
47
- >(queryOptions, envVariables) as ListResult<TProduct> | undefined;
56
+ undefined,
57
+ envVariables,
58
+ ["slug"],
59
+ langAbbr,
60
+ )
61
+ : await queryProductSlugPage(envVariables, langAbbr, offset);
48
62
 
63
+ lastResult = result;
49
64
  const pageItems = result?.items || [];
50
65
  items.push(...pageItems);
51
66
 
52
67
  if (pageItems.length < PAGE_SIZE) {
53
68
  return {
54
- ...result,
69
+ ...lastResult,
55
70
  items,
56
71
  } as ListResult<TProduct>;
57
72
  }
@@ -59,3 +74,56 @@ export async function queryAllProducts(envVariables: EnvVariables, langAbbr: str
59
74
  offset += PAGE_SIZE;
60
75
  }
61
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,
97
+ },
98
+ },
99
+ },
100
+ orderBy: [
101
+ { [ProductFields.seqValue]: "asc" },
102
+ { [ProductFields.updatedAt]: "desc" },
103
+ ],
104
+ });
105
+
106
+ return await queryEntityList<
107
+ Product,
108
+ ProductBoolExp,
109
+ ProductOrderBy,
110
+ ProductDistinctExp
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
+ ]);
129
+ }