Compare commits
12 Commits
05911f4230
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| fa3361ebd7 | |||
| d07c4df52d | |||
| 04960379e8 | |||
| 6121b9c9bf | |||
| c91a510c56 | |||
|
|
358e99c1ba | ||
|
|
60cd4cc1d6 | ||
|
|
aa4262c45a | ||
|
|
9cfcd12351 | ||
|
|
e6e39b4f80 | ||
|
|
0d7e7b7f87 | ||
|
|
dec13587f0 |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -63,7 +63,12 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|||||||
# Claude Code personal settings (.claude/settings.json stays shareable)
|
# Claude Code personal settings (.claude/settings.json stays shareable)
|
||||||
/.claude/settings.local.json
|
/.claude/settings.local.json
|
||||||
|
|
||||||
# Postman (skills fetched by `postman init`, personal workspace binding)
|
# Postman: collections, specs and flows are versioned with the code.
|
||||||
/postman/
|
# Ignored: skills and SDKs (generated by the CLI), globals and environments (may hold personal values or secrets),
|
||||||
|
# and .postman/ (binding to each developer's own workspace).
|
||||||
|
/postman/*
|
||||||
|
!/postman/collections/
|
||||||
|
!/postman/specs/
|
||||||
|
!/postman/flows/
|
||||||
/.postman/
|
/.postman/
|
||||||
/AGENTS.md
|
/AGENTS.md
|
||||||
|
|||||||
25
CLAUDE.md
25
CLAUDE.md
@@ -23,6 +23,8 @@ npm run test:e2e # e2e tests (test/jest-e2e.json)
|
|||||||
|
|
||||||
Jest runs in CommonJS: its config (in `package.json` and `test/jest-e2e.json`) transpiles the ESM-only `@nestjs/config` (`transformIgnorePatterns`) and maps `./x.js` imports to `./x` (`moduleNameMapper`) for the generated Prisma client. Keep both configs in sync. Unit tests build services directly with mocked repositories; the e2e test boots `AppModule` with `PrismaService` and `HiboutikService` overridden, so it needs neither a database nor Hiboutik.
|
Jest runs in CommonJS: its config (in `package.json` and `test/jest-e2e.json`) transpiles the ESM-only `@nestjs/config` (`transformIgnorePatterns`) and maps `./x.js` imports to `./x` (`moduleNameMapper`) for the generated Prisma client. Keep both configs in sync. Unit tests build services directly with mocked repositories; the e2e test boots `AppModule` with `PrismaService` and `HiboutikService` overridden, so it needs neither a database nor Hiboutik.
|
||||||
|
|
||||||
|
TypeScript runs in `strict` mode and ESLint treats floating promises as errors: a deliberate fire-and-forget call is written `void promise`.
|
||||||
|
|
||||||
Prisma: the config file is `prisma7.config.ts` (non-default name), so pass it explicitly:
|
Prisma: the config file is `prisma7.config.ts` (non-default name), so pass it explicitly:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -34,11 +36,11 @@ The client is generated into `prisma/generated/` (generator `prisma-client`, not
|
|||||||
|
|
||||||
## Environment
|
## Environment
|
||||||
|
|
||||||
Read from `.env` (`@nestjs/config`, global): `DATABASE_URL` (used by Prisma and `PrismaService`), `HIBOUTIK_BASE_URL`, `HIBOUTIK_USERNAME`, `HIBOUTIK_PASSWORD` (HTTP Basic auth). HTTP calls to Hiboutik time out after 30 s (`HttpModule.register` in `AppModule`).
|
Read from `.env` (`@nestjs/config`, global): `DATABASE_URL` (used by Prisma and `PrismaService`), `HIBOUTIK_BASE_URL`, `HIBOUTIK_USERNAME`, `HIBOUTIK_PASSWORD` (HTTP Basic auth). HTTP calls to Hiboutik (`HttpModule.register` in `AppModule`) time out after 30 s, never follow redirects (the Basic auth credentials must not leave) and are capped at 50 MB. `HiboutikClient` retries a 429 three times with backoff (2/4/8 s, or the `Retry-After` delay capped at 60 s), then throws `HiboutikRateLimitError`: per-item `catch` blocks must rethrow it (`rethrowIfRateLimited`) and `syncAll` stops on it. Hiboutik allows 10,000 calls per month and 30 concurrent calls per shop.
|
||||||
|
|
||||||
The environment is validated at startup (`src/config/env.validation.ts`, wired in `ConfigModule.forRoot`): a missing or malformed required variable stops the app, and `HIBOUTIK_BASE_URL` must be HTTPS (Basic auth). Optional variables: `PORT`, `CORS_ORIGINS` (comma-separated; CORS stays disabled when unset) and `TRUST_PROXY` (number of reverse proxies, so rate limiting sees the real client IP). Any new variable goes in `EnvironmentVariables`; the e2e test gets dummy values from `test/setup-env.ts`.
|
The environment is validated at startup (`src/config/env.validation.ts`, wired in `ConfigModule.forRoot`): a missing or malformed required variable stops the app, and `HIBOUTIK_BASE_URL` must be HTTPS (Basic auth). Optional variables: `NODE_ENV` (`development`/`production`/`test`; in `production`, `DATABASE_URL` must set `sslmode=require`, `verify-ca` or `verify-full`), `PORT`, `CORS_ORIGINS` (comma-separated; CORS stays disabled when unset), `TRUST_PROXY` (number of reverse proxies, so rate limiting sees the real client IP) and `SYNC_SHUTDOWN_TIMEOUT` (seconds a stopping instance waits for its running sync, default 25; keep it below the orchestrator's grace period). Any new variable goes in `EnvironmentVariables`; the e2e test gets dummy values from `test/setup-env.ts`.
|
||||||
|
|
||||||
Security defaults in `main.ts`/`AppModule`: Helmet, global `ValidationPipe`, `ThrottlerGuard` on every route (60 req/min/IP; `@Throttle` for stricter limits, e.g. `POST /sync` at 5/min), `AllExceptionsFilter` (unexpected errors are logged with message + stack and answered with a generic 500; deliberate `HttpException`s and 4xx HTTP-layer errors pass through), HTTP header/request timeouts (15 s / 30 s), shutdown hooks. Long work must run in the background (like `POST /sync`), never inside a request.
|
Security defaults in `main.ts`/`AppModule`: Helmet, global `ValidationPipe`, `ThrottlerGuard` on every route (60 req/min/IP; `@Throttle` for stricter limits, e.g. `POST /sync` at 5/min; counters shared by every instance in the `throttler_records` table through `ThrottlerDatabaseStorage`, one atomic upsert per request on the database clock, failing open if the database is unreachable), `AllExceptionsFilter` (unexpected errors are logged with message + stack and answered with a generic 500; deliberate `HttpException`s and 4xx HTTP-layer errors pass through), HTTP header/request timeouts (15 s / 30 s), shutdown hooks. The app is meant to run as several instances. On shutdown, `HiboutikService.beforeApplicationShutdown` refuses new syncs (503) and waits for the running one up to `SYNC_SHUTDOWN_TIMEOUT`; Prisma disconnects last, in `onApplicationShutdown` (never move it back to `onModuleDestroy`, which runs first). Long work must run in the background (like `POST /sync`), never inside a request.
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
@@ -46,21 +48,24 @@ Everything is registered as providers in a single `AppModule` (`src/app.module.t
|
|||||||
|
|
||||||
Sync pipeline, one entity per layer:
|
Sync pipeline, one entity per layer:
|
||||||
|
|
||||||
1. **`src/hiboutik/*.hiboutik.repository.ts`** – fetch raw data from Hiboutik through `HiboutikClient` (`get`, and `getAllPages`, which walks the `p` page param until a page smaller than 250 results, Hiboutik's page size). `/suppliers` and `/stock_available/all_wh` are not paginated.
|
1. **`src/hiboutik/*.hiboutik.repository.ts`** – fetch data from Hiboutik through `HiboutikClient`: `getList`, `getAllPages` (walks the `p` page param until a page smaller than 250 results, Hiboutik's page size) and `getOne` (single resource: `null` on a 404 or an empty 2xx answer, throws on anything else). `/suppliers` and `/stock_available/all_wh` are not paginated.
|
||||||
2. **`src/models/hiboutikDTO/`** – snake_case types mirroring Hiboutik API payloads.
|
2. **`src/models/hiboutikDTO/`** – zod schemas of the Hiboutik payloads (snake_case, only the fields used), the DTO types being inferred from them. `HiboutikClient` validates every answer: shared field types in `hiboutik.schema.ts` turn numeric strings into numbers and empty values into `0`/`""`/`[]`; an invalid id, relation or price rejects the whole answer (so nothing is deleted). Validation errors name fields, never values.
|
||||||
3. **`src/models/converter/`** – classes extending abstract `Converter<Model, Dto>`; `fromHiboutik` maps a DTO to a Prisma `*CreateInput`. Relations use `connect: { hiboutikId }`.
|
3. **`src/models/converter/`** – classes extending abstract `Converter<Model, Dto>`; `fromHiboutik` maps a DTO to a Prisma `*CreateInput`. Relations use `connect: { hiboutikId }`.
|
||||||
4. **`src/database/*.database.repository.ts`** – Prisma access, mainly `upsert` keyed on the unique `hiboutikId`.
|
4. **`src/database/*.database.repository.ts`** – Prisma access, mainly `upsert` keyed on the unique `hiboutikId`.
|
||||||
5. **`src/services/hiboutik/*.hiboutik.service.ts`** – per-entity sync orchestration; `HiboutikService.syncAll` runs them in dependency order: brands → categories → suppliers → products → customers (products `connect` to the first three by `hiboutikId`, so order matters).
|
5. **`src/services/hiboutik/*.hiboutik.service.ts`** – per-entity sync orchestration; `HiboutikService.syncAll` runs them in dependency order: brands → categories → suppliers → products → customers (products `connect` to the first three by `hiboutikId`, so order matters).
|
||||||
|
|
||||||
Domain notes:
|
Domain notes:
|
||||||
- Synced models carry a unique `hiboutikId` (join key with Hiboutik), except `Order` (nullable `hiboutikSaleId`), `ProductVersion` (unique `[productId, hiboutikSizeId]`) and `OrderLine`; the internal `id` is a UUID. Empty Hiboutik barcodes are stored as `null`. `ProductVersion.barcode` is indexed but **not unique** (Hiboutik may reuse a barcode).
|
- Synced models carry a unique `hiboutikId` (join key with Hiboutik), except `Order` (nullable `hiboutikSaleId`), `ProductVersion` (unique `[productId, hiboutikSizeId]`) and `OrderLine`; the internal `id` is a UUID. Empty Hiboutik barcodes are stored as `null`. `ProductVersion.barcode` is indexed but **not unique** (Hiboutik may reuse a barcode).
|
||||||
- Hiboutik is the source of truth: brands, categories, suppliers, products and customers missing from it are deleted. Hiboutik is trusted: a 2xx answer is the truth, even an empty list (everything is then deleted). Deletion only requires that the call did not fail: `HiboutikClient` throws on HTTP errors (4xx/5xx) and `getList`/`getAllPages` also throw on a 2xx body that is not a list (a 2xx without body is an empty list), so a failed step deletes nothing. List endpoints must go through `getList`/`getAllPages`, never a bare `get`. Products are deleted before the upserts (so a recreated product can reuse its barcode); their versions go by cascade, and versions missing from a product are deleted before its versions are upserted. Customer addresses go by cascade with their customer; other relations (product brand/category/supplier, sub-categories, order customer) are set to null and orders are kept.
|
- Hiboutik is the source of truth: brands, categories, suppliers, products and customers missing from it are deleted. Hiboutik is trusted: a 2xx answer is the truth, even an empty list (everything is then deleted). Deletion only requires that the call did not fail: `HiboutikClient` throws on HTTP errors (4xx/5xx) and `getList`/`getAllPages` also throw on a 2xx body that is not a list (a 2xx without body is an empty list), so a failed step deletes nothing. Every call goes through `getList`/`getAllPages`/`getAllPagesForSync`/`getOne` with a schema (the bare `get` is private). A paginated list that drives deletions must guard against page shifts (an item created or deleted during the walk shifts the following pages, so an item can be skipped and wrongly deleted), and be sorted on a unique key. Brands, categories and products use `getAllPagesForSync`: a list spanning several pages is walked twice and both walks are merged by id. Customers do not, as each one is fetched anyway: as a customer created or deleted during the walk shifts the pages, a stored customer missing from the list is deleted only once `GET /customer/:id` confirms it (404 or empty answer); if it still exists it is synced, if the check fails it is kept. Products are deleted before the upserts; their versions go by cascade. Each product is written in one transaction (upsert, deletion of its missing versions, version upserts), with the repositories taking an optional `Prisma.TransactionClient`. Customer addresses go by cascade with their customer; other relations (product brand/category/supplier, sub-categories, order customer) are set to null and orders are kept.
|
||||||
- Brands, categories and suppliers are upserted through `forEachSafely` (`src/utils/sync.ts`): a failing row is logged and skipped. Each sync step and each product/customer is in a try/catch: failures are logged, the sync goes on, and a summary is logged at the end.
|
- Brands, categories and suppliers are upserted through `forEachSafely` (`src/utils/sync.ts`): a failing row is logged and skipped. Each sync step and each product/customer is in a try/catch: failures are logged, the sync goes on, and a summary is logged at the end.
|
||||||
- Categories form a tree: parents are linked in a second pass (`setParent`). `enabled` mirrors `category_enabled` (POS), `webEnabled` mirrors `category_enabled_www`.
|
- Categories form a tree: parents are linked in a second pass (`setParent`). `enabled` mirrors `category_enabled` (POS), `webEnabled` mirrors `category_enabled_www`.
|
||||||
- Products: `ProductVersion` rows come from `product_size_details` (barcode, price/discount overrides from `product_specific_rules`, stock from the stocks endpoint); no sizes means one version with `hiboutikSizeId: 0`. `globalStock` is the sum of available stock. `archived` mirrors `product_arch`.
|
- Products: `ProductVersion` rows come from `product_size_details` (barcode, price/discount overrides from `product_specific_rules`, stock from the stocks endpoint); no sizes means one version with `hiboutikSizeId: 0`. `globalStock` is the sum of available stock. `archived` mirrors `product_arch`.
|
||||||
- `GET /customer/:id` embeds `addresses` and `sales` (typed in `customer.hiboutik.dto.ts`); `sales` only has headers, lines come from `GET /sales/:id` (`SaleHiboutikRepository`).
|
- `GET /customer/:id` embeds `addresses` and `sales` (typed in `customer.hiboutik.dto.ts`); `sales` only has headers, lines come from `GET /sales/:id` (`SaleHiboutikRepository`).
|
||||||
- Addresses (from the embedded array): missing ones are deleted, the rest upserted via `forEachSafely`; no array means stored addresses are left untouched. Each has its own recipient fields, empty optional values stored as `null`. Company details (`taxNumber`, `companyNumber`, `legalStatus`) come from `GET /customers_addresses/:id`, only for addresses with a `company` (`null` otherwise, untouched if the call fails).
|
- Addresses (from the embedded array): missing ones are deleted, the rest upserted via `forEachSafely`; no array means stored addresses are left untouched. Each has its own recipient fields, empty optional values stored as `null`. Company details (`taxNumber`, `companyNumber`, `legalStatus`) come from `GET /customers_addresses/:id`, only for addresses with a `company` (`null` otherwise, untouched if the call fails).
|
||||||
- Orders come from the customer's `sales`: sales already stored as `COMPLETED` are skipped, the others are fetched and upserted on `hiboutikSaleId` (`source: STORE` on creation only). `status` is `COMPLETED` or `PENDING`, `orderedAt` is the completion date else the creation date. Lines are replaced on each upsert and snapshot name, version, barcode, price and Hiboutik ids; the `ProductVersion` link is optional (`SetNull`). `unitPrice` is the paid price, taxes and discount included. Quotations are skipped, anonymous sales are never synced (only customers' sales are read), and orders are never deleted.
|
- Customers: an email or phone removed in Hiboutik is erased in database (`null`, data minimization).
|
||||||
|
- Dates: Hiboutik sends French local time without offset; `parseHiboutikDate` reads datetimes as `Europe/Paris` whatever the server time zone, and date-only values (date of birth) as midnight UTC.
|
||||||
|
- Orders come from the customer's `sales`: sales already stored as `COMPLETED` and linked to this customer are skipped (an order that lost its customer is fetched again to be linked back), the others are fetched and upserted on `hiboutikSaleId` (`source: STORE` on creation only). `status` is `COMPLETED` or `PENDING`, `orderedAt` is the completion date else the creation date. Lines are replaced on each upsert and snapshot name, version, barcode, price and Hiboutik ids; the `ProductVersion` link is optional (`SetNull`). `unitPrice` is the paid price, taxes and discount included. Quotations are skipped, anonymous sales are never synced (only customers' sales are read), and orders are never deleted.
|
||||||
|
- Slugs (SEO): `Brand`, `Category` and `Product` have a unique `slug`, generated once by `SlugService.assignMissingSlugs` at the end of their sync step and never overwritten by the sync (an admin may replace it). Generation handles the oldest `hiboutikId` first: the slugified name, else (sub-categories only) `<parent name>-<name>`, else `name-<hiboutikId>` on a collision (logged as a warning), else `brand|category|product-<hiboutikId>` for a name without latin letters. `SlugService.changeSlug` keeps the former slug in `SlugRedirect` (for a 301); slugs of redirects stay reserved (an entity may only take back its own former slug). `GET /sitemap` lists the public slugs (brands `enabled`, categories `webEnabled`, products `enabled` and not `archived`) for the storefront's sitemap.xml; only products carry a date, `hiboutikUpdatedAt` (the row's `updatedAt` changes on every sync).
|
||||||
- Shared Prisma payload types (e.g. `ProductWithVersions`) live in `prisma/customObjects/`.
|
- Shared Prisma payload types (e.g. `ProductWithVersions`) live in `prisma/customObjects/`.
|
||||||
|
|
||||||
## Input validation
|
## Input validation
|
||||||
@@ -73,13 +78,13 @@ All data coming from API clients (`@Body()`, `@Query()`, `@Param()`) must be val
|
|||||||
|
|
||||||
## Security rules
|
## Security rules
|
||||||
|
|
||||||
- **Logs**: never log a raw error object, a request/response or an axios config: they carry the Hiboutik `Authorization` header (credentials). Log `(error as Error).message` and `.stack` only, as the sync services do. No personal data (emails, names, addresses, phone numbers, tax numbers) in logs: ids only.
|
- **Logs**: never log a raw error object, a request/response or an axios config: they carry the Hiboutik `Authorization` header (credentials). Log `(error as Error).message` and `.stack` only, as the sync services do. No personal data (emails, names, addresses, phone numbers, tax numbers) in logs: ids only. `PrismaService` uses `errorFormat: 'minimal'` because the default error format prints the query arguments; keep it.
|
||||||
- **SQL**: use the Prisma query API. `$queryRaw`/`$executeRaw` are allowed only as tagged templates with parameters (`` $queryRaw`... ${value}` ``); never `$queryRawUnsafe`/`$executeRawUnsafe` nor string concatenation or interpolation into SQL text.
|
- **SQL**: use the Prisma query API. `$queryRaw`/`$executeRaw` are allowed only as tagged templates with parameters (`` $queryRaw`... ${value}` ``); never `$queryRawUnsafe`/`$executeRawUnsafe` nor string concatenation or interpolation into SQL text.
|
||||||
- **Sync lock**: only one synchronization runs at a time across all instances. `HiboutikService.startSync` takes a Postgres advisory lock (`SyncLockDatabaseRepository`, dedicated `pg` connection held for the whole sync, released by Postgres itself if the process dies). Start syncs only through `startSync`, never call `syncAll` directly from a controller or a scheduler.
|
- **Sync lock**: only one synchronization runs at a time across all instances. `HiboutikService.startSync` takes a Postgres advisory lock (`SyncLockDatabaseRepository`, dedicated `pg` connection held for the whole sync, released by Postgres itself if the process dies). Start syncs only through `startSync`, never call `syncAll` directly from a controller or a scheduler.
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
If the Postman plugin is installed, load its `postman:api-engineer` skill before any non-trivial API engineering task (designing, implementing, mocking, testing, monitoring, documenting, deploying). Pure questions and trivial one-line edits do not need it. The local `postman/` and `.postman/` folders are generated by `postman init` and are git-ignored.
|
If the Postman plugin is installed, load its `postman:api-engineer` skill before any non-trivial API engineering task (designing, implementing, mocking, testing, monitoring, documenting, deploying). Pure questions and trivial one-line edits do not need it. The API collection lives in `postman/collections/` as v3 YAML and is versioned with the code: update it with the API, check with `postman collection lint` and `postman workspace diff --push-strategy default`, and only `postman workspace push` with the user's consent. Each developer binds the folder to their own Postman workspace (`.postman/resources.yaml`, git-ignored, created by `postman workspace connect-git <workspace-id>` or `postman init`). Also versioned: `postman/specs/`, `postman/flows/`; git-ignored: skills and SDKs (generated), globals and environments (personal values, secrets).
|
||||||
|
|
||||||
## Git workflow
|
## Git workflow
|
||||||
|
|
||||||
@@ -88,3 +93,5 @@ The remote is a Gitea instance (`origin`). Use plain `git` or the Gitea CLI (`gt
|
|||||||
- Never commit or push directly to the integration branch: always push changes on a `feature/<name>` branch.
|
- Never commit or push directly to the integration branch: always push changes on a `feature/<name>` branch.
|
||||||
- Merges go into `develop`. Until `develop` exists (early project phase), they go into `main`.
|
- Merges go into `develop`. Until `develop` exists (early project phase), they go into `main`.
|
||||||
- Commit messages follow Conventional Commits (`feat(sync): ...`, `fix(db): ...`).
|
- Commit messages follow Conventional Commits (`feat(sync): ...`, `fix(db): ...`).
|
||||||
|
- Commit messages and PR/MR content must contain a single-line title only.
|
||||||
|
Do not include any body description or co-author metadata (e.g., "Co-authored-by: Claude").
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export default tseslint.config(
|
|||||||
{
|
{
|
||||||
rules: {
|
rules: {
|
||||||
'@typescript-eslint/no-explicit-any': 'off',
|
'@typescript-eslint/no-explicit-any': 'off',
|
||||||
'@typescript-eslint/no-floating-promises': 'warn',
|
'@typescript-eslint/no-floating-promises': 'error',
|
||||||
'@typescript-eslint/no-unsafe-argument': 'warn',
|
'@typescript-eslint/no-unsafe-argument': 'warn',
|
||||||
"prettier/prettier": "error",
|
"prettier/prettier": "error",
|
||||||
},
|
},
|
||||||
|
|||||||
185
package-lock.json
generated
185
package-lock.json
generated
@@ -11,10 +11,10 @@
|
|||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nestjs/axios": "^4.0.1",
|
"@nestjs/axios": "^4.0.1",
|
||||||
"@nestjs/common": "^11.0.1",
|
"@nestjs/common": "^11.1.29",
|
||||||
"@nestjs/config": "^12.0.0",
|
"@nestjs/config": "^12.0.0",
|
||||||
"@nestjs/core": "^11.0.1",
|
"@nestjs/core": "^11.1.29",
|
||||||
"@nestjs/platform-express": "^11.0.1",
|
"@nestjs/platform-express": "^11.1.29",
|
||||||
"@nestjs/throttler": "^6.7.1",
|
"@nestjs/throttler": "^6.7.1",
|
||||||
"@prisma/adapter-pg": "^7.10.0",
|
"@prisma/adapter-pg": "^7.10.0",
|
||||||
"@prisma/client": "^7.10.0",
|
"@prisma/client": "^7.10.0",
|
||||||
@@ -24,14 +24,15 @@
|
|||||||
"helmet": "^8.3.0",
|
"helmet": "^8.3.0",
|
||||||
"pg": "^8.23.0",
|
"pg": "^8.23.0",
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1"
|
"rxjs": "^7.8.1",
|
||||||
|
"zod": "4.6.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.2.0",
|
"@eslint/eslintrc": "^3.2.0",
|
||||||
"@eslint/js": "^9.18.0",
|
"@eslint/js": "^9.18.0",
|
||||||
"@nestjs/cli": "^12.0.11",
|
"@nestjs/cli": "^12.0.11",
|
||||||
"@nestjs/schematics": "^11.0.0",
|
"@nestjs/schematics": "^11.0.0",
|
||||||
"@nestjs/testing": "^11.0.1",
|
"@nestjs/testing": "^11.1.29",
|
||||||
"@types/express": "^5.0.0",
|
"@types/express": "^5.0.0",
|
||||||
"@types/jest": "^30.0.0",
|
"@types/jest": "^30.0.0",
|
||||||
"@types/node": "^24.0.0",
|
"@types/node": "^24.0.0",
|
||||||
@@ -2554,9 +2555,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@nestjs/common": {
|
"node_modules/@nestjs/common": {
|
||||||
"version": "11.1.26",
|
"version": "11.1.29",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/common/-/common-11.1.26.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/common/-/common-11.1.29.tgz",
|
||||||
"integrity": "sha512-0VARQyzuGbprvjO+slWq9Jtj1P0jYCSKAUSv9LWFNWD39ZbDzXXM1pMs35kReVXwchra0urMfTQxw4uAOfdSzA==",
|
"integrity": "sha512-zkeNRlfiQIH/044r5zphjNzKYxBRC4O00onTdCsH2qq0R30Ixo0gOS9so5TadEbJJCuGNm0Vx3PBE2FG2vkBdA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"file-type": "21.3.4",
|
"file-type": "21.3.4",
|
||||||
@@ -2601,9 +2602,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@nestjs/core": {
|
"node_modules/@nestjs/core": {
|
||||||
"version": "11.1.26",
|
"version": "11.1.29",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.26.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-11.1.29.tgz",
|
||||||
"integrity": "sha512-K45zUwYpowEsVqm8qNIzsMcl4LJev0MK9zVhDnmym7YRTJ2/caslqVeKYhPRd5+Fh81IkvWUVu6vEo46uZ5mgQ==",
|
"integrity": "sha512-ANXnZxirMNAY+JuRHCIYIdTfbhX3rizeSbJloub/4EYwpIfCuUoaz9woOofWmywYYc3gsVkLxVZV03gqntI8vA==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fast-safe-stringify": "2.1.1",
|
"fast-safe-stringify": "2.1.1",
|
||||||
@@ -2640,15 +2641,15 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@nestjs/platform-express": {
|
"node_modules/@nestjs/platform-express": {
|
||||||
"version": "11.1.1",
|
"version": "11.1.29",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-11.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-11.1.29.tgz",
|
||||||
"integrity": "sha512-IUxk380qnUtz0PCRQ5i+o9UHlGMrFzGPIJxDwyt3JZZwx2AngOlcEcm5e+7YeJQEr2QYX2QyC4tUQg0zde+D7A==",
|
"integrity": "sha512-OngsAjdzM+SbbmP1XojKfBgl0Lj7u8b7IK5duwpFymzNP5EmjS8ujvqck9M8h0L14I91aRIFSjIpVfHj8WVtqw==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "2.8.5",
|
"cors": "2.8.6",
|
||||||
"express": "5.1.0",
|
"express": "5.2.1",
|
||||||
"multer": "1.4.5-lts.2",
|
"multer": "2.2.0",
|
||||||
"path-to-regexp": "8.2.0",
|
"path-to-regexp": "8.4.2",
|
||||||
"tslib": "2.8.1"
|
"tslib": "2.8.1"
|
||||||
},
|
},
|
||||||
"funding": {
|
"funding": {
|
||||||
@@ -2660,15 +2661,6 @@
|
|||||||
"@nestjs/core": "^11.0.0"
|
"@nestjs/core": "^11.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@nestjs/platform-express/node_modules/path-to-regexp": {
|
|
||||||
"version": "8.2.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.2.0.tgz",
|
|
||||||
"integrity": "sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==",
|
|
||||||
"license": "MIT",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=16"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/@nestjs/schematics": {
|
"node_modules/@nestjs/schematics": {
|
||||||
"version": "11.1.0",
|
"version": "11.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-11.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-11.1.0.tgz",
|
||||||
@@ -2774,9 +2766,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@nestjs/testing": {
|
"node_modules/@nestjs/testing": {
|
||||||
"version": "11.1.26",
|
"version": "11.1.29",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-11.1.26.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-11.1.29.tgz",
|
||||||
"integrity": "sha512-PNf/TSIoF525pb3O9bu06S5QFWl4gArngbjI6yRFKZrF7d85vGjleVkgVVbw879Y95EYDzIS3eel6YlaY4TGFw==",
|
"integrity": "sha512-u+aEp90Ly3uQbLoicZs+ZOrJTyvaD0fbd6dpnSgFZz5Bf77gbKKfr+xFE2JxqZuIH15HlBrqDL7/OL2qL8oG+A==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -5598,6 +5590,7 @@
|
|||||||
"version": "1.1.2",
|
"version": "1.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
|
||||||
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
|
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
|
||||||
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/busboy": {
|
"node_modules/busboy": {
|
||||||
@@ -5995,51 +5988,6 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/concat-stream": {
|
|
||||||
"version": "1.6.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz",
|
|
||||||
"integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==",
|
|
||||||
"engines": [
|
|
||||||
"node >= 0.8"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"buffer-from": "^1.0.0",
|
|
||||||
"inherits": "^2.0.3",
|
|
||||||
"readable-stream": "^2.2.2",
|
|
||||||
"typedarray": "^0.0.6"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/concat-stream/node_modules/readable-stream": {
|
|
||||||
"version": "2.3.8",
|
|
||||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz",
|
|
||||||
"integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"core-util-is": "~1.0.0",
|
|
||||||
"inherits": "~2.0.3",
|
|
||||||
"isarray": "~1.0.0",
|
|
||||||
"process-nextick-args": "~2.0.0",
|
|
||||||
"safe-buffer": "~5.1.1",
|
|
||||||
"string_decoder": "~1.1.1",
|
|
||||||
"util-deprecate": "~1.0.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/concat-stream/node_modules/safe-buffer": {
|
|
||||||
"version": "5.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
|
||||||
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/concat-stream/node_modules/string_decoder": {
|
|
||||||
"version": "1.1.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
|
|
||||||
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"safe-buffer": "~5.1.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/confbox": {
|
"node_modules/confbox": {
|
||||||
"version": "0.2.4",
|
"version": "0.2.4",
|
||||||
"resolved": "https://registry.npmjs.org/confbox/-/confbox-0.2.4.tgz",
|
"resolved": "https://registry.npmjs.org/confbox/-/confbox-0.2.4.tgz",
|
||||||
@@ -6101,16 +6049,10 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/core-util-is": {
|
|
||||||
"version": "1.0.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
|
|
||||||
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/cors": {
|
"node_modules/cors": {
|
||||||
"version": "2.8.5",
|
"version": "2.8.6",
|
||||||
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
|
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz",
|
||||||
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
|
"integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"object-assign": "^4",
|
"object-assign": "^4",
|
||||||
@@ -6118,6 +6060,10 @@
|
|||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.10"
|
"node": ">= 0.10"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/express"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/create-require": {
|
"node_modules/create-require": {
|
||||||
@@ -7006,18 +6952,19 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/express": {
|
"node_modules/express": {
|
||||||
"version": "5.1.0",
|
"version": "5.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/express/-/express-5.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/express/-/express-5.2.1.tgz",
|
||||||
"integrity": "sha512-DT9ck5YIRU+8GYzzU5kT3eHGA5iL+1Zd0EutOmTE9Dtk+Tvuzd23VBU+ec7HPNSTxXYO55gPV/hq4pSBJDjFpA==",
|
"integrity": "sha512-hIS4idWWai69NezIdRt2xFVofaF4j+6INOpJlVOLDO8zXGpUVEVzIYk12UUi2JzjEzWL3IOAxcTubgz9Po0yXw==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"accepts": "^2.0.0",
|
"accepts": "^2.0.0",
|
||||||
"body-parser": "^2.2.0",
|
"body-parser": "^2.2.1",
|
||||||
"content-disposition": "^1.0.0",
|
"content-disposition": "^1.0.0",
|
||||||
"content-type": "^1.0.5",
|
"content-type": "^1.0.5",
|
||||||
"cookie": "^0.7.1",
|
"cookie": "^0.7.1",
|
||||||
"cookie-signature": "^1.2.1",
|
"cookie-signature": "^1.2.1",
|
||||||
"debug": "^4.4.0",
|
"debug": "^4.4.0",
|
||||||
|
"depd": "^2.0.0",
|
||||||
"encodeurl": "^2.0.0",
|
"encodeurl": "^2.0.0",
|
||||||
"escape-html": "^1.0.3",
|
"escape-html": "^1.0.3",
|
||||||
"etag": "^1.8.1",
|
"etag": "^1.8.1",
|
||||||
@@ -8068,12 +8015,6 @@
|
|||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/isarray": {
|
|
||||||
"version": "1.0.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
|
|
||||||
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/isexe": {
|
"node_modules/isexe": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
|
||||||
@@ -9197,6 +9138,7 @@
|
|||||||
"version": "1.2.8",
|
"version": "1.2.8",
|
||||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
|
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
|
||||||
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
|
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
|
||||||
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"funding": {
|
"funding": {
|
||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
@@ -9212,18 +9154,6 @@
|
|||||||
"node": ">=16 || 14 >=14.17"
|
"node": ">=16 || 14 >=14.17"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/mkdirp": {
|
|
||||||
"version": "0.5.6",
|
|
||||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz",
|
|
||||||
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"minimist": "^1.2.6"
|
|
||||||
},
|
|
||||||
"bin": {
|
|
||||||
"mkdirp": "bin/cmd.js"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/ms": {
|
"node_modules/ms": {
|
||||||
"version": "2.1.3",
|
"version": "2.1.3",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
||||||
@@ -9231,22 +9161,21 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/multer": {
|
"node_modules/multer": {
|
||||||
"version": "1.4.5-lts.2",
|
"version": "2.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/multer/-/multer-1.4.5-lts.2.tgz",
|
"resolved": "https://registry.npmjs.org/multer/-/multer-2.5.0.tgz",
|
||||||
"integrity": "sha512-VzGiVigcG9zUAoCNU+xShztrlr1auZOlurXynNvO9GiWD1/mTBbUljOKY+qMeazBqXgRnjzeEgJI/wyjJUHg9A==",
|
"integrity": "sha512-BsqOzYGVq49JcW2IzoSAxeKYRevb+dGGPPWleOR8Lrlk4igwxu1/9pwSXlByhCb7MVZDrNJFC0IBP5/O5nLYRw==",
|
||||||
"deprecated": "Multer 1.x is impacted by a number of vulnerabilities, which have been patched in 2.x. You should upgrade to the latest 2.x version.",
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"append-field": "^1.0.0",
|
"append-field": "^1.0.0",
|
||||||
"busboy": "^1.0.0",
|
"busboy": "^1.6.0",
|
||||||
"concat-stream": "^1.5.2",
|
"type-is": "^1.6.18"
|
||||||
"mkdirp": "^0.5.4",
|
|
||||||
"object-assign": "^4.1.1",
|
|
||||||
"type-is": "^1.6.4",
|
|
||||||
"xtend": "^4.0.0"
|
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 6.0.0"
|
"node": ">= 10.16.0"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/express"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/multer/node_modules/media-typer": {
|
"node_modules/multer/node_modules/media-typer": {
|
||||||
@@ -10104,12 +10033,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/process-nextick-args": {
|
|
||||||
"version": "2.0.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
|
||||||
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/proper-lockfile": {
|
"node_modules/proper-lockfile": {
|
||||||
"version": "4.1.2",
|
"version": "4.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz",
|
||||||
@@ -11660,12 +11583,6 @@
|
|||||||
"url": "https://opencollective.com/express"
|
"url": "https://opencollective.com/express"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/typedarray": {
|
|
||||||
"version": "0.0.6",
|
|
||||||
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
|
|
||||||
"integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==",
|
|
||||||
"license": "MIT"
|
|
||||||
},
|
|
||||||
"node_modules/typescript": {
|
"node_modules/typescript": {
|
||||||
"version": "5.9.3",
|
"version": "5.9.3",
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
||||||
@@ -11848,6 +11765,7 @@
|
|||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
|
||||||
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
|
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
|
||||||
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/v8-compile-cache-lib": {
|
"node_modules/v8-compile-cache-lib": {
|
||||||
@@ -12286,6 +12204,15 @@
|
|||||||
"grammex": "^3.1.11",
|
"grammex": "^3.1.11",
|
||||||
"graphmatch": "^1.1.0"
|
"graphmatch": "^1.1.0"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"node_modules/zod": {
|
||||||
|
"version": "4.6.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/zod/-/zod-4.6.7.tgz",
|
||||||
|
"integrity": "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/colinhacks"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
package.json
14
package.json
@@ -22,10 +22,10 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nestjs/axios": "^4.0.1",
|
"@nestjs/axios": "^4.0.1",
|
||||||
"@nestjs/common": "^11.0.1",
|
"@nestjs/common": "^11.1.29",
|
||||||
"@nestjs/config": "^12.0.0",
|
"@nestjs/config": "^12.0.0",
|
||||||
"@nestjs/core": "^11.0.1",
|
"@nestjs/core": "^11.1.29",
|
||||||
"@nestjs/platform-express": "^11.0.1",
|
"@nestjs/platform-express": "^11.1.29",
|
||||||
"@nestjs/throttler": "^6.7.1",
|
"@nestjs/throttler": "^6.7.1",
|
||||||
"@prisma/adapter-pg": "^7.10.0",
|
"@prisma/adapter-pg": "^7.10.0",
|
||||||
"@prisma/client": "^7.10.0",
|
"@prisma/client": "^7.10.0",
|
||||||
@@ -35,14 +35,15 @@
|
|||||||
"helmet": "^8.3.0",
|
"helmet": "^8.3.0",
|
||||||
"pg": "^8.23.0",
|
"pg": "^8.23.0",
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1"
|
"rxjs": "^7.8.1",
|
||||||
|
"zod": "4.6.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.2.0",
|
"@eslint/eslintrc": "^3.2.0",
|
||||||
"@eslint/js": "^9.18.0",
|
"@eslint/js": "^9.18.0",
|
||||||
"@nestjs/cli": "^12.0.11",
|
"@nestjs/cli": "^12.0.11",
|
||||||
"@nestjs/schematics": "^11.0.0",
|
"@nestjs/schematics": "^11.0.0",
|
||||||
"@nestjs/testing": "^11.0.1",
|
"@nestjs/testing": "^11.1.29",
|
||||||
"@types/express": "^5.0.0",
|
"@types/express": "^5.0.0",
|
||||||
"@types/jest": "^30.0.0",
|
"@types/jest": "^30.0.0",
|
||||||
"@types/node": "^24.0.0",
|
"@types/node": "^24.0.0",
|
||||||
@@ -101,5 +102,8 @@
|
|||||||
"@parcel/watcher@2.6.0": true,
|
"@parcel/watcher@2.6.0": true,
|
||||||
"@prisma/engines@7.10.1": true,
|
"@prisma/engines@7.10.1": true,
|
||||||
"prisma@7.10.1": true
|
"prisma@7.10.1": true
|
||||||
|
},
|
||||||
|
"overrides": {
|
||||||
|
"multer": "^2.5.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
$kind: collection
|
||||||
|
variables:
|
||||||
|
baseUrl: "http://localhost:3000"
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
$kind: http-example
|
||||||
|
request:
|
||||||
|
url: "{{baseUrl}}/sitemap"
|
||||||
|
method: GET
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
response:
|
||||||
|
statusCode: 200
|
||||||
|
statusText: OK
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json; charset=utf-8
|
||||||
|
Cache-Control: public, max-age=600
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
content: |
|
||||||
|
{
|
||||||
|
"brands": [{ "slug": "stray-kids" }],
|
||||||
|
"categories": [{ "slug": "albums" }],
|
||||||
|
"products": [{ "slug": "ate-digipack", "updatedAt": "2026-10-28T14:32:00.000Z" }]
|
||||||
|
}
|
||||||
|
order: 1000
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
$kind: http-example
|
||||||
|
request:
|
||||||
|
url: "{{baseUrl}}/sync"
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
response:
|
||||||
|
statusCode: 503
|
||||||
|
statusText: Service Unavailable
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json; charset=utf-8
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
content: |
|
||||||
|
{ "message": "This instance is shutting down, retry in a moment", "error": "Service Unavailable", "statusCode": 503 }
|
||||||
|
order: 4000
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
$kind: http-example
|
||||||
|
request:
|
||||||
|
url: "{{baseUrl}}/sync"
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
response:
|
||||||
|
statusCode: 429
|
||||||
|
statusText: Too Many Requests
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json; charset=utf-8
|
||||||
|
Retry-After: "30"
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
content: |
|
||||||
|
{ "statusCode": 429, "message": "ThrottlerException: Too Many Requests" }
|
||||||
|
order: 3000
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
$kind: http-example
|
||||||
|
request:
|
||||||
|
url: "{{baseUrl}}/sync"
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
response:
|
||||||
|
statusCode: 409
|
||||||
|
statusText: Conflict
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json; charset=utf-8
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
content: |
|
||||||
|
{ "message": "A Hiboutik synchronization is already in progress", "error": "Conflict", "statusCode": 409 }
|
||||||
|
order: 2000
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
$kind: http-example
|
||||||
|
request:
|
||||||
|
url: "{{baseUrl}}/sync"
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
response:
|
||||||
|
statusCode: 202
|
||||||
|
statusText: Accepted
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json; charset=utf-8
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
content: |
|
||||||
|
{ "message": "Hiboutik synchronization started" }
|
||||||
|
order: 1000
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
$kind: http-example
|
||||||
|
request:
|
||||||
|
url: "{{baseUrl}}/sync"
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
response:
|
||||||
|
statusCode: 500
|
||||||
|
statusText: Internal Server Error
|
||||||
|
headers:
|
||||||
|
Content-Type: application/json; charset=utf-8
|
||||||
|
body:
|
||||||
|
type: json
|
||||||
|
content: |
|
||||||
|
{ "message": "Internal Server Error", "statusCode": 500 }
|
||||||
|
order: 5000
|
||||||
8
postman/collections/DinggaDingga/getSitemap.request.yaml
Normal file
8
postman/collections/DinggaDingga/getSitemap.request.yaml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
$kind: http-request
|
||||||
|
description: "Public pages of the storefront (brands enabled, categories displayed on the website, products displayed on the website and not archived) that have a slug. Products carry `updatedAt`, their last update in Hiboutik (null when unknown); brands and categories have no date, as the sync rewrites their rows every time. The storefront builds its sitemap.xml from it. Answered with Cache-Control: public, max-age=600."
|
||||||
|
url: "{{baseUrl}}/sitemap"
|
||||||
|
method: GET
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
examples: ./.resources/getSitemap.resources/examples
|
||||||
|
order: 2000
|
||||||
26
postman/collections/DinggaDingga/syncHiboutik.request.yaml
Normal file
26
postman/collections/DinggaDingga/syncHiboutik.request.yaml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
$kind: http-request
|
||||||
|
description: |-
|
||||||
|
Starts a full Hiboutik → database synchronization **in the background** and answers immediately: the request never waits for the sync to end.
|
||||||
|
|
||||||
|
**What it syncs**, in dependency order: brands → categories → suppliers → products (with their versions and stocks) → customers (with their addresses and orders). New brands, categories and products get their slug at the end of their step.
|
||||||
|
|
||||||
|
**Hiboutik is the source of truth**: brands, categories, suppliers, products and customers missing from Hiboutik are deleted from the database (orders are never deleted). A failed Hiboutik call deletes nothing. A failing item is logged and skipped; the sync goes on and logs a summary at the end. Follow its progress in the server logs.
|
||||||
|
|
||||||
|
**One sync at a time**, across all instances (Postgres advisory lock): a call while a sync is running answers `409`.
|
||||||
|
|
||||||
|
**Cost**: a sync uses roughly one Hiboutik API call per customer, plus a few per entity type. Hiboutik allows 10,000 calls per month per shop; when its rate limit is reached, the sync stops.
|
||||||
|
|
||||||
|
**Responses**
|
||||||
|
- `202` sync started.
|
||||||
|
- `409` a sync is already running (on this instance or another one).
|
||||||
|
- `429` rate limit: 5 calls per minute per IP on this route. `Retry-After` gives the seconds to wait.
|
||||||
|
- `503` this instance is shutting down: retry, another instance can take the call.
|
||||||
|
- `500` unexpected error (e.g. database unreachable); details are in the server logs only.
|
||||||
|
|
||||||
|
No body, no parameters.
|
||||||
|
url: "{{baseUrl}}/sync"
|
||||||
|
method: POST
|
||||||
|
headers:
|
||||||
|
Accept: application/json
|
||||||
|
examples: ./.resources/syncHiboutik.resources/examples
|
||||||
|
order: 1000
|
||||||
@@ -1,20 +1,25 @@
|
|||||||
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
import { Injectable, OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
|
||||||
import { PrismaClient } from './generated/client';
|
import { PrismaClient } from './generated/client';
|
||||||
import { PrismaPg } from '@prisma/adapter-pg';
|
import { PrismaPg } from '@prisma/adapter-pg';
|
||||||
|
|
||||||
/** Prisma client shared by the whole app (global PrismaModule), connected to `DATABASE_URL` through the pg driver adapter. */
|
/** Prisma client shared by the whole app (global PrismaModule), connected to `DATABASE_URL` through the pg driver adapter. */
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
|
export class PrismaService extends PrismaClient implements OnModuleInit, OnApplicationShutdown {
|
||||||
constructor() {
|
constructor() {
|
||||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
|
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
|
||||||
super({ adapter });
|
// 'minimal': by default, error messages print the query arguments (emails, names, addresses...), which end up in the logs
|
||||||
|
super({ adapter, errorFormat: 'minimal' });
|
||||||
}
|
}
|
||||||
|
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
await this.$connect();
|
await this.$connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
async onModuleDestroy() {
|
/**
|
||||||
|
* Last shutdown hook (after `onModuleDestroy` and `beforeApplicationShutdown`, once the HTTP server is closed):
|
||||||
|
* a synchronization still running is waited for in `beforeApplicationShutdown` and keeps the database until then.
|
||||||
|
*/
|
||||||
|
async onApplicationShutdown() {
|
||||||
await this.$disconnect();
|
await this.$disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ model Category {
|
|||||||
name String
|
name String
|
||||||
/// Display order among its siblings (category_position)
|
/// Display order among its siblings (category_position)
|
||||||
position Int @default(0)
|
position Int @default(0)
|
||||||
|
/// URL identifier (SEO): generated once by the sync, never overwritten by it; null until generated
|
||||||
|
slug String? @unique
|
||||||
/// Displayed on the website (category_enabled_www)
|
/// Displayed on the website (category_enabled_www)
|
||||||
webEnabled Boolean @default(true)
|
webEnabled Boolean @default(true)
|
||||||
|
|
||||||
@@ -31,7 +33,8 @@ model Category {
|
|||||||
parentId String?
|
parentId String?
|
||||||
children Category[] @relation("CategoryTree")
|
children Category[] @relation("CategoryTree")
|
||||||
|
|
||||||
products Product[]
|
products Product[]
|
||||||
|
slugRedirects SlugRedirect[]
|
||||||
|
|
||||||
@@map("categories")
|
@@map("categories")
|
||||||
}
|
}
|
||||||
@@ -44,8 +47,11 @@ model Brand {
|
|||||||
enabled Boolean @default(true)
|
enabled Boolean @default(true)
|
||||||
hiboutikId Int @unique
|
hiboutikId Int @unique
|
||||||
name String
|
name String
|
||||||
|
/// URL identifier (SEO): generated once by the sync, never overwritten by it; null until generated
|
||||||
|
slug String? @unique
|
||||||
|
|
||||||
products Product[]
|
products Product[]
|
||||||
|
slugRedirects SlugRedirect[]
|
||||||
|
|
||||||
@@map("brands")
|
@@map("brands")
|
||||||
}
|
}
|
||||||
@@ -84,6 +90,8 @@ model Product {
|
|||||||
hiboutikUpdatedAt DateTime?
|
hiboutikUpdatedAt DateTime?
|
||||||
name String
|
name String
|
||||||
price Decimal @db.Decimal(10, 2)
|
price Decimal @db.Decimal(10, 2)
|
||||||
|
/// URL identifier (SEO): generated once by the sync, never overwritten by it; null until generated
|
||||||
|
slug String? @unique
|
||||||
|
|
||||||
brand Brand? @relation(fields: [brandId], references: [id], onDelete: SetNull)
|
brand Brand? @relation(fields: [brandId], references: [id], onDelete: SetNull)
|
||||||
brandId String?
|
brandId String?
|
||||||
@@ -92,11 +100,42 @@ model Product {
|
|||||||
supplier Supplier? @relation(fields: [supplierId], references: [id], onDelete: SetNull)
|
supplier Supplier? @relation(fields: [supplierId], references: [id], onDelete: SetNull)
|
||||||
supplierId String?
|
supplierId String?
|
||||||
|
|
||||||
versions ProductVersion[]
|
versions ProductVersion[]
|
||||||
|
slugRedirects SlugRedirect[]
|
||||||
|
|
||||||
@@map("products")
|
@@map("products")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Kind of entity a slug identifies. Slugs are unique per kind (each one has its own URL space).
|
||||||
|
enum SlugEntityType {
|
||||||
|
BRAND
|
||||||
|
CATEGORY
|
||||||
|
PRODUCT
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Former slug of a brand, category or product, kept so that its old URL can be answered with a 301 redirect.
|
||||||
|
/// Exactly one of brandId / categoryId / productId is set, matching `entityType`. Deleted with its entity.
|
||||||
|
model SlugRedirect {
|
||||||
|
id String @id @default(uuid())
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
|
||||||
|
entityType SlugEntityType
|
||||||
|
slug String
|
||||||
|
|
||||||
|
brand Brand? @relation(fields: [brandId], references: [id], onDelete: Cascade)
|
||||||
|
brandId String?
|
||||||
|
category Category? @relation(fields: [categoryId], references: [id], onDelete: Cascade)
|
||||||
|
categoryId String?
|
||||||
|
product Product? @relation(fields: [productId], references: [id], onDelete: Cascade)
|
||||||
|
productId String?
|
||||||
|
|
||||||
|
@@unique([entityType, slug])
|
||||||
|
@@index([brandId])
|
||||||
|
@@index([categoryId])
|
||||||
|
@@index([productId])
|
||||||
|
@@map("slug_redirects")
|
||||||
|
}
|
||||||
|
|
||||||
/// Sellable version of a product: one per Hiboutik size, or a single one (hiboutikSizeId 0) for a product without sizes.
|
/// Sellable version of a product: one per Hiboutik size, or a single one (hiboutikSizeId 0) for a product without sizes.
|
||||||
model ProductVersion {
|
model ProductVersion {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
@@ -130,7 +169,7 @@ model Customer {
|
|||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
dateOfBirth DateTime?
|
dateOfBirth DateTime?
|
||||||
/// Kept when removed in Hiboutik (an empty Hiboutik email does not erase it)
|
/// Null when the customer has no email in Hiboutik (erased when removed there)
|
||||||
email String?
|
email String?
|
||||||
firstName String
|
firstName String
|
||||||
hiboutikId Int @unique
|
hiboutikId Int @unique
|
||||||
@@ -227,3 +266,19 @@ model OrderLine {
|
|||||||
|
|
||||||
@@map("order_lines")
|
@@map("order_lines")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Rate limiting counter shared by every instance (ThrottlerDatabaseStorage): one fixed window per throttler key
|
||||||
|
/// (route + client IP). Not synced from Hiboutik. Rows whose window and block are over are purged regularly.
|
||||||
|
model ThrottlerRecord {
|
||||||
|
/// Key generated by the ThrottlerGuard (hash of route, throttler name and client IP)
|
||||||
|
key String @id
|
||||||
|
/// Requests counted in the current window
|
||||||
|
hits Int
|
||||||
|
/// End of the current window
|
||||||
|
expiresAt DateTime @db.Timestamptz(3)
|
||||||
|
/// End of the block once the limit was exceeded, null when not blocked
|
||||||
|
blockedUntil DateTime? @db.Timestamptz(3)
|
||||||
|
|
||||||
|
@@index([expiresAt])
|
||||||
|
@@map("throttler_records")
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ export class AppController {
|
|||||||
constructor(private readonly hiboutikService: HiboutikService) {}
|
constructor(private readonly hiboutikService: HiboutikService) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the synchronization in the background and answers immediately: 202, or 409 if one is already running.
|
* Starts the synchronization in the background and answers immediately: 202, 409 if one is already running, or 503
|
||||||
|
* if this instance is shutting down (another instance can take the call).
|
||||||
* Rate limited to 5 calls per minute: each one can trigger thousands of Hiboutik API calls.
|
* Rate limited to 5 calls per minute: each one can trigger thousands of Hiboutik API calls.
|
||||||
*/
|
*/
|
||||||
@Post('sync')
|
@Post('sync')
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import { ProductHiboutikRepository } from './hiboutik/product.hiboutik.repositor
|
|||||||
import { HttpModule } from '@nestjs/axios';
|
import { HttpModule } from '@nestjs/axios';
|
||||||
import { CategoryDatabaseRepository } from './database/category.database.repository';
|
import { CategoryDatabaseRepository } from './database/category.database.repository';
|
||||||
import { PrismaModule } from '../prisma/prisma.module';
|
import { PrismaModule } from '../prisma/prisma.module';
|
||||||
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
|
import { ThrottlerDatabaseStorage } from './database/throttlerStorage.database.repository';
|
||||||
import { HiboutikClient } from './hiboutik/hiboutik.client';
|
import { HiboutikClient } from './hiboutik/hiboutik.client';
|
||||||
import { BrandHiboutikRepository } from './hiboutik/brand.hiboutik.repository';
|
import { BrandHiboutikRepository } from './hiboutik/brand.hiboutik.repository';
|
||||||
import { BrandDatabaseRepository } from './database/brand.database.repository';
|
import { BrandDatabaseRepository } from './database/brand.database.repository';
|
||||||
@@ -37,6 +39,10 @@ import { CustomerHiboutikService } from './services/hiboutik/customer.hiboutik.s
|
|||||||
import { SupplierHiboutikService } from './services/hiboutik/supplier.hiboutik.service';
|
import { SupplierHiboutikService } from './services/hiboutik/supplier.hiboutik.service';
|
||||||
import { ProductHiboutikService } from './services/hiboutik/product.hiboutik.service';
|
import { ProductHiboutikService } from './services/hiboutik/product.hiboutik.service';
|
||||||
import { SyncLockDatabaseRepository } from './database/syncLock.database.repository';
|
import { SyncLockDatabaseRepository } from './database/syncLock.database.repository';
|
||||||
|
import { SlugDatabaseRepository } from './database/slug.database.repository';
|
||||||
|
import { SlugService } from './services/slug/slug.service';
|
||||||
|
import { SitemapDatabaseRepository } from './database/sitemap.database.repository';
|
||||||
|
import { SitemapController } from './sitemap.controller';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -44,12 +50,25 @@ import { SyncLockDatabaseRepository } from './database/syncLock.database.reposit
|
|||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
validate: validateEnv,
|
validate: validateEnv,
|
||||||
}),
|
}),
|
||||||
// Default rate limit for every route: 60 requests per minute per IP (stricter limits via @Throttle)
|
// Default rate limit for every route: 60 requests per minute per IP (stricter limits via @Throttle).
|
||||||
ThrottlerModule.forRoot([{ ttl: 60_000, limit: 60 }]),
|
// Counters live in PostgreSQL so that every instance shares them (see ThrottlerDatabaseStorage).
|
||||||
HttpModule.register({ timeout: 30_000 }),
|
ThrottlerModule.forRootAsync({
|
||||||
|
inject: [PrismaService],
|
||||||
|
useFactory: (prisma: PrismaService) => ({
|
||||||
|
throttlers: [{ ttl: 60_000, limit: 60 }],
|
||||||
|
storage: new ThrottlerDatabaseStorage(prisma),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
HttpModule.register({
|
||||||
|
timeout: 30_000,
|
||||||
|
// Hiboutik never redirects: never follow a redirect with the Basic auth credentials
|
||||||
|
maxRedirects: 0,
|
||||||
|
// A runaway answer (bug, faulty proxy) must not be loaded whole in memory
|
||||||
|
maxContentLength: 50 * 1024 * 1024,
|
||||||
|
}),
|
||||||
PrismaModule,
|
PrismaModule,
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController, SitemapController],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: APP_GUARD, useClass: ThrottlerGuard },
|
{ provide: APP_GUARD, useClass: ThrottlerGuard },
|
||||||
{ provide: APP_FILTER, useClass: AllExceptionsFilter },
|
{ provide: APP_FILTER, useClass: AllExceptionsFilter },
|
||||||
@@ -77,6 +96,9 @@ import { SyncLockDatabaseRepository } from './database/syncLock.database.reposit
|
|||||||
ProductHiboutikService,
|
ProductHiboutikService,
|
||||||
ProductVersionDatabaseRepository,
|
ProductVersionDatabaseRepository,
|
||||||
SaleHiboutikRepository,
|
SaleHiboutikRepository,
|
||||||
|
SitemapDatabaseRepository,
|
||||||
|
SlugDatabaseRepository,
|
||||||
|
SlugService,
|
||||||
StockHiboutikRepository,
|
StockHiboutikRepository,
|
||||||
SupplierConverter,
|
SupplierConverter,
|
||||||
SupplierDatabaseRepository,
|
SupplierDatabaseRepository,
|
||||||
|
|||||||
@@ -36,4 +36,35 @@ describe('validateEnv', () => {
|
|||||||
expect((error as Error).message).not.toContain('secret');
|
expect((error as Error).message).not.toContain('secret');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('in production', () => {
|
||||||
|
const production = { ...valid, NODE_ENV: 'production' };
|
||||||
|
|
||||||
|
it('requires an encrypted database connection', () => {
|
||||||
|
expect(() => validateEnv(production)).toThrow(/DATABASE_URL: sslmode/);
|
||||||
|
expect(() => validateEnv({ ...production, DATABASE_URL: `${valid.DATABASE_URL}?sslmode=disable` })).toThrow(/DATABASE_URL/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(['require', 'verify-ca', 'verify-full'])('accepts sslmode=%s', (mode) => {
|
||||||
|
expect(() => validateEnv({ ...production, DATABASE_URL: `${valid.DATABASE_URL}?sslmode=${mode}` })).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('never puts the connection string in the error message', () => {
|
||||||
|
expect.assertions(2);
|
||||||
|
try {
|
||||||
|
validateEnv(production);
|
||||||
|
} catch (error) {
|
||||||
|
expect((error as Error).message).toContain('DATABASE_URL');
|
||||||
|
expect((error as Error).message).not.toContain('secret');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not require TLS outside production', () => {
|
||||||
|
expect(() => validateEnv({ ...valid, NODE_ENV: 'development' })).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects an unknown NODE_ENV', () => {
|
||||||
|
expect(() => validateEnv({ ...valid, NODE_ENV: 'prod' })).toThrow(/NODE_ENV/);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,8 +1,17 @@
|
|||||||
import { plainToInstance } from 'class-transformer';
|
import { plainToInstance } from 'class-transformer';
|
||||||
import { IsInt, IsNotEmpty, IsOptional, IsString, IsUrl, Max, Min, validateSync } from 'class-validator';
|
import { IsIn, IsInt, IsNotEmpty, IsOptional, IsString, IsUrl, Max, Min, validateSync } from 'class-validator';
|
||||||
|
|
||||||
|
/** `sslmode` values that encrypt the database connection (the pg driver treats them all as `verify-full`). */
|
||||||
|
const SECURE_SSL_MODES = ['require', 'verify-ca', 'verify-full'];
|
||||||
|
|
||||||
/** Environment variables read by the app. A missing or malformed required one stops the startup. */
|
/** Environment variables read by the app. A missing or malformed required one stops the startup. */
|
||||||
export class EnvironmentVariables {
|
export class EnvironmentVariables {
|
||||||
|
/** `production` enforces production-only rules (TLS to the database). */
|
||||||
|
@IsOptional()
|
||||||
|
@IsIn(['development', 'production', 'test'])
|
||||||
|
NODE_ENV?: string;
|
||||||
|
|
||||||
|
/** In production, `sslmode` must be require, verify-ca or verify-full: customer data must not travel in clear. */
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
DATABASE_URL!: string;
|
DATABASE_URL!: string;
|
||||||
@@ -36,6 +45,25 @@ export class EnvironmentVariables {
|
|||||||
@Min(1)
|
@Min(1)
|
||||||
@Max(10)
|
@Max(10)
|
||||||
TRUST_PROXY?: number;
|
TRUST_PROXY?: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seconds a shutting down instance waits for its running synchronization (default 25). Keep it below the grace period
|
||||||
|
* of the orchestrator (Docker `stop_grace_period`, Kubernetes `terminationGracePeriodSeconds`), which kills afterwards.
|
||||||
|
*/
|
||||||
|
@IsOptional()
|
||||||
|
@IsInt()
|
||||||
|
@Min(0)
|
||||||
|
@Max(3600)
|
||||||
|
SYNC_SHUTDOWN_TIMEOUT?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Whether the connection string asks for an encrypted connection. An unparsable one does not. */
|
||||||
|
function hasSecureSslMode(databaseUrl: string): boolean {
|
||||||
|
try {
|
||||||
|
return SECURE_SSL_MODES.includes(new URL(databaseUrl).searchParams.get('sslmode') ?? '');
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** `ConfigModule` validation hook: fails fast with the names of the faulty variables (never their values, they may be secrets). */
|
/** `ConfigModule` validation hook: fails fast with the names of the faulty variables (never their values, they may be secrets). */
|
||||||
@@ -43,9 +71,13 @@ export function validateEnv(config: Record<string, unknown>): Record<string, unk
|
|||||||
const validated = plainToInstance(EnvironmentVariables, config, { enableImplicitConversion: true });
|
const validated = plainToInstance(EnvironmentVariables, config, { enableImplicitConversion: true });
|
||||||
const errors = validateSync(validated, { whitelist: false });
|
const errors = validateSync(validated, { whitelist: false });
|
||||||
|
|
||||||
if (errors.length > 0) {
|
const details = errors.map((error) => `${error.property}: ${Object.values(error.constraints ?? {}).join(', ')}`);
|
||||||
const details = errors.map((error) => `${error.property}: ${Object.values(error.constraints ?? {}).join(', ')}`).join('\n ');
|
if (validated.NODE_ENV === 'production' && validated.DATABASE_URL && !hasSecureSslMode(validated.DATABASE_URL)) {
|
||||||
throw new Error(`Invalid environment configuration:\n ${details}`);
|
details.push(`DATABASE_URL: sslmode must be one of ${SECURE_SSL_MODES.join(', ')} in production`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (details.length > 0) {
|
||||||
|
throw new Error(`Invalid environment configuration:\n ${details.join('\n ')}`);
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,13 +23,20 @@ export class CustomerDatabaseRepository {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getAllHiboutikIds(): Promise<number[]> {
|
||||||
|
const customers = await this.prisma.customer.findMany({ select: { hiboutikId: true } });
|
||||||
|
return customers.map((customer) => customer.hiboutikId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the customers that no longer exist in Hiboutik.
|
* Deletes customers that no longer exist in Hiboutik.
|
||||||
* Their addresses are deleted (onDelete: Cascade), their orders are kept with no customer (onDelete: SetNull).
|
* Their addresses are deleted (onDelete: Cascade), their orders are kept with no customer (onDelete: SetNull).
|
||||||
*/
|
*/
|
||||||
async deleteMissing(keptHiboutikIds: number[]): Promise<number> {
|
async deleteByHiboutikIds(hiboutikIds: number[]): Promise<number> {
|
||||||
|
if (hiboutikIds.length === 0) return 0;
|
||||||
|
|
||||||
const result = await this.prisma.customer.deleteMany({
|
const result = await this.prisma.customer.deleteMany({
|
||||||
where: { hiboutikId: { notIn: keptHiboutikIds } },
|
where: { hiboutikId: { in: hiboutikIds } },
|
||||||
});
|
});
|
||||||
return result.count;
|
return result.count;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,19 @@ import { OrderStatus } from '../models/orderStatus';
|
|||||||
export class OrderDatabaseRepository {
|
export class OrderDatabaseRepository {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
|
||||||
/** Among `hiboutikSaleIds`, the ones already stored as completed orders. */
|
/**
|
||||||
async getCompletedSaleIds(hiboutikSaleIds: number[]): Promise<Set<number>> {
|
* Among `hiboutikSaleIds`, the ones already stored as completed orders of this customer. A completed order that lost
|
||||||
|
* its customer (customer deleted, then found again in Hiboutik) is not returned, so that the sync links it back.
|
||||||
|
*/
|
||||||
|
async getCompletedSaleIds(customerHiboutikId: number, hiboutikSaleIds: number[]): Promise<Set<number>> {
|
||||||
if (hiboutikSaleIds.length === 0) return new Set();
|
if (hiboutikSaleIds.length === 0) return new Set();
|
||||||
|
|
||||||
const orders = await this.prisma.order.findMany({
|
const orders = await this.prisma.order.findMany({
|
||||||
where: { hiboutikSaleId: { in: hiboutikSaleIds }, status: OrderStatus.COMPLETED },
|
where: {
|
||||||
|
hiboutikSaleId: { in: hiboutikSaleIds },
|
||||||
|
status: OrderStatus.COMPLETED,
|
||||||
|
customer: { is: { hiboutikId: customerHiboutikId } },
|
||||||
|
},
|
||||||
select: { hiboutikSaleId: true },
|
select: { hiboutikSaleId: true },
|
||||||
});
|
});
|
||||||
return new Set(orders.map((order) => order.hiboutikSaleId).filter((id): id is number => id !== null));
|
return new Set(orders.map((order) => order.hiboutikSaleId).filter((id): id is number => id !== null));
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PrismaService } from '../../prisma/prisma.service';
|
import { PrismaService } from '../../prisma/prisma.service';
|
||||||
import { Product } from '../../prisma/generated/client';
|
import { Prisma, Product } from '../../prisma/generated/client';
|
||||||
import { ProductCreateInput } from '../../prisma/generated/models/Product';
|
import { ProductCreateInput } from '../../prisma/generated/models/Product';
|
||||||
|
|
||||||
/** Database access to products. Rows are matched with Hiboutik by `hiboutikId`. */
|
/** Database access to products. Rows are matched with Hiboutik by `hiboutikId`. */
|
||||||
@@ -14,9 +14,12 @@ export class ProductDatabaseRepository {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Creates or updates the product with the same `hiboutikId`. On update, the relations missing from the input are unlinked. */
|
/**
|
||||||
async upsert(product: ProductCreateInput): Promise<Product> {
|
* Creates or updates the product with the same `hiboutikId`. On update, the relations missing from the input are unlinked.
|
||||||
return this.prisma.product.upsert({
|
* @param db transaction client, to run it inside a transaction.
|
||||||
|
*/
|
||||||
|
async upsert(product: ProductCreateInput, db: Prisma.TransactionClient = this.prisma): Promise<Product> {
|
||||||
|
return db.product.upsert({
|
||||||
where: { hiboutikId: product.hiboutikId },
|
where: { hiboutikId: product.hiboutikId },
|
||||||
create: product,
|
create: product,
|
||||||
update: {
|
update: {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PrismaService } from '../../prisma/prisma.service';
|
import { PrismaService } from '../../prisma/prisma.service';
|
||||||
import { ProductVersion } from '../../prisma/generated/client';
|
import { Prisma, ProductVersion } from '../../prisma/generated/client';
|
||||||
import { ProductVersionCreateInput } from '../../prisma/generated/models/ProductVersion';
|
import { ProductVersionCreateInput } from '../../prisma/generated/models/ProductVersion';
|
||||||
|
|
||||||
/** Database access to product versions (one per Hiboutik size), identified by their product and `hiboutikSizeId`. */
|
/** Database access to product versions (one per Hiboutik size), identified by their product and `hiboutikSizeId`. */
|
||||||
@@ -14,9 +14,16 @@ export class ProductVersionDatabaseRepository {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Creates or updates the version of `productId` with the same `hiboutikSizeId`. */
|
/**
|
||||||
async upsert(productId: string, productVersion: ProductVersionCreateInput): Promise<ProductVersion> {
|
* Creates or updates the version of `productId` with the same `hiboutikSizeId`.
|
||||||
return this.prisma.productVersion.upsert({
|
* @param db transaction client, to run it inside a transaction.
|
||||||
|
*/
|
||||||
|
async upsert(
|
||||||
|
productId: string,
|
||||||
|
productVersion: ProductVersionCreateInput,
|
||||||
|
db: Prisma.TransactionClient = this.prisma,
|
||||||
|
): Promise<ProductVersion> {
|
||||||
|
return db.productVersion.upsert({
|
||||||
where: { productId_hiboutikSizeId: { productId: productId, hiboutikSizeId: productVersion.hiboutikSizeId } },
|
where: { productId_hiboutikSizeId: { productId: productId, hiboutikSizeId: productVersion.hiboutikSizeId } },
|
||||||
create: productVersion,
|
create: productVersion,
|
||||||
update: productVersion,
|
update: productVersion,
|
||||||
@@ -26,9 +33,10 @@ export class ProductVersionDatabaseRepository {
|
|||||||
/**
|
/**
|
||||||
* Deletes the versions of a product that no longer exist in Hiboutik.
|
* Deletes the versions of a product that no longer exist in Hiboutik.
|
||||||
* Order lines keep their own snapshot of the product, their link is set to null.
|
* Order lines keep their own snapshot of the product, their link is set to null.
|
||||||
|
* @param db transaction client, to run it inside a transaction.
|
||||||
*/
|
*/
|
||||||
async deleteMissing(productId: string, keptHiboutikSizeIds: number[]): Promise<number> {
|
async deleteMissing(productId: string, keptHiboutikSizeIds: number[], db: Prisma.TransactionClient = this.prisma): Promise<number> {
|
||||||
const result = await this.prisma.productVersion.deleteMany({
|
const result = await db.productVersion.deleteMany({
|
||||||
where: { productId, hiboutikSizeId: { notIn: keptHiboutikSizeIds } },
|
where: { productId, hiboutikSizeId: { notIn: keptHiboutikSizeIds } },
|
||||||
});
|
});
|
||||||
return result.count;
|
return result.count;
|
||||||
|
|||||||
53
src/database/sitemap.database.repository.ts
Normal file
53
src/database/sitemap.database.repository.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { PrismaService } from '../../prisma/prisma.service';
|
||||||
|
|
||||||
|
/** Public page of the storefront, identified by its slug. */
|
||||||
|
export interface SitemapEntry {
|
||||||
|
slug: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Public product page, with the last change of its content in Hiboutik (null when Hiboutik does not give it). */
|
||||||
|
export interface ProductSitemapEntry extends SitemapEntry {
|
||||||
|
updatedAt: Date | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Sitemap {
|
||||||
|
brands: SitemapEntry[];
|
||||||
|
categories: SitemapEntry[];
|
||||||
|
products: ProductSitemapEntry[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Reads the public pages of the storefront, for its sitemap. */
|
||||||
|
@Injectable()
|
||||||
|
export class SitemapDatabaseRepository {
|
||||||
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Brands enabled, categories displayed on the website and products displayed on the website and not archived, that
|
||||||
|
* have a slug. Only products carry a date, their last update in Hiboutik: the `updatedAt` of the rows is rewritten by
|
||||||
|
* every sync, so it would give every page the date of the last sync, which search engines learn to ignore.
|
||||||
|
*/
|
||||||
|
async getSitemap(): Promise<Sitemap> {
|
||||||
|
const orderBy = { slug: 'asc' } as const;
|
||||||
|
const hasSlug = { slug: { not: null } };
|
||||||
|
const [brands, categories, products] = await Promise.all([
|
||||||
|
this.prisma.brand.findMany({ where: { ...hasSlug, enabled: true }, select: { slug: true }, orderBy }),
|
||||||
|
this.prisma.category.findMany({ where: { ...hasSlug, webEnabled: true }, select: { slug: true }, orderBy }),
|
||||||
|
this.prisma.product.findMany({
|
||||||
|
where: { ...hasSlug, enabled: true, archived: false },
|
||||||
|
select: { slug: true, hiboutikUpdatedAt: true },
|
||||||
|
orderBy,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
return {
|
||||||
|
brands: brands.map((row) => ({ slug: toSlug(row.slug) })),
|
||||||
|
categories: categories.map((row) => ({ slug: toSlug(row.slug) })),
|
||||||
|
products: products.map((row) => ({ slug: toSlug(row.slug), updatedAt: row.hiboutikUpdatedAt })),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The query filters out null slugs; the type does not know it. */
|
||||||
|
function toSlug(slug: string | null): string {
|
||||||
|
return slug as string;
|
||||||
|
}
|
||||||
57
src/database/slug.database.repository.spec.ts
Normal file
57
src/database/slug.database.repository.spec.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import { SlugEntityType } from '../../prisma/generated/client';
|
||||||
|
import { SlugDatabaseRepository } from './slug.database.repository';
|
||||||
|
|
||||||
|
describe('SlugDatabaseRepository.changeSlug', () => {
|
||||||
|
const tx = {
|
||||||
|
product: { findUnique: jest.fn(), update: jest.fn() },
|
||||||
|
slugRedirect: { findUnique: jest.fn(), delete: jest.fn(), create: jest.fn() },
|
||||||
|
};
|
||||||
|
const prisma = { $transaction: jest.fn((fn: (client: typeof tx) => unknown) => fn(tx)) };
|
||||||
|
let repository: SlugDatabaseRepository;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
repository = new SlugDatabaseRepository(prisma as never);
|
||||||
|
// product-a currently named "ate"; no product currently named with the new slug
|
||||||
|
tx.product.findUnique.mockImplementation(({ where }: { where: { id?: string } }) =>
|
||||||
|
Promise.resolve(where.id === 'product-a' ? { slug: 'ate' } : null),
|
||||||
|
);
|
||||||
|
tx.slugRedirect.findUnique.mockResolvedValue(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the former slug as a redirect', async () => {
|
||||||
|
await expect(repository.changeSlug(SlugEntityType.PRODUCT, 'product-a', 'ate-digipack')).resolves.toBe('CHANGED');
|
||||||
|
|
||||||
|
expect(tx.slugRedirect.delete).not.toHaveBeenCalled();
|
||||||
|
expect(tx.slugRedirect.create).toHaveBeenCalledWith({ data: { entityType: 'PRODUCT', slug: 'ate', productId: 'product-a' } });
|
||||||
|
expect(tx.product.update).toHaveBeenCalledWith({ where: { id: 'product-a' }, data: { slug: 'ate-digipack' } });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lets an entity take back one of its own former slugs', async () => {
|
||||||
|
tx.slugRedirect.findUnique.mockResolvedValue({ id: 'redirect-1', brandId: null, categoryId: null, productId: 'product-a' });
|
||||||
|
|
||||||
|
await expect(repository.changeSlug(SlugEntityType.PRODUCT, 'product-a', 'ate-old')).resolves.toBe('CHANGED');
|
||||||
|
|
||||||
|
expect(tx.slugRedirect.delete).toHaveBeenCalledWith({ where: { id: 'redirect-1' } });
|
||||||
|
expect(tx.product.update).toHaveBeenCalledWith({ where: { id: 'product-a' }, data: { slug: 'ate-old' } });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses another entity's former slug: its old URL keeps redirecting to it", async () => {
|
||||||
|
tx.slugRedirect.findUnique.mockResolvedValue({ id: 'redirect-1', brandId: null, categoryId: null, productId: 'product-b' });
|
||||||
|
|
||||||
|
await expect(repository.changeSlug(SlugEntityType.PRODUCT, 'product-a', 'maxident')).resolves.toBe('TAKEN');
|
||||||
|
|
||||||
|
expect(tx.slugRedirect.delete).not.toHaveBeenCalled();
|
||||||
|
expect(tx.slugRedirect.create).not.toHaveBeenCalled();
|
||||||
|
expect(tx.product.update).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses another entity's current slug", async () => {
|
||||||
|
tx.product.findUnique.mockImplementation(({ where }: { where: { id?: string; slug?: string } }) =>
|
||||||
|
Promise.resolve(where.id === 'product-a' ? { slug: 'ate' } : where.slug === 'maxident' ? { slug: 'maxident' } : null),
|
||||||
|
);
|
||||||
|
|
||||||
|
await expect(repository.changeSlug(SlugEntityType.PRODUCT, 'product-a', 'maxident')).resolves.toBe('TAKEN');
|
||||||
|
expect(tx.product.update).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
135
src/database/slug.database.repository.ts
Normal file
135
src/database/slug.database.repository.ts
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { PrismaService } from '../../prisma/prisma.service';
|
||||||
|
import { Prisma, SlugEntityType } from '../../prisma/generated/client';
|
||||||
|
|
||||||
|
/** Entity waiting for its first slug, with what is needed to build it. */
|
||||||
|
export interface SlugCandidateRow {
|
||||||
|
id: string;
|
||||||
|
hiboutikId: number;
|
||||||
|
name: string;
|
||||||
|
/** Name of the parent category (categories only), null for a root category or another entity type. */
|
||||||
|
parentName: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Outcome of {@link SlugDatabaseRepository.changeSlug}. */
|
||||||
|
export type ChangeSlugResult = 'CHANGED' | 'UNCHANGED' | 'NOT_FOUND' | 'TAKEN';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Database access to the slugs of brands, categories and products, and to the redirects of their former slugs.
|
||||||
|
* Slugs are unique per entity type: an entity's current slug and the former slugs of the other entities of the same
|
||||||
|
* type (redirects) share one namespace.
|
||||||
|
*/
|
||||||
|
@Injectable()
|
||||||
|
export class SlugDatabaseRepository {
|
||||||
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
|
||||||
|
/** Slugs that are in use for this type: current slugs and redirected former slugs. */
|
||||||
|
async getTakenSlugs(type: SlugEntityType): Promise<Set<string>> {
|
||||||
|
const where = { slug: { not: null } };
|
||||||
|
const select = { slug: true } as const;
|
||||||
|
const [current, redirects] = await Promise.all([
|
||||||
|
type === SlugEntityType.BRAND
|
||||||
|
? this.prisma.brand.findMany({ where, select })
|
||||||
|
: type === SlugEntityType.CATEGORY
|
||||||
|
? this.prisma.category.findMany({ where, select })
|
||||||
|
: this.prisma.product.findMany({ where, select }),
|
||||||
|
this.prisma.slugRedirect.findMany({ where: { entityType: type }, select }),
|
||||||
|
]);
|
||||||
|
return new Set([...current, ...redirects].map((row) => row.slug).filter((slug): slug is string => slug !== null));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entities of this type without a slug yet, oldest in Hiboutik first (so that it gets the plain slug).
|
||||||
|
* Categories come with the name of their parent (the parent's name, not its slug: a younger parent may have no slug yet,
|
||||||
|
* and an admin may have changed it, while the name keeps the generation deterministic).
|
||||||
|
*/
|
||||||
|
async getWithoutSlug(type: SlugEntityType): Promise<SlugCandidateRow[]> {
|
||||||
|
const where = { slug: null };
|
||||||
|
const orderBy = { hiboutikId: 'asc' } as const;
|
||||||
|
const select = { id: true, hiboutikId: true, name: true } as const;
|
||||||
|
if (type === SlugEntityType.CATEGORY) {
|
||||||
|
const rows = await this.prisma.category.findMany({ where, orderBy, select: { ...select, parent: { select: { name: true } } } });
|
||||||
|
return rows.map(({ parent, ...row }) => ({ ...row, parentName: parent?.name ?? null }));
|
||||||
|
}
|
||||||
|
const rows =
|
||||||
|
type === SlugEntityType.BRAND
|
||||||
|
? await this.prisma.brand.findMany({ where, orderBy, select })
|
||||||
|
: await this.prisma.product.findMany({ where, orderBy, select });
|
||||||
|
return rows.map((row) => ({ ...row, parentName: null }));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the first slug of an entity. Never overwrites an existing slug (e.g. one an admin set in the meantime).
|
||||||
|
* @returns false when the entity already had a slug or no longer exists.
|
||||||
|
*/
|
||||||
|
async setFirstSlug(type: SlugEntityType, id: string, slug: string): Promise<boolean> {
|
||||||
|
const args = { where: { id, slug: null }, data: { slug } };
|
||||||
|
const result =
|
||||||
|
type === SlugEntityType.BRAND
|
||||||
|
? await this.prisma.brand.updateMany(args)
|
||||||
|
: type === SlugEntityType.CATEGORY
|
||||||
|
? await this.prisma.category.updateMany(args)
|
||||||
|
: await this.prisma.product.updateMany(args);
|
||||||
|
return result.count === 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the slug of an entity, in one transaction: its former slug becomes a redirect to it.
|
||||||
|
* The new slug may be one of its own former slugs (that redirect is dropped, the slug naming it again), never another
|
||||||
|
* entity's former slug: that URL keeps redirecting to its entity (`TAKEN`).
|
||||||
|
* A concurrent write of the same slug makes the transaction fail on the unique constraint (Prisma error P2002).
|
||||||
|
*/
|
||||||
|
async changeSlug(type: SlugEntityType, id: string, newSlug: string): Promise<ChangeSlugResult> {
|
||||||
|
return this.prisma.$transaction(async (tx) => {
|
||||||
|
const entity = await this.findSlugOwner(tx, type, { id });
|
||||||
|
if (!entity) return 'NOT_FOUND';
|
||||||
|
if (entity.slug === newSlug) return 'UNCHANGED';
|
||||||
|
|
||||||
|
const owner = await this.findSlugOwner(tx, type, { slug: newSlug });
|
||||||
|
if (owner) return 'TAKEN';
|
||||||
|
|
||||||
|
const redirect = await tx.slugRedirect.findUnique({
|
||||||
|
where: { entityType_slug: { entityType: type, slug: newSlug } },
|
||||||
|
select: { id: true, brandId: true, categoryId: true, productId: true },
|
||||||
|
});
|
||||||
|
if (redirect) {
|
||||||
|
if ((redirect.brandId ?? redirect.categoryId ?? redirect.productId) !== id) return 'TAKEN';
|
||||||
|
await tx.slugRedirect.delete({ where: { id: redirect.id } });
|
||||||
|
}
|
||||||
|
if (entity.slug) {
|
||||||
|
await tx.slugRedirect.create({ data: { entityType: type, slug: entity.slug, ...this.redirectTarget(type, id) } });
|
||||||
|
}
|
||||||
|
const args = { where: { id }, data: { slug: newSlug } };
|
||||||
|
if (type === SlugEntityType.BRAND) await tx.brand.update(args);
|
||||||
|
else if (type === SlugEntityType.CATEGORY) await tx.category.update(args);
|
||||||
|
else await tx.product.update(args);
|
||||||
|
return 'CHANGED';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @returns the current slug of the entity a former slug redirects to, null when the slug is not a redirect. */
|
||||||
|
async findRedirect(type: SlugEntityType, slug: string): Promise<string | null> {
|
||||||
|
const redirect = await this.prisma.slugRedirect.findUnique({
|
||||||
|
where: { entityType_slug: { entityType: type, slug } },
|
||||||
|
select: { brand: { select: { slug: true } }, category: { select: { slug: true } }, product: { select: { slug: true } } },
|
||||||
|
});
|
||||||
|
return redirect?.brand?.slug ?? redirect?.category?.slug ?? redirect?.product?.slug ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async findSlugOwner(
|
||||||
|
db: Prisma.TransactionClient,
|
||||||
|
type: SlugEntityType,
|
||||||
|
where: { id: string } | { slug: string },
|
||||||
|
): Promise<{ slug: string | null } | null> {
|
||||||
|
const args = { where, select: { slug: true } };
|
||||||
|
if (type === SlugEntityType.BRAND) return db.brand.findUnique(args);
|
||||||
|
if (type === SlugEntityType.CATEGORY) return db.category.findUnique(args);
|
||||||
|
return db.product.findUnique(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private redirectTarget(type: SlugEntityType, id: string) {
|
||||||
|
if (type === SlugEntityType.BRAND) return { brandId: id };
|
||||||
|
if (type === SlugEntityType.CATEGORY) return { categoryId: id };
|
||||||
|
return { productId: id };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,7 +27,7 @@ describe('SyncLockDatabaseRepository', () => {
|
|||||||
const release = await repository.tryAcquire();
|
const release = await repository.tryAcquire();
|
||||||
|
|
||||||
expect(release).toEqual(expect.any(Function));
|
expect(release).toEqual(expect.any(Function));
|
||||||
expect(Client).toHaveBeenCalledWith({ connectionString: 'postgresql://user:pass@localhost:5432/db' });
|
expect(Client).toHaveBeenCalledWith({ connectionString: 'postgresql://user:pass@localhost:5432/db', connectionTimeoutMillis: 10_000 });
|
||||||
expect(client.query).toHaveBeenCalledWith('SELECT pg_try_advisory_lock($1) AS locked', [expect.any(Number)]);
|
expect(client.query).toHaveBeenCalledWith('SELECT pg_try_advisory_lock($1) AS locked', [expect.any(Number)]);
|
||||||
expect(client.end).not.toHaveBeenCalled();
|
expect(client.end).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ export class SyncLockDatabaseRepository {
|
|||||||
|
|
||||||
/** @returns the function releasing the lock, or `null` if another instance (or process) already holds it. */
|
/** @returns the function releasing the lock, or `null` if another instance (or process) already holds it. */
|
||||||
async tryAcquire(): Promise<ReleaseLock | null> {
|
async tryAcquire(): Promise<ReleaseLock | null> {
|
||||||
const client = new Client({ connectionString: this.configService.get<string>('DATABASE_URL') });
|
// Fails fast instead of holding the POST /sync request until its 30 s timeout when the database does not answer
|
||||||
|
const client = new Client({ connectionString: this.configService.get<string>('DATABASE_URL'), connectionTimeoutMillis: 10_000 });
|
||||||
// A lost connection means Postgres released the lock: log it instead of crashing the process on an unhandled 'error'
|
// A lost connection means Postgres released the lock: log it instead of crashing the process on an unhandled 'error'
|
||||||
client.on('error', (error) => this.logger.error(`Synchronization lock connection error: ${error.message}`));
|
client.on('error', (error) => this.logger.error(`Synchronization lock connection error: ${error.message}`));
|
||||||
await client.connect();
|
await client.connect();
|
||||||
|
|||||||
56
src/database/throttlerStorage.database.repository.spec.ts
Normal file
56
src/database/throttlerStorage.database.repository.spec.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import { Logger } from '@nestjs/common';
|
||||||
|
import { ThrottlerDatabaseStorage } from './throttlerStorage.database.repository';
|
||||||
|
|
||||||
|
// The SQL itself (window, block, reset) was checked on a real Postgres engine; these tests cover the TypeScript side.
|
||||||
|
describe('ThrottlerDatabaseStorage', () => {
|
||||||
|
const record = { totalHits: 2, timeToExpire: 40, isBlocked: false, timeToBlockExpire: 0 };
|
||||||
|
const prisma = { $queryRaw: jest.fn(), throttlerRecord: { deleteMany: jest.fn() } };
|
||||||
|
let logError: jest.SpyInstance;
|
||||||
|
let random: jest.Mock;
|
||||||
|
let storage: ThrottlerDatabaseStorage;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
logError = jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
|
||||||
|
prisma.$queryRaw.mockResolvedValue([record]);
|
||||||
|
prisma.throttlerRecord.deleteMany.mockResolvedValue({ count: 0 });
|
||||||
|
random = jest.fn().mockReturnValue(0.5);
|
||||||
|
storage = new ThrottlerDatabaseStorage(prisma as never, random);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the record computed by the database, with parameters bound (never interpolated)', async () => {
|
||||||
|
await expect(storage.increment('key-hash', 60_000, 5, 30_000)).resolves.toEqual(record);
|
||||||
|
|
||||||
|
const [strings, ...values] = prisma.$queryRaw.mock.calls[0] as [TemplateStringsArray, ...unknown[]];
|
||||||
|
expect(values).toEqual(['key-hash', 60_000, 5, 30_000]);
|
||||||
|
expect(strings.join('?')).not.toContain('key-hash');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('purges the finished counters on about one request in 100', async () => {
|
||||||
|
await storage.increment('k', 60_000, 5, 60_000);
|
||||||
|
expect(prisma.throttlerRecord.deleteMany).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
random.mockReturnValue(0.001);
|
||||||
|
await storage.increment('k', 60_000, 5, 60_000);
|
||||||
|
expect(prisma.throttlerRecord.deleteMany).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lets the request through when the database fails, logging at most once a minute', async () => {
|
||||||
|
prisma.$queryRaw.mockRejectedValue(new Error('connection refused'));
|
||||||
|
|
||||||
|
await expect(storage.increment('k', 60_000, 5, 60_000)).resolves.toMatchObject({ isBlocked: false });
|
||||||
|
await storage.increment('k', 60_000, 5, 60_000);
|
||||||
|
|
||||||
|
expect(logError).toHaveBeenCalledTimes(1);
|
||||||
|
expect(logError).toHaveBeenCalledWith(expect.stringContaining('connection refused'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not fail the request when the purge fails', async () => {
|
||||||
|
random.mockReturnValue(0);
|
||||||
|
prisma.throttlerRecord.deleteMany.mockRejectedValue(new Error('boom'));
|
||||||
|
|
||||||
|
await expect(storage.increment('k', 60_000, 5, 60_000)).resolves.toEqual(record);
|
||||||
|
await new Promise((resolve) => setImmediate(resolve));
|
||||||
|
expect(logError).toHaveBeenCalledWith(expect.stringContaining('boom'));
|
||||||
|
});
|
||||||
|
});
|
||||||
90
src/database/throttlerStorage.database.repository.ts
Normal file
90
src/database/throttlerStorage.database.repository.ts
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import { Logger } from '@nestjs/common';
|
||||||
|
import { ThrottlerStorage } from '@nestjs/throttler';
|
||||||
|
import { PrismaService } from '../../prisma/prisma.service';
|
||||||
|
|
||||||
|
/** Answer of {@link ThrottlerStorage.increment} (its type is not exported by the package root). */
|
||||||
|
type ThrottlerStorageRecord = Awaited<ReturnType<ThrottlerStorage['increment']>>;
|
||||||
|
|
||||||
|
/** Share of the requests that also purge the finished counters (about one in 100). */
|
||||||
|
const PURGE_PROBABILITY = 0.01;
|
||||||
|
/** At most one log line per minute while the database is unreachable (one request = one failure). */
|
||||||
|
const ERROR_LOG_INTERVAL_MS = 60_000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rate limiting counters stored in PostgreSQL (`throttler_records`), so that every instance shares them: with an
|
||||||
|
* in-memory storage, each instance would count apart and N instances would allow N times the limit.
|
||||||
|
*
|
||||||
|
* One fixed window per key: the first request opens a window of `ttl`; past `limit` requests in it, the key is blocked
|
||||||
|
* for `blockDuration`, then a new window starts. Each request is a single atomic upsert, computed with the database
|
||||||
|
* clock (`now()`), so the instances need neither a lock nor synchronized clocks.
|
||||||
|
*
|
||||||
|
* Fails open: if the database cannot be reached, the request is let through (and the failure logged) rather than
|
||||||
|
* answered with a 500. Every endpoint needs the database anyway.
|
||||||
|
*/
|
||||||
|
export class ThrottlerDatabaseStorage implements ThrottlerStorage {
|
||||||
|
private readonly logger = new Logger('ThrottlerDatabaseStorage');
|
||||||
|
private lastErrorLogAt = 0;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly prisma: PrismaService,
|
||||||
|
private readonly random: () => number = Math.random,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/** @param ttl window length (ms) @param blockDuration block length once the limit is exceeded (ms) */
|
||||||
|
async increment(key: string, ttl: number, limit: number, blockDuration: number): Promise<ThrottlerStorageRecord> {
|
||||||
|
try {
|
||||||
|
const [record] = await this.prisma.$queryRaw<ThrottlerStorageRecord[]>`
|
||||||
|
INSERT INTO throttler_records AS r (key, hits, "expiresAt", "blockedUntil")
|
||||||
|
VALUES (${key}, 1, now() + ${ttl}::int * interval '1 millisecond', NULL)
|
||||||
|
ON CONFLICT (key) DO UPDATE SET
|
||||||
|
hits = CASE
|
||||||
|
WHEN r."blockedUntil" > now() THEN r.hits
|
||||||
|
WHEN r."expiresAt" <= now() OR r."blockedUntil" IS NOT NULL THEN 1
|
||||||
|
ELSE r.hits + 1
|
||||||
|
END,
|
||||||
|
"expiresAt" = CASE
|
||||||
|
WHEN r."blockedUntil" > now() THEN r."expiresAt"
|
||||||
|
WHEN r."expiresAt" <= now() OR r."blockedUntil" IS NOT NULL THEN EXCLUDED."expiresAt"
|
||||||
|
ELSE r."expiresAt"
|
||||||
|
END,
|
||||||
|
"blockedUntil" = CASE
|
||||||
|
WHEN r."blockedUntil" > now() THEN r."blockedUntil"
|
||||||
|
WHEN r."expiresAt" <= now() OR r."blockedUntil" IS NOT NULL THEN NULL
|
||||||
|
WHEN r.hits + 1 > ${limit}::int THEN now() + ${blockDuration}::int * interval '1 millisecond'
|
||||||
|
ELSE NULL
|
||||||
|
END
|
||||||
|
RETURNING
|
||||||
|
hits AS "totalHits",
|
||||||
|
GREATEST(0, CEIL(EXTRACT(EPOCH FROM ("expiresAt" - now()))))::int AS "timeToExpire",
|
||||||
|
COALESCE("blockedUntil" > now(), false) AS "isBlocked",
|
||||||
|
COALESCE(GREATEST(0, CEIL(EXTRACT(EPOCH FROM ("blockedUntil" - now())))), 0)::int AS "timeToBlockExpire"`;
|
||||||
|
|
||||||
|
if (this.random() < PURGE_PROBABILITY) void this.purgeFinished();
|
||||||
|
return record;
|
||||||
|
} catch (error) {
|
||||||
|
this.logFailure('Rate limiting storage unavailable, request let through', error);
|
||||||
|
return { totalHits: 0, timeToExpire: 0, isBlocked: false, timeToBlockExpire: 0 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Deletes the counters whose window and block are both over (idempotent: every instance may run it). */
|
||||||
|
private async purgeFinished(): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.prisma.throttlerRecord.deleteMany({
|
||||||
|
where: {
|
||||||
|
expiresAt: { lt: new Date() },
|
||||||
|
OR: [{ blockedUntil: null }, { blockedUntil: { lt: new Date() } }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
this.logFailure('Failed to purge the finished rate limiting counters', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private logFailure(message: string, error: unknown): void {
|
||||||
|
const now = Date.now();
|
||||||
|
if (now - this.lastErrorLogAt < ERROR_LOG_INTERVAL_MS) return;
|
||||||
|
this.lastErrorLogAt = now;
|
||||||
|
this.logger.error(`${message}: ${(error as Error)?.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { HiboutikClient } from './hiboutik.client';
|
import { HiboutikClient } from './hiboutik.client';
|
||||||
import { BrandHiboutikDto } from '../models/hiboutikDTO/brand.hiboutik.dto';
|
import { BrandHiboutikDto, brandHiboutikSchema } from '../models/hiboutikDTO/brand.hiboutik.dto';
|
||||||
|
|
||||||
/** Reads brands from the Hiboutik API. */
|
/** Reads brands from the Hiboutik API. */
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BrandHiboutikRepository {
|
export class BrandHiboutikRepository {
|
||||||
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
||||||
|
|
||||||
|
/** Walked twice when it spans several pages: it drives the deletion of brands (see `getAllPagesForSync`). */
|
||||||
async getBrands(): Promise<BrandHiboutikDto[]> {
|
async getBrands(): Promise<BrandHiboutikDto[]> {
|
||||||
return this.hiboutikClient.getAllPages<BrandHiboutikDto>('/brands', {
|
return this.hiboutikClient.getAllPagesForSync('/brands', brandHiboutikSchema, (brand) => brand.brand_id, {
|
||||||
order_by: 'brand_id',
|
order_by: 'brand_id',
|
||||||
sort: 'ASC',
|
sort: 'ASC',
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { CategoryHiboutikDto } from '../models/hiboutikDTO/category.hiboutik.dto';
|
import { CategoryHiboutikDto, categoryHiboutikSchema } from '../models/hiboutikDTO/category.hiboutik.dto';
|
||||||
import { HiboutikClient } from './hiboutik.client';
|
import { HiboutikClient } from './hiboutik.client';
|
||||||
|
|
||||||
/** Reads categories from the Hiboutik API. */
|
/** Reads categories from the Hiboutik API. */
|
||||||
@@ -7,10 +7,13 @@ import { HiboutikClient } from './hiboutik.client';
|
|||||||
export class CategoryHiboutikRepository {
|
export class CategoryHiboutikRepository {
|
||||||
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
||||||
|
|
||||||
/** Sorted by parent id, which does not guarantee that a parent comes before its children. */
|
/**
|
||||||
|
* Sorted by id (unique, so that the pages are stable), a parent may come after its children. Walked twice when it
|
||||||
|
* spans several pages: it drives the deletion of categories (see `getAllPagesForSync`).
|
||||||
|
*/
|
||||||
async getCategories(): Promise<CategoryHiboutikDto[]> {
|
async getCategories(): Promise<CategoryHiboutikDto[]> {
|
||||||
return this.hiboutikClient.getAllPages<CategoryHiboutikDto>('/categories', {
|
return this.hiboutikClient.getAllPagesForSync('/categories', categoryHiboutikSchema, (category) => category.category_id, {
|
||||||
order_by: 'category_id_parent',
|
order_by: 'category_id',
|
||||||
sort: 'ASC',
|
sort: 'ASC',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,11 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { HiboutikClient } from './hiboutik.client';
|
import { HiboutikClient } from './hiboutik.client';
|
||||||
import {
|
import {
|
||||||
CustomerAddressDetailsHiboutikDto,
|
CustomerAddressDetailsHiboutikDto,
|
||||||
|
customerAddressDetailsHiboutikSchema,
|
||||||
CustomerHiboutikDto,
|
CustomerHiboutikDto,
|
||||||
|
customerHiboutikSchema,
|
||||||
FullCustomerHiboutikDto,
|
FullCustomerHiboutikDto,
|
||||||
|
fullCustomerHiboutikSchema,
|
||||||
} from '../models/hiboutikDTO/customer.hiboutik.dto';
|
} from '../models/hiboutikDTO/customer.hiboutik.dto';
|
||||||
|
|
||||||
/** Reads customers from the Hiboutik API. */
|
/** Reads customers from the Hiboutik API. */
|
||||||
@@ -11,29 +14,39 @@ import {
|
|||||||
export class CustomerHiboutikRepository {
|
export class CustomerHiboutikRepository {
|
||||||
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
||||||
|
|
||||||
/** Customer summaries only: use {@link getCustomerById} for addresses and sales. */
|
/**
|
||||||
|
* Customer summaries only: use {@link getCustomerById} for addresses and sales. Sorted by id so that the pages are
|
||||||
|
* stable, but a customer created or deleted while they are walked can still shift the following ones.
|
||||||
|
*/
|
||||||
async getCustomers(): Promise<CustomerHiboutikDto[]> {
|
async getCustomers(): Promise<CustomerHiboutikDto[]> {
|
||||||
return this.hiboutikClient.getAllPages<CustomerHiboutikDto>('/customers');
|
return this.hiboutikClient.getAllPages('/customers', customerHiboutikSchema, {
|
||||||
|
order_by: 'customers_id',
|
||||||
|
sort: 'ASC',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Full customer, with its addresses and sales (Hiboutik answers with a one-element array).
|
* Full customer, with its addresses and sales.
|
||||||
* @throws if the customer does not exist.
|
* @returns `null` when Hiboutik says the customer does not exist (404 or empty answer).
|
||||||
|
* @throws on any other failure: an unknown state is never taken for a deleted customer.
|
||||||
*/
|
*/
|
||||||
|
async findCustomerById(id: number): Promise<FullCustomerHiboutikDto | null> {
|
||||||
|
return this.hiboutikClient.getOne(`/customer/${id}`, fullCustomerHiboutikSchema);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Same as {@link findCustomerById}, but a missing customer throws. */
|
||||||
async getCustomerById(id: number): Promise<FullCustomerHiboutikDto> {
|
async getCustomerById(id: number): Promise<FullCustomerHiboutikDto> {
|
||||||
const data = await this.hiboutikClient.get<FullCustomerHiboutikDto[]>('/customer/' + String(id));
|
const customer = await this.findCustomerById(id);
|
||||||
const customer = data[0];
|
|
||||||
if (!customer) throw new Error(`Hiboutik customer ${id} not found`);
|
if (!customer) throw new Error(`Hiboutik customer ${id} not found`);
|
||||||
return customer;
|
return customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Address with its company details (Hiboutik answers with a one-element array).
|
* Address with its company details.
|
||||||
* @throws if the address does not exist.
|
* @throws if the address does not exist.
|
||||||
*/
|
*/
|
||||||
async getAddressById(id: number): Promise<CustomerAddressDetailsHiboutikDto> {
|
async getAddressById(id: number): Promise<CustomerAddressDetailsHiboutikDto> {
|
||||||
const data = await this.hiboutikClient.get<CustomerAddressDetailsHiboutikDto[]>('/customers_addresses/' + String(id));
|
const address = await this.hiboutikClient.getOne(`/customers_addresses/${id}`, customerAddressDetailsHiboutikSchema);
|
||||||
const address = data[0];
|
|
||||||
if (!address) throw new Error(`Hiboutik address ${id} not found`);
|
if (!address) throw new Error(`Hiboutik address ${id} not found`);
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,26 @@
|
|||||||
|
import { AxiosError, AxiosHeaders } from 'axios';
|
||||||
import { of, throwError } from 'rxjs';
|
import { of, throwError } from 'rxjs';
|
||||||
import { HiboutikClient } from './hiboutik.client';
|
import { z } from 'zod';
|
||||||
|
import { HiboutikClient, HiboutikRateLimitError } from './hiboutik.client';
|
||||||
|
|
||||||
describe('HiboutikClient', () => {
|
describe('HiboutikClient', () => {
|
||||||
const httpService = { get: jest.fn() };
|
const httpService = { get: jest.fn() };
|
||||||
const configService = { get: jest.fn((key: string) => (key === 'HIBOUTIK_BASE_URL' ? 'https://shop.hiboutik.test/api' : 'x')) };
|
const configService = { get: jest.fn((key: string) => (key === 'HIBOUTIK_BASE_URL' ? 'https://shop.hiboutik.test/api' : 'x')) };
|
||||||
|
const schema = z.object({ id: z.coerce.number() });
|
||||||
let client: HiboutikClient;
|
let client: HiboutikClient;
|
||||||
|
|
||||||
const answer = (data: unknown) => of({ data });
|
const answer = (data: unknown) => of({ data });
|
||||||
|
const httpError = (status: number) =>
|
||||||
|
throwError(
|
||||||
|
() =>
|
||||||
|
new AxiosError(`Request failed with status code ${status}`, undefined, undefined, undefined, {
|
||||||
|
status,
|
||||||
|
statusText: '',
|
||||||
|
headers: {},
|
||||||
|
config: { headers: new AxiosHeaders() },
|
||||||
|
data: null,
|
||||||
|
}),
|
||||||
|
);
|
||||||
const page = (size: number) => Array.from({ length: size }, (_, index) => ({ id: index }));
|
const page = (size: number) => Array.from({ length: size }, (_, index) => ({ id: index }));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -18,30 +32,42 @@ describe('HiboutikClient', () => {
|
|||||||
it('returns the list of a 2xx answer, empty or not', async () => {
|
it('returns the list of a 2xx answer, empty or not', async () => {
|
||||||
httpService.get.mockReturnValueOnce(answer([{ id: 1 }])).mockReturnValueOnce(answer([]));
|
httpService.get.mockReturnValueOnce(answer([{ id: 1 }])).mockReturnValueOnce(answer([]));
|
||||||
|
|
||||||
await expect(client.getList('/x')).resolves.toEqual([{ id: 1 }]);
|
await expect(client.getList('/x', schema)).resolves.toEqual([{ id: 1 }]);
|
||||||
await expect(client.getList('/x')).resolves.toEqual([]);
|
await expect(client.getList('/x', schema)).resolves.toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('treats a 2xx answer without body as an empty list', async () => {
|
it('treats a 2xx answer without body as an empty list', async () => {
|
||||||
httpService.get.mockReturnValueOnce(answer('')).mockReturnValueOnce(answer(null));
|
httpService.get.mockReturnValueOnce(answer('')).mockReturnValueOnce(answer(null));
|
||||||
|
|
||||||
await expect(client.getList('/x')).resolves.toEqual([]);
|
await expect(client.getList('/x', schema)).resolves.toEqual([]);
|
||||||
await expect(client.getList('/x')).resolves.toEqual([]);
|
await expect(client.getList('/x', schema)).resolves.toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rejects a 2xx answer that is not a list, without leaking its content', async () => {
|
it('rejects a 2xx answer that is not a list, without leaking its content', async () => {
|
||||||
httpService.get.mockReturnValueOnce(answer({ error: 'secret details' }));
|
httpService.get.mockReturnValueOnce(answer({ error: 'secret details' }));
|
||||||
|
|
||||||
const result = client.getList('/x');
|
const result = client.getList('/x', schema);
|
||||||
|
|
||||||
await expect(result).rejects.toThrow('a list was expected');
|
await expect(result).rejects.toThrow('a list was expected');
|
||||||
await expect(result).rejects.not.toThrow('secret details');
|
await expect(result).rejects.not.toThrow('secret details');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('lets HTTP errors through', async () => {
|
it('validates and normalizes each item', async () => {
|
||||||
httpService.get.mockReturnValueOnce(throwError(() => new Error('Request failed with status code 500')));
|
httpService.get.mockReturnValueOnce(answer([{ id: '1', extra: true }]));
|
||||||
|
|
||||||
await expect(client.getList('/x')).rejects.toThrow('500');
|
await expect(client.getList('/x', schema)).resolves.toEqual([{ id: 1 }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects the whole list when an item is invalid, so that nothing is deleted', async () => {
|
||||||
|
httpService.get.mockReturnValueOnce(answer([{ id: 1 }, { id: 'abc' }]));
|
||||||
|
|
||||||
|
await expect(client.getList('/x', schema)).rejects.toThrow('Unexpected answer from Hiboutik /x: invalid 1.id');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lets HTTP errors through', async () => {
|
||||||
|
httpService.get.mockReturnValueOnce(httpError(500));
|
||||||
|
|
||||||
|
await expect(client.getList('/x', schema)).rejects.toThrow('500');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -49,18 +75,140 @@ describe('HiboutikClient', () => {
|
|||||||
it('walks the pages until a short one', async () => {
|
it('walks the pages until a short one', async () => {
|
||||||
httpService.get.mockReturnValueOnce(answer(page(250))).mockReturnValueOnce(answer(page(10)));
|
httpService.get.mockReturnValueOnce(answer(page(250))).mockReturnValueOnce(answer(page(10)));
|
||||||
|
|
||||||
const results = await client.getAllPages('/x');
|
const results = await client.getAllPages('/x', schema);
|
||||||
|
|
||||||
expect(results).toHaveLength(260);
|
expect(results).toHaveLength(260);
|
||||||
expect(httpService.get).toHaveBeenCalledTimes(2);
|
expect(httpService.get).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fails as a whole when a later page fails, instead of returning a truncated list', async () => {
|
it('fails as a whole when a later page fails, instead of returning a truncated list', async () => {
|
||||||
|
httpService.get.mockReturnValueOnce(answer(page(250))).mockReturnValueOnce(httpError(502));
|
||||||
|
|
||||||
|
await expect(client.getAllPages('/x', schema)).rejects.toThrow('502');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getAllPagesForSync', () => {
|
||||||
|
const idOf = (item: { id: number }) => item.id;
|
||||||
|
const ids = (from: number, to: number) => Array.from({ length: to - from + 1 }, (_, index) => ({ id: from + index }));
|
||||||
|
|
||||||
|
it('walks a list that fits in one page only once', async () => {
|
||||||
|
httpService.get.mockReturnValueOnce(answer(page(10)));
|
||||||
|
|
||||||
|
await expect(client.getAllPagesForSync('/x', schema, idOf)).resolves.toHaveLength(10);
|
||||||
|
expect(httpService.get).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('walks a multi-page list twice and keeps an item skipped by a page shift in one walk', async () => {
|
||||||
|
// First walk: item 10 is deleted right after page 1 is read, so item 250 moves to page 1 and page 2 starts at 251
|
||||||
|
httpService.get.mockReturnValueOnce(answer(ids(0, 249))).mockReturnValueOnce(answer(ids(251, 260)));
|
||||||
|
// Second walk: the list without item 10
|
||||||
|
const withoutTen = ids(0, 250).filter(({ id }) => id !== 10);
|
||||||
|
httpService.get.mockReturnValueOnce(answer(withoutTen)).mockReturnValueOnce(answer(ids(251, 260)));
|
||||||
|
|
||||||
|
const results = await client.getAllPagesForSync('/x', schema, idOf);
|
||||||
|
|
||||||
|
expect(httpService.get).toHaveBeenCalledTimes(4);
|
||||||
|
// Item 250 is kept (it would otherwise be deleted); item 10 is only deleted at the next sync
|
||||||
|
expect(results.map(idOf).sort((a, b) => a - b)).toEqual(ids(0, 260).map(idOf));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails as a whole when the second walk fails', async () => {
|
||||||
httpService.get
|
httpService.get
|
||||||
.mockReturnValueOnce(answer(page(250)))
|
.mockReturnValueOnce(answer(page(250)))
|
||||||
.mockReturnValueOnce(throwError(() => new Error('Request failed with status code 502')));
|
.mockReturnValueOnce(answer(page(10)))
|
||||||
|
.mockReturnValueOnce(httpError(502));
|
||||||
|
|
||||||
await expect(client.getAllPages('/x')).rejects.toThrow('502');
|
await expect(client.getAllPagesForSync('/x', schema, idOf)).rejects.toThrow('502');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('rate limit (429)', () => {
|
||||||
|
let sleep: jest.SpyInstance;
|
||||||
|
const tooManyRequests = (retryAfter?: string) =>
|
||||||
|
throwError(
|
||||||
|
() =>
|
||||||
|
new AxiosError('Request failed with status code 429', undefined, undefined, undefined, {
|
||||||
|
status: 429,
|
||||||
|
statusText: '',
|
||||||
|
headers: retryAfter === undefined ? {} : { 'retry-after': retryAfter },
|
||||||
|
config: { headers: new AxiosHeaders() },
|
||||||
|
data: null,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
sleep = jest.spyOn(client as unknown as { sleep: (ms: number) => Promise<void> }, 'sleep').mockResolvedValue(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('retries with exponential backoff, then succeeds', async () => {
|
||||||
|
httpService.get
|
||||||
|
.mockReturnValueOnce(tooManyRequests())
|
||||||
|
.mockReturnValueOnce(tooManyRequests())
|
||||||
|
.mockReturnValueOnce(answer([{ id: 1 }]));
|
||||||
|
|
||||||
|
await expect(client.getList('/x', schema)).resolves.toEqual([{ id: 1 }]);
|
||||||
|
expect(sleep.mock.calls).toEqual([[2_000], [4_000]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('waits as asked by Retry-After, capped at 60 s', async () => {
|
||||||
|
httpService.get
|
||||||
|
.mockReturnValueOnce(tooManyRequests('5'))
|
||||||
|
.mockReturnValueOnce(tooManyRequests('3600'))
|
||||||
|
.mockReturnValueOnce(answer([]));
|
||||||
|
|
||||||
|
await client.getList('/x', schema);
|
||||||
|
|
||||||
|
expect(sleep.mock.calls).toEqual([[5_000], [60_000]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gives up with a HiboutikRateLimitError after 3 retries', async () => {
|
||||||
|
httpService.get.mockReturnValue(tooManyRequests());
|
||||||
|
|
||||||
|
await expect(client.getList('/x', schema)).rejects.toThrow(HiboutikRateLimitError);
|
||||||
|
expect(httpService.get).toHaveBeenCalledTimes(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not retry other errors', async () => {
|
||||||
|
httpService.get.mockReturnValueOnce(httpError(500));
|
||||||
|
|
||||||
|
await expect(client.getList('/x', schema)).rejects.toThrow('500');
|
||||||
|
expect(sleep).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not take a rate limited single resource for a missing one', async () => {
|
||||||
|
httpService.get.mockReturnValue(tooManyRequests());
|
||||||
|
|
||||||
|
await expect(client.getOne('/x/7', schema)).rejects.toThrow(HiboutikRateLimitError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getOne', () => {
|
||||||
|
it('returns the single element of the answer', async () => {
|
||||||
|
httpService.get.mockReturnValueOnce(answer([{ id: '7' }]));
|
||||||
|
|
||||||
|
await expect(client.getOne('/x/7', schema)).resolves.toEqual({ id: 7 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null when Hiboutik says the resource does not exist (404 or empty answer)', async () => {
|
||||||
|
httpService.get.mockReturnValueOnce(httpError(404)).mockReturnValueOnce(answer([])).mockReturnValueOnce(answer(''));
|
||||||
|
|
||||||
|
await expect(client.getOne('/x/7', schema)).resolves.toBeNull();
|
||||||
|
await expect(client.getOne('/x/7', schema)).resolves.toBeNull();
|
||||||
|
await expect(client.getOne('/x/7', schema)).resolves.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws on any other failure: an unknown state is never taken for a missing resource', async () => {
|
||||||
|
httpService.get
|
||||||
|
.mockReturnValueOnce(httpError(500))
|
||||||
|
.mockReturnValueOnce(httpError(401))
|
||||||
|
.mockReturnValueOnce(throwError(() => new Error('timeout of 30000ms exceeded')))
|
||||||
|
.mockReturnValueOnce(answer({ error: 'x' }));
|
||||||
|
|
||||||
|
await expect(client.getOne('/x/7', schema)).rejects.toThrow('500');
|
||||||
|
await expect(client.getOne('/x/7', schema)).rejects.toThrow('401');
|
||||||
|
await expect(client.getOne('/x/7', schema)).rejects.toThrow('timeout');
|
||||||
|
await expect(client.getOne('/x/7', schema)).rejects.toThrow('a list was expected');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,12 +1,49 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { HttpService } from '@nestjs/axios';
|
import { HttpService } from '@nestjs/axios';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { isAxiosError } from 'axios';
|
||||||
import { firstValueFrom } from 'rxjs';
|
import { firstValueFrom } from 'rxjs';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { parseHiboutik } from '../models/hiboutikDTO/hiboutik.schema';
|
||||||
|
|
||||||
/** Results per page of every paginated Hiboutik endpoint (see "Results per page" in the API docs). */
|
/** Results per page of every paginated Hiboutik endpoint (see "Results per page" in the API docs). */
|
||||||
const HIBOUTIK_PAGE_SIZE = 250;
|
const HIBOUTIK_PAGE_SIZE = 250;
|
||||||
|
|
||||||
/** Low-level HTTP client for the Hiboutik REST API: base URL and Basic auth credentials come from the environment. */
|
/** Retries of a request answered 429 (too many requests), with exponential backoff: 2 s, 4 s, 8 s. */
|
||||||
|
const RATE_LIMIT_RETRIES = 3;
|
||||||
|
const RATE_LIMIT_BASE_DELAY_MS = 2_000;
|
||||||
|
/** Cap of the wait asked by a `Retry-After` header: beyond, waiting holds the sync for nothing (monthly quota). */
|
||||||
|
const RATE_LIMIT_MAX_DELAY_MS = 60_000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hiboutik still answers 429 after the retries: the rate limit (30 concurrent calls) or the monthly quota (10,000
|
||||||
|
* calls per shop) is reached. The synchronization must stop: every next call would fail the same way.
|
||||||
|
*/
|
||||||
|
export class HiboutikRateLimitError extends Error {
|
||||||
|
constructor(path: string) {
|
||||||
|
super(`Hiboutik rate limit reached on ${path} (still 429 after ${RATE_LIMIT_RETRIES} retries): monthly quota exhausted?`);
|
||||||
|
this.name = 'HiboutikRateLimitError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Rethrows a {@link HiboutikRateLimitError}: for the per-item `catch` blocks of the sync, which must not swallow it. */
|
||||||
|
export function rethrowIfRateLimited(error: unknown): void {
|
||||||
|
if (error instanceof HiboutikRateLimitError) throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Wait asked by a `Retry-After` header (seconds or HTTP date), if any. */
|
||||||
|
function retryAfterMs(header: unknown): number | undefined {
|
||||||
|
if (typeof header !== 'string' || header.trim() === '') return undefined;
|
||||||
|
const seconds = Number(header);
|
||||||
|
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1000);
|
||||||
|
const date = Date.parse(header);
|
||||||
|
return Number.isNaN(date) ? undefined : Math.max(0, date - Date.now());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Low-level HTTP client for the Hiboutik REST API: base URL and Basic auth credentials come from the environment.
|
||||||
|
* Every answer is validated against a zod schema (see `hiboutik.schema.ts`): a malformed one throws, like an HTTP error.
|
||||||
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HiboutikClient {
|
export class HiboutikClient {
|
||||||
private readonly baseUrl: string;
|
private readonly baseUrl: string;
|
||||||
@@ -22,45 +59,109 @@ export class HiboutikClient {
|
|||||||
this.baseUrl = this.configService.get<string>('HIBOUTIK_BASE_URL') ?? '';
|
this.baseUrl = this.configService.get<string>('HIBOUTIK_BASE_URL') ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/** GET on a Hiboutik path (relative to `HIBOUTIK_BASE_URL`), returns the response body. */
|
/**
|
||||||
async get<T>(path: string, params?: Record<string, unknown>): Promise<T> {
|
* GET on a Hiboutik path (relative to `HIBOUTIK_BASE_URL`), returns the raw response body. A 429 is retried with
|
||||||
const response = await firstValueFrom(
|
* backoff (or after the `Retry-After` delay), then turns into a {@link HiboutikRateLimitError}.
|
||||||
this.httpService.get<T>(`${this.baseUrl}${path}`, {
|
*/
|
||||||
headers: { Authorization: this.authHeader },
|
private async get(path: string, params?: Record<string, unknown>): Promise<unknown> {
|
||||||
params,
|
for (let attempt = 0; ; attempt++) {
|
||||||
}),
|
try {
|
||||||
);
|
const response = await firstValueFrom(
|
||||||
return response.data;
|
this.httpService.get<unknown>(`${this.baseUrl}${path}`, {
|
||||||
|
headers: { Authorization: this.authHeader },
|
||||||
|
params,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (!isAxiosError(error) || error.response?.status !== 429) throw error;
|
||||||
|
if (attempt >= RATE_LIMIT_RETRIES) throw new HiboutikRateLimitError(path);
|
||||||
|
|
||||||
|
const delay = retryAfterMs(error.response.headers['retry-after']) ?? RATE_LIMIT_BASE_DELAY_MS * 2 ** attempt;
|
||||||
|
await this.sleep(Math.min(delay, RATE_LIMIT_MAX_DELAY_MS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Separate method so that tests can skip the waits. */
|
||||||
|
protected sleep(ms: number): Promise<void> {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET on a list endpoint. HTTP errors (4xx/5xx) throw, a 2xx answer without body is an empty list, and any other
|
* GET on a list endpoint. HTTP errors (4xx/5xx) throw, a 2xx answer without body is an empty list, and any other
|
||||||
* non-list body throws: an empty list triggers deletions in the sync, so it must never be inferred from a malformed answer.
|
* non-list body or invalid item throws: an empty list triggers deletions in the sync, so it must never be inferred
|
||||||
|
* from a malformed answer.
|
||||||
*/
|
*/
|
||||||
async getList<T>(path: string, params?: Record<string, unknown>): Promise<T[]> {
|
async getList<T extends z.ZodType>(path: string, itemSchema: T, params?: Record<string, unknown>): Promise<z.output<T>[]> {
|
||||||
const data = await this.get<T[] | '' | null | undefined>(path, params);
|
const data = await this.get(path, params);
|
||||||
if (data === '' || data == null) return [];
|
if (data === '' || data == null) return [];
|
||||||
if (!Array.isArray(data)) throw new Error(`Unexpected answer from Hiboutik ${path}: a list was expected`);
|
if (!Array.isArray(data)) throw new Error(`Unexpected answer from Hiboutik ${path}: a list was expected`);
|
||||||
return data;
|
return parseHiboutik(z.array(itemSchema), data, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches every page of a paginated endpoint (`p` param, starting at 1) and concatenates the results.
|
* Fetches every page of a paginated endpoint (`p` param, starting at 1) and concatenates the results.
|
||||||
* Only for paginated endpoints: on a non-paginated one, a full first page would loop forever on the same results.
|
* Only for paginated endpoints: on a non-paginated one, a full first page would loop forever on the same results.
|
||||||
*/
|
*/
|
||||||
async getAllPages<T>(path: string, baseParams: Record<string, unknown> = {}): Promise<T[]> {
|
async getAllPages<T extends z.ZodType>(path: string, itemSchema: T, baseParams: Record<string, unknown> = {}): Promise<z.output<T>[]> {
|
||||||
const results: T[] = [];
|
return (await this.walkPages(path, itemSchema, baseParams)).items;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as {@link getAllPages}, for a list whose content drives deletions. An item created or deleted in Hiboutik while
|
||||||
|
* the pages are walked shifts the following pages, so an item can be skipped. When the list spans several pages, it
|
||||||
|
* is walked a second time and both walks are merged by id (the second one wins): an item is missing from the result
|
||||||
|
* only if both walks missed it. A list that fits in one page costs no extra call.
|
||||||
|
*/
|
||||||
|
async getAllPagesForSync<T extends z.ZodType>(
|
||||||
|
path: string,
|
||||||
|
itemSchema: T,
|
||||||
|
idOf: (item: z.output<T>) => number,
|
||||||
|
baseParams: Record<string, unknown> = {},
|
||||||
|
): Promise<z.output<T>[]> {
|
||||||
|
const firstWalk = await this.walkPages(path, itemSchema, baseParams);
|
||||||
|
if (firstWalk.nbPages <= 1) return firstWalk.items;
|
||||||
|
|
||||||
|
const secondWalk = await this.walkPages(path, itemSchema, baseParams);
|
||||||
|
const byId = new Map<number, z.output<T>>();
|
||||||
|
for (const item of [...firstWalk.items, ...secondWalk.items]) byId.set(idOf(item), item);
|
||||||
|
return [...byId.values()];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Walks the pages of a paginated endpoint. `nbPages` counts the pages holding results. */
|
||||||
|
private async walkPages<T extends z.ZodType>(
|
||||||
|
path: string,
|
||||||
|
itemSchema: T,
|
||||||
|
baseParams: Record<string, unknown>,
|
||||||
|
): Promise<{ items: z.output<T>[]; nbPages: number }> {
|
||||||
|
const items: z.output<T>[] = [];
|
||||||
let page = 1;
|
let page = 1;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const pageData = await this.getList<T>(path, { ...baseParams, p: page });
|
const pageData = await this.getList(path, itemSchema, { ...baseParams, p: page });
|
||||||
if (pageData.length === 0) break;
|
if (pageData.length === 0) break;
|
||||||
results.push(...pageData);
|
items.push(...pageData);
|
||||||
// A page smaller than the page size is the last one: no need to request an empty page
|
// A page smaller than the page size is the last one: no need to request an empty page
|
||||||
if (pageData.length < HIBOUTIK_PAGE_SIZE) break;
|
if (pageData.length < HIBOUTIK_PAGE_SIZE) break;
|
||||||
page++;
|
page++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return { items, nbPages: Math.ceil(items.length / HIBOUTIK_PAGE_SIZE) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET on a single-resource endpoint (Hiboutik answers with a one-element array).
|
||||||
|
* @returns `null` when Hiboutik says the resource does not exist: a 404, or a 2xx answer that is empty.
|
||||||
|
* @throws on any other HTTP error or malformed answer: an unknown state is never taken for a missing resource.
|
||||||
|
*/
|
||||||
|
async getOne<T extends z.ZodType>(path: string, schema: T): Promise<z.output<T> | null> {
|
||||||
|
try {
|
||||||
|
const [item] = await this.getList(path, schema);
|
||||||
|
return item ?? null;
|
||||||
|
} catch (error) {
|
||||||
|
if (isAxiosError(error) && error.response?.status === 404) return null;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { HiboutikClient } from './hiboutik.client';
|
import { HiboutikClient } from './hiboutik.client';
|
||||||
import { ProductHiboutikDto } from '../models/hiboutikDTO/product.hiboutik.dto';
|
import { ProductHiboutikDto, productHiboutikSchema } from '../models/hiboutikDTO/product.hiboutik.dto';
|
||||||
|
|
||||||
/** Reads products from the Hiboutik API. */
|
/** Reads products from the Hiboutik API. */
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProductHiboutikRepository {
|
export class ProductHiboutikRepository {
|
||||||
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
||||||
|
|
||||||
/** All products, archived ones included, with their sizes and per-size rules. */
|
/**
|
||||||
|
* All products, archived ones included, with their sizes and per-size rules. Walked twice when it spans several
|
||||||
|
* pages: it drives the deletion of products (see `getAllPagesForSync`).
|
||||||
|
*/
|
||||||
async getProducts(): Promise<ProductHiboutikDto[]> {
|
async getProducts(): Promise<ProductHiboutikDto[]> {
|
||||||
return this.hiboutikClient.getAllPages<ProductHiboutikDto>('/products', {
|
return this.hiboutikClient.getAllPagesForSync('/products', productHiboutikSchema, (product) => product.product_id, {
|
||||||
order_by: 'product_id',
|
order_by: 'product_id',
|
||||||
sort: 'ASC',
|
sort: 'ASC',
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { HiboutikClient } from './hiboutik.client';
|
import { HiboutikClient } from './hiboutik.client';
|
||||||
import { SaleHiboutikDto } from '../models/hiboutikDTO/sale.hiboutik.dto';
|
import { SaleHiboutikDto, saleHiboutikSchema } from '../models/hiboutikDTO/sale.hiboutik.dto';
|
||||||
|
|
||||||
/** Reads sales from the Hiboutik API. */
|
/** Reads sales from the Hiboutik API. */
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -8,12 +8,11 @@ export class SaleHiboutikRepository {
|
|||||||
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
constructor(private readonly hiboutikClient: HiboutikClient) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Full sale, with its lines (Hiboutik answers with a one-element array).
|
* Full sale, with its lines.
|
||||||
* @throws if the sale does not exist.
|
* @throws if the sale does not exist.
|
||||||
*/
|
*/
|
||||||
async getSaleById(id: number): Promise<SaleHiboutikDto> {
|
async getSaleById(id: number): Promise<SaleHiboutikDto> {
|
||||||
const data = await this.hiboutikClient.get<SaleHiboutikDto[]>('/sales/' + String(id));
|
const sale = await this.hiboutikClient.getOne(`/sales/${id}`, saleHiboutikSchema);
|
||||||
const sale = data[0];
|
|
||||||
if (!sale) throw new Error(`Hiboutik sale ${id} not found`);
|
if (!sale) throw new Error(`Hiboutik sale ${id} not found`);
|
||||||
return sale;
|
return sale;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { HiboutikClient } from './hiboutik.client';
|
import { HiboutikClient } from './hiboutik.client';
|
||||||
import { StockHiboutikDto } from '../models/hiboutikDTO/stock.hiboutik.dto';
|
import { StockHiboutikDto, stockHiboutikSchema } from '../models/hiboutikDTO/stock.hiboutik.dto';
|
||||||
|
|
||||||
/** Reads available stocks from the Hiboutik API. */
|
/** Reads available stocks from the Hiboutik API. */
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -9,12 +9,6 @@ export class StockHiboutikRepository {
|
|||||||
|
|
||||||
/** Stock of every product and size, all warehouses combined. Not paginated. */
|
/** Stock of every product and size, all warehouses combined. Not paginated. */
|
||||||
async getStocks(): Promise<StockHiboutikDto[]> {
|
async getStocks(): Promise<StockHiboutikDto[]> {
|
||||||
const stocks = await this.hiboutikClient.getList<StockHiboutikDto>('/stock_available/all_wh', {});
|
return this.hiboutikClient.getList('/stock_available/all_wh', stockHiboutikSchema);
|
||||||
// Hiboutik may return ids as strings: normalize them so strict comparisons work downstream
|
|
||||||
return stocks.map((stock) => ({
|
|
||||||
...stock,
|
|
||||||
product_id: Number(stock.product_id),
|
|
||||||
product_size: Number(stock.product_size),
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { HiboutikClient } from './hiboutik.client';
|
import { HiboutikClient } from './hiboutik.client';
|
||||||
import { SupplierHiboutikDto } from '../models/hiboutikDTO/supplier.hiboutik.dto';
|
import { SupplierHiboutikDto, supplierHiboutikSchema } from '../models/hiboutikDTO/supplier.hiboutik.dto';
|
||||||
|
|
||||||
/** Reads suppliers from the Hiboutik API. */
|
/** Reads suppliers from the Hiboutik API. */
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -9,7 +9,7 @@ export class SupplierHiboutikRepository {
|
|||||||
|
|
||||||
/** `/suppliers` is not paginated: everything comes in one call. */
|
/** `/suppliers` is not paginated: everything comes in one call. */
|
||||||
async getSuppliers(): Promise<SupplierHiboutikDto[]> {
|
async getSuppliers(): Promise<SupplierHiboutikDto[]> {
|
||||||
return this.hiboutikClient.getList<SupplierHiboutikDto>('/suppliers', {
|
return this.hiboutikClient.getList('/suppliers', supplierHiboutikSchema, {
|
||||||
order_by: 'supplier_id',
|
order_by: 'supplier_id',
|
||||||
sort: 'ASC',
|
sort: 'ASC',
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export class BrandConverter extends Converter<BrandCreateInput, BrandHiboutikDto
|
|||||||
return {
|
return {
|
||||||
hiboutikId: dto.brand_id,
|
hiboutikId: dto.brand_id,
|
||||||
name: dto.brand_name,
|
name: dto.brand_name,
|
||||||
enabled: dto.brand_enabled == 1,
|
enabled: dto.brand_enabled === 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ export class CategoryConverter extends Converter<CategoryCreateInput, CategoryHi
|
|||||||
return {
|
return {
|
||||||
hiboutikId: hiboutikDto.category_id,
|
hiboutikId: hiboutikDto.category_id,
|
||||||
name: hiboutikDto.category_name,
|
name: hiboutikDto.category_name,
|
||||||
enabled: hiboutikDto.category_enabled == 1,
|
enabled: hiboutikDto.category_enabled === 1,
|
||||||
position: Number(hiboutikDto.category_position) || 0,
|
position: hiboutikDto.category_position,
|
||||||
webEnabled: hiboutikDto.category_enabled_www == 1,
|
webEnabled: hiboutikDto.category_enabled_www === 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
import { CustomerConverter } from './customer.converter';
|
import { CustomerConverter } from './customer.converter';
|
||||||
import { FullCustomerHiboutikDto } from '../hiboutikDTO/customer.hiboutik.dto';
|
import { FullCustomerHiboutikDto } from '../hiboutikDTO/customer.hiboutik.dto';
|
||||||
|
|
||||||
const hiboutikCustomer = (overrides: Partial<FullCustomerHiboutikDto> = {}): FullCustomerHiboutikDto =>
|
const hiboutikCustomer = (overrides: Partial<FullCustomerHiboutikDto> = {}): FullCustomerHiboutikDto => ({
|
||||||
({
|
customers_id: 42,
|
||||||
customers_id: 42,
|
first_name: 'Jane',
|
||||||
first_name: 'Jane',
|
last_name: 'Doe',
|
||||||
last_name: 'Doe',
|
email: 'jane@example.com',
|
||||||
email: 'jane@example.com',
|
phone: '0600000000',
|
||||||
phone: '0600000000',
|
date_of_birth: '1990-05-12',
|
||||||
date_of_birth: '1990-05-12',
|
updated_at: '2026-06-28 17:02:55',
|
||||||
updated_at: '2026-06-28 17:02:55',
|
...overrides,
|
||||||
...overrides,
|
});
|
||||||
}) as FullCustomerHiboutikDto;
|
|
||||||
|
|
||||||
describe('CustomerConverter', () => {
|
describe('CustomerConverter', () => {
|
||||||
const converter = new CustomerConverter();
|
const converter = new CustomerConverter();
|
||||||
@@ -22,7 +21,8 @@ describe('CustomerConverter', () => {
|
|||||||
email: 'jane@example.com',
|
email: 'jane@example.com',
|
||||||
firstName: 'Jane',
|
firstName: 'Jane',
|
||||||
hiboutikId: 42,
|
hiboutikId: 42,
|
||||||
hiboutikUpdatedAt: new Date('2026-06-28T17:02:55'),
|
// French summer time (UTC+2)
|
||||||
|
hiboutikUpdatedAt: new Date('2026-06-28T15:02:55Z'),
|
||||||
lastName: 'Doe',
|
lastName: 'Doe',
|
||||||
phone: '0600000000',
|
phone: '0600000000',
|
||||||
});
|
});
|
||||||
@@ -32,13 +32,13 @@ describe('CustomerConverter', () => {
|
|||||||
expect(converter.fromHiboutik(hiboutikCustomer({ date_of_birth: '0000-00-00' })).dateOfBirth).toBeUndefined();
|
expect(converter.fromHiboutik(hiboutikCustomer({ date_of_birth: '0000-00-00' })).dateOfBirth).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('keeps the stored email when Hiboutik has none (undefined)', () => {
|
it('erases the email when Hiboutik has none (null)', () => {
|
||||||
expect(converter.fromHiboutik(hiboutikCustomer({ email: '' })).email).toBeUndefined();
|
expect(converter.fromHiboutik(hiboutikCustomer({ email: '' })).email).toBeNull();
|
||||||
expect(converter.fromHiboutik(hiboutikCustomer({ email: null as unknown as string })).email).toBeUndefined();
|
expect(converter.fromHiboutik(hiboutikCustomer({ email: ' ' })).email).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('erases the phone when Hiboutik has none (null)', () => {
|
it('erases the phone when Hiboutik has none (null)', () => {
|
||||||
expect(converter.fromHiboutik(hiboutikCustomer({ phone: '' })).phone).toBeNull();
|
expect(converter.fromHiboutik(hiboutikCustomer({ phone: '' })).phone).toBeNull();
|
||||||
expect(converter.fromHiboutik(hiboutikCustomer({ phone: null as unknown as string })).phone).toBeNull();
|
expect(converter.fromHiboutik(hiboutikCustomer({ phone: ' ' })).phone).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ export class CustomerConverter extends Converter<CustomerCreateInput, FullCustom
|
|||||||
fromHiboutik(dto: FullCustomerHiboutikDto): CustomerCreateInput {
|
fromHiboutik(dto: FullCustomerHiboutikDto): CustomerCreateInput {
|
||||||
return {
|
return {
|
||||||
dateOfBirth: parseHiboutikDate(dto.date_of_birth),
|
dateOfBirth: parseHiboutikDate(dto.date_of_birth),
|
||||||
// undefined (not null) on purpose: an email removed in Hiboutik must not erase the one stored in database
|
// null (not undefined) so that an email removed in Hiboutik is also removed in database (data minimization)
|
||||||
email: dto.email?.trim() || undefined,
|
email: dto.email.trim() || null,
|
||||||
firstName: dto.first_name,
|
firstName: dto.first_name,
|
||||||
hiboutikId: dto.customers_id,
|
hiboutikId: dto.customers_id,
|
||||||
hiboutikUpdatedAt: parseHiboutikDate(dto.updated_at),
|
hiboutikUpdatedAt: parseHiboutikDate(dto.updated_at),
|
||||||
lastName: dto.last_name,
|
lastName: dto.last_name,
|
||||||
// null (not undefined) so that a phone removed in Hiboutik is also removed in database
|
// null (not undefined) so that a phone removed in Hiboutik is also removed in database
|
||||||
phone: dto.phone?.trim() || null,
|
phone: dto.phone.trim() || null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
toHiboutik(): FullCustomerHiboutikDto {
|
toHiboutik(): FullCustomerHiboutikDto {
|
||||||
|
|||||||
@@ -1,23 +1,22 @@
|
|||||||
import { CustomerAddressConverter } from './customerAddress.converter';
|
import { CustomerAddressConverter } from './customerAddress.converter';
|
||||||
import { CustomerAddressHiboutikDto } from '../hiboutikDTO/customer.hiboutik.dto';
|
import { CustomerAddressHiboutikDto } from '../hiboutikDTO/customer.hiboutik.dto';
|
||||||
|
|
||||||
const hiboutikAddress = (overrides: Partial<CustomerAddressHiboutikDto> = {}): CustomerAddressHiboutikDto =>
|
const hiboutikAddress = (overrides: Partial<CustomerAddressHiboutikDto> = {}): CustomerAddressHiboutikDto => ({
|
||||||
({
|
address_id: 7,
|
||||||
address_id: 7,
|
customers_id: 42,
|
||||||
customers_id: 42,
|
first_name: 'Jane',
|
||||||
first_name: 'Jane',
|
last_name: 'Doe',
|
||||||
last_name: 'Doe',
|
company: 'ACME',
|
||||||
company: 'ACME',
|
phone: '0600000000',
|
||||||
phone: '0600000000',
|
address: ' 1 rue de la Paix ',
|
||||||
address: ' 1 rue de la Paix ',
|
zip_code: '75002',
|
||||||
zip_code: '75002',
|
city: 'Paris',
|
||||||
city: 'Paris',
|
state: '',
|
||||||
state: '',
|
country: 'FRA',
|
||||||
country: 'FRA',
|
other: 'Bât. B',
|
||||||
other: 'Bât. B',
|
default: 1,
|
||||||
default: 1,
|
...overrides,
|
||||||
...overrides,
|
});
|
||||||
}) as CustomerAddressHiboutikDto;
|
|
||||||
|
|
||||||
describe('CustomerAddressConverter', () => {
|
describe('CustomerAddressConverter', () => {
|
||||||
const converter = new CustomerAddressConverter();
|
const converter = new CustomerAddressConverter();
|
||||||
@@ -40,17 +39,8 @@ describe('CustomerAddressConverter', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('accepts ids and flags sent as strings', () => {
|
|
||||||
const address = converter.fromHiboutik(hiboutikAddress({ address_id: '8', customers_id: '43', default: '0' } as never));
|
|
||||||
expect(address.hiboutikId).toBe(8);
|
|
||||||
expect(address.isDefault).toBe(false);
|
|
||||||
expect(address.customer).toEqual({ connect: { hiboutikId: 43 } });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('erases empty optional fields (null) and defaults empty required ones to ""', () => {
|
it('erases empty optional fields (null) and defaults empty required ones to ""', () => {
|
||||||
const address = converter.fromHiboutik(
|
const address = converter.fromHiboutik(hiboutikAddress({ company: ' ', phone: '', other: '', first_name: '' }));
|
||||||
hiboutikAddress({ company: ' ', phone: null as unknown as string, other: '', first_name: null as unknown as string }),
|
|
||||||
);
|
|
||||||
expect(address.company).toBeNull();
|
expect(address.company).toBeNull();
|
||||||
expect(address.phone).toBeNull();
|
expect(address.phone).toBeNull();
|
||||||
expect(address.other).toBeNull();
|
expect(address.other).toBeNull();
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ export class CustomerAddressConverter extends Converter<
|
|||||||
companyNumber: companyDetail(details.company_number),
|
companyNumber: companyDetail(details.company_number),
|
||||||
country: required(dto.country),
|
country: required(dto.country),
|
||||||
firstName: required(dto.first_name),
|
firstName: required(dto.first_name),
|
||||||
hiboutikId: Number(dto.address_id),
|
hiboutikId: dto.address_id,
|
||||||
isDefault: Number(dto.default) === 1,
|
isDefault: dto.default === 1,
|
||||||
lastName: required(dto.last_name),
|
lastName: required(dto.last_name),
|
||||||
legalStatus: companyDetail(details.legal_status),
|
legalStatus: companyDetail(details.legal_status),
|
||||||
other: optional(dto.other),
|
other: optional(dto.other),
|
||||||
@@ -40,7 +40,7 @@ export class CustomerAddressConverter extends Converter<
|
|||||||
state: optional(dto.state),
|
state: optional(dto.state),
|
||||||
taxNumber: companyDetail(details.tax_number),
|
taxNumber: companyDetail(details.tax_number),
|
||||||
zipCode: required(dto.zip_code),
|
zipCode: required(dto.zip_code),
|
||||||
customer: { connect: { hiboutikId: Number(dto.customers_id) } },
|
customer: { connect: { hiboutikId: dto.customers_id } },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
toHiboutik(): CustomerAddressHiboutikDto {
|
toHiboutik(): CustomerAddressHiboutikDto {
|
||||||
|
|||||||
@@ -2,29 +2,26 @@ import { OrderConverter } from './order.converter';
|
|||||||
import { SaleHiboutikDto, SaleLineHiboutikDto } from '../hiboutikDTO/sale.hiboutik.dto';
|
import { SaleHiboutikDto, SaleLineHiboutikDto } from '../hiboutikDTO/sale.hiboutik.dto';
|
||||||
|
|
||||||
const hiboutikLine = (overrides: Partial<SaleLineHiboutikDto> = {}): SaleLineHiboutikDto => ({
|
const hiboutikLine = (overrides: Partial<SaleLineHiboutikDto> = {}): SaleLineHiboutikDto => ({
|
||||||
line_item_id: 3,
|
|
||||||
product_id: 746,
|
product_id: 746,
|
||||||
product_size: 1,
|
product_size: 1,
|
||||||
quantity: 2,
|
quantity: 2,
|
||||||
product_model: 'KATSEYE - WILD',
|
product_model: 'KATSEYE - WILD',
|
||||||
product_barcode: '2430010007467',
|
product_barcode: '2430010007467',
|
||||||
size_name: 'Daniela',
|
size_name: 'Daniela',
|
||||||
item_unit_gross_wo_discount: '19.00',
|
|
||||||
item_unit_gross: '17.10',
|
item_unit_gross: '17.10',
|
||||||
...overrides,
|
...overrides,
|
||||||
});
|
});
|
||||||
|
|
||||||
const hiboutikSale = (overrides: Partial<SaleHiboutikDto> = {}): SaleHiboutikDto =>
|
const hiboutikSale = (overrides: Partial<SaleHiboutikDto> = {}): SaleHiboutikDto => ({
|
||||||
({
|
sale_id: 5,
|
||||||
sale_id: 5,
|
created_at: '2026-10-18 11:00:21',
|
||||||
created_at: '2026-10-18 11:00:21',
|
completed_at: '2026-10-18 11:01:04',
|
||||||
completed_at: '2026-10-18 11:01:04',
|
customer_id: 1,
|
||||||
customer_id: 1,
|
total: '34.20',
|
||||||
total: '34.20',
|
quotation: 0,
|
||||||
quotation: 0,
|
line_items: [hiboutikLine()],
|
||||||
line_items: [hiboutikLine()],
|
...overrides,
|
||||||
...overrides,
|
});
|
||||||
}) as SaleHiboutikDto;
|
|
||||||
|
|
||||||
describe('OrderConverter', () => {
|
describe('OrderConverter', () => {
|
||||||
const converter = new OrderConverter();
|
const converter = new OrderConverter();
|
||||||
@@ -32,7 +29,8 @@ describe('OrderConverter', () => {
|
|||||||
it('maps a completed Hiboutik sale with its lines', () => {
|
it('maps a completed Hiboutik sale with its lines', () => {
|
||||||
expect(converter.fromHiboutik(hiboutikSale())).toEqual({
|
expect(converter.fromHiboutik(hiboutikSale())).toEqual({
|
||||||
hiboutikSaleId: 5,
|
hiboutikSaleId: 5,
|
||||||
orderedAt: new Date('2026-10-18T11:01:04'),
|
// French summer time (UTC+2)
|
||||||
|
orderedAt: new Date('2026-10-18T09:01:04Z'),
|
||||||
source: 'STORE',
|
source: 'STORE',
|
||||||
status: 'COMPLETED',
|
status: 'COMPLETED',
|
||||||
total: '34.20',
|
total: '34.20',
|
||||||
@@ -54,7 +52,7 @@ describe('OrderConverter', () => {
|
|||||||
it('maps a sale not completed yet as PENDING, ordered at its creation date', () => {
|
it('maps a sale not completed yet as PENDING, ordered at its creation date', () => {
|
||||||
const order = converter.fromHiboutik(hiboutikSale({ completed_at: '0000-00-00 00:00:00' }));
|
const order = converter.fromHiboutik(hiboutikSale({ completed_at: '0000-00-00 00:00:00' }));
|
||||||
expect(order.status).toBe('PENDING');
|
expect(order.status).toBe('PENDING');
|
||||||
expect(order.orderedAt).toEqual(new Date('2026-10-18T11:00:21'));
|
expect(order.orderedAt).toEqual(new Date('2026-10-18T09:00:21Z'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws when the sale has no valid date', () => {
|
it('throws when the sale has no valid date', () => {
|
||||||
@@ -75,6 +73,6 @@ describe('OrderConverter', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('accepts a sale without lines', () => {
|
it('accepts a sale without lines', () => {
|
||||||
expect(converter.fromHiboutik(hiboutikSale({ line_items: undefined as never })).lines).toEqual([]);
|
expect(converter.fromHiboutik(hiboutikSale({ line_items: [] })).lines).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,29 +17,28 @@ export class OrderConverter extends Converter<OrderWithLinesInput, SaleHiboutikD
|
|||||||
const orderedAt = completedAt ?? parseHiboutikDate(dto.created_at);
|
const orderedAt = completedAt ?? parseHiboutikDate(dto.created_at);
|
||||||
if (!orderedAt) throw new Error(`Hiboutik sale ${dto.sale_id} has no valid date`);
|
if (!orderedAt) throw new Error(`Hiboutik sale ${dto.sale_id} has no valid date`);
|
||||||
|
|
||||||
const customerId = Number(dto.customer_id);
|
|
||||||
return {
|
return {
|
||||||
hiboutikSaleId: Number(dto.sale_id),
|
hiboutikSaleId: dto.sale_id,
|
||||||
orderedAt,
|
orderedAt,
|
||||||
source: OrderSource.STORE,
|
source: OrderSource.STORE,
|
||||||
status: completedAt ? OrderStatus.COMPLETED : OrderStatus.PENDING,
|
status: completedAt ? OrderStatus.COMPLETED : OrderStatus.PENDING,
|
||||||
total: dto.total,
|
total: dto.total,
|
||||||
// 0 for an anonymous sale
|
// 0 for an anonymous sale
|
||||||
customer: customerId > 0 ? { connect: { hiboutikId: customerId } } : undefined,
|
customer: dto.customer_id > 0 ? { connect: { hiboutikId: dto.customer_id } } : undefined,
|
||||||
lines: (dto.line_items ?? []).map((line) => this.lineFromHiboutik(line)),
|
lines: dto.line_items.map((line) => this.lineFromHiboutik(line)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private lineFromHiboutik(line: SaleLineHiboutikDto): OrderWithLinesInput['lines'][number] {
|
private lineFromHiboutik(line: SaleLineHiboutikDto): OrderWithLinesInput['lines'][number] {
|
||||||
return {
|
return {
|
||||||
barcode: line.product_barcode?.trim() || null,
|
barcode: line.product_barcode.trim() || null,
|
||||||
hiboutikProductId: Number(line.product_id),
|
hiboutikProductId: line.product_id,
|
||||||
hiboutikSizeId: Number(line.product_size),
|
hiboutikSizeId: line.product_size,
|
||||||
productName: line.product_model,
|
productName: line.product_model,
|
||||||
quantity: Number(line.quantity),
|
quantity: line.quantity,
|
||||||
unitPrice: line.item_unit_gross,
|
unitPrice: line.item_unit_gross,
|
||||||
// same rule as ProductVersion: the product name for a product without sizes
|
// same rule as ProductVersion: the product name for a product without sizes
|
||||||
versionName: line.size_name?.trim() || line.product_model,
|
versionName: line.size_name.trim() || line.product_model,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ const hiboutikProduct = (overrides: Partial<ProductHiboutikDto> = {}): ProductHi
|
|||||||
product_supplier: 2,
|
product_supplier: 2,
|
||||||
product_price: '13.99',
|
product_price: '13.99',
|
||||||
product_discount_price: '0.00',
|
product_discount_price: '0.00',
|
||||||
product_supply_price: '7.49',
|
|
||||||
product_category: 19,
|
product_category: 19,
|
||||||
product_display_www: 0,
|
product_display_www: 0,
|
||||||
product_arch: 0,
|
product_arch: 0,
|
||||||
products_desc: '',
|
products_desc: '',
|
||||||
updated_at: '2026-06-28 17:02:55',
|
updated_at: '2026-06-28 17:02:55',
|
||||||
product_specific_rules: [],
|
product_specific_rules: [],
|
||||||
|
product_size_details: [],
|
||||||
...overrides,
|
...overrides,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ describe('ProductConverter', () => {
|
|||||||
description: '',
|
description: '',
|
||||||
discountPrice: null,
|
discountPrice: null,
|
||||||
hiboutikId: 1,
|
hiboutikId: 1,
|
||||||
hiboutikUpdatedAt: new Date('2026-06-28T17:02:55'),
|
hiboutikUpdatedAt: new Date('2026-06-28T15:02:55Z'),
|
||||||
name: 'BOUTEILLE MÉTAL ISOTHERME 260ML BT21',
|
name: 'BOUTEILLE MÉTAL ISOTHERME 260ML BT21',
|
||||||
price: '13.99',
|
price: '13.99',
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export class ProductConverter extends Converter<ProductCreateInput, ProductHibou
|
|||||||
/** `globalStock` and versions are not set here: they depend on the stocks endpoint (see ProductHiboutikService). */
|
/** `globalStock` and versions are not set here: they depend on the stocks endpoint (see ProductHiboutikService). */
|
||||||
fromHiboutik(dto: ProductHiboutikDto): ProductCreateInput {
|
fromHiboutik(dto: ProductHiboutikDto): ProductCreateInput {
|
||||||
return {
|
return {
|
||||||
archived: dto.product_arch == 1,
|
archived: dto.product_arch === 1,
|
||||||
barcode: dto.product_barcode || null,
|
barcode: dto.product_barcode || null,
|
||||||
brand: dto.product_brand ? { connect: { hiboutikId: dto.product_brand } } : undefined,
|
brand: dto.product_brand ? { connect: { hiboutikId: dto.product_brand } } : undefined,
|
||||||
category: dto.product_category ? { connect: { hiboutikId: dto.product_category } } : undefined,
|
category: dto.product_category ? { connect: { hiboutikId: dto.product_category } } : undefined,
|
||||||
@@ -20,7 +20,7 @@ export class ProductConverter extends Converter<ProductCreateInput, ProductHibou
|
|||||||
hiboutikUpdatedAt: parseHiboutikDate(dto.updated_at),
|
hiboutikUpdatedAt: parseHiboutikDate(dto.updated_at),
|
||||||
name: dto.product_model,
|
name: dto.product_model,
|
||||||
price: dto.product_price,
|
price: dto.product_price,
|
||||||
enabled: dto.product_display_www == 1,
|
enabled: dto.product_display_www === 1,
|
||||||
supplier: dto.product_supplier ? { connect: { hiboutikId: dto.product_supplier } } : undefined,
|
supplier: dto.product_supplier ? { connect: { hiboutikId: dto.product_supplier } } : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export class SupplierConverter extends Converter<SupplierCreateInput, SupplierHi
|
|||||||
return {
|
return {
|
||||||
hiboutikId: dto.supplier_id,
|
hiboutikId: dto.supplier_id,
|
||||||
name: dto.supplier_name,
|
name: dto.supplier_name,
|
||||||
enabled: dto.supplier_enabled == 1,
|
enabled: dto.supplier_enabled === 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
/** Brand as returned by Hiboutik `GET /brands`. */
|
import { z } from 'zod';
|
||||||
export interface BrandHiboutikDto {
|
import { hiboutikId, hiboutikInt, hiboutikText } from './hiboutik.schema';
|
||||||
brand_id: number;
|
|
||||||
brand_name: string;
|
/** Brand as returned by Hiboutik `GET /brands` (only the fields used by the sync). */
|
||||||
|
export const brandHiboutikSchema = z.object({
|
||||||
|
brand_id: hiboutikId,
|
||||||
|
brand_name: hiboutikText,
|
||||||
/** 1 = enabled, 0 = disabled. */
|
/** 1 = enabled, 0 = disabled. */
|
||||||
brand_enabled: number;
|
brand_enabled: hiboutikInt,
|
||||||
}
|
});
|
||||||
|
|
||||||
|
export type BrandHiboutikDto = z.output<typeof brandHiboutikSchema>;
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
/** Category as returned by Hiboutik `GET /categories`. */
|
import { z } from 'zod';
|
||||||
export interface CategoryHiboutikDto {
|
import { hiboutikId, hiboutikInt, hiboutikRef, hiboutikText } from './hiboutik.schema';
|
||||||
category_id: number;
|
|
||||||
category_name: string;
|
/** Category as returned by Hiboutik `GET /categories` (only the fields used by the sync). */
|
||||||
|
export const categoryHiboutikSchema = z.object({
|
||||||
|
category_id: hiboutikId,
|
||||||
|
category_name: hiboutikText,
|
||||||
/** Id of the parent category, 0 for a root category. */
|
/** Id of the parent category, 0 for a root category. */
|
||||||
category_id_parent: number;
|
category_id_parent: hiboutikRef,
|
||||||
/** Enabled in the POS: 1 = yes, 0 = no. */
|
/** Enabled in the POS: 1 = yes, 0 = no. */
|
||||||
category_enabled: number;
|
category_enabled: hiboutikInt,
|
||||||
/** Displayed on the website: 1 = yes, 0 = no. */
|
/** Displayed on the website: 1 = yes, 0 = no. */
|
||||||
category_enabled_www: number;
|
category_enabled_www: hiboutikInt,
|
||||||
/** Display order among its siblings. */
|
/** Display order among its siblings. */
|
||||||
category_position: number;
|
category_position: hiboutikInt,
|
||||||
category_bck_color: string;
|
});
|
||||||
category_color: string;
|
|
||||||
category_desc: string;
|
export type CategoryHiboutikDto = z.output<typeof categoryHiboutikSchema>;
|
||||||
accounting_account: string;
|
|
||||||
category_ref_ext: string;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,91 +1,65 @@
|
|||||||
/** Customer summary as returned by Hiboutik `GET /customers` (no addresses nor sales). */
|
import { z } from 'zod';
|
||||||
export interface CustomerHiboutikDto {
|
import { hiboutikId, hiboutikInt, hiboutikOptionalList, hiboutikRef, hiboutikText } from './hiboutik.schema';
|
||||||
customers_id: number;
|
|
||||||
last_name: string;
|
|
||||||
first_name: string;
|
|
||||||
company: string;
|
|
||||||
email: string;
|
|
||||||
phone: string;
|
|
||||||
vat: string;
|
|
||||||
country: string;
|
|
||||||
/** "YYYY-MM-DD", "0000-00-00" when unknown. */
|
|
||||||
date_of_birth: string;
|
|
||||||
customers_code: string;
|
|
||||||
loyalty_points: number;
|
|
||||||
intial_loyalty_points: number;
|
|
||||||
validity: string;
|
|
||||||
prepaid_purchases: string;
|
|
||||||
store_credit: string;
|
|
||||||
turnover: string;
|
|
||||||
orders: number;
|
|
||||||
customer_ref_ext: string;
|
|
||||||
last_order_date: string;
|
|
||||||
/** Local time, "YYYY-MM-DD HH:mm:ss". */
|
|
||||||
updated_at: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Full customer as returned by Hiboutik `GET /customer/:customers_id`, with its addresses and sales. */
|
/** Customer summary as returned by Hiboutik `GET /customers`: only its id is used (no addresses nor sales). */
|
||||||
export interface FullCustomerHiboutikDto extends CustomerHiboutikDto {
|
export const customerHiboutikSchema = z.object({
|
||||||
gender: number;
|
customers_id: hiboutikId,
|
||||||
comments: string;
|
});
|
||||||
customers_id_referrer: number;
|
|
||||||
customers_misc: string;
|
|
||||||
accounting_account: string;
|
|
||||||
addresses: CustomerAddressHiboutikDto[];
|
|
||||||
sales: CustomerSaleHiboutikDto[];
|
|
||||||
tags: unknown[];
|
|
||||||
customers_referee: unknown[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Address of a customer, embedded in {@link FullCustomerHiboutikDto}. Name and phone can differ from the customer's. */
|
/** Address of a customer, embedded in the full customer. Name and phone can differ from the customer's. */
|
||||||
export interface CustomerAddressHiboutikDto {
|
export const customerAddressHiboutikSchema = z.object({
|
||||||
address_id: number;
|
address_id: hiboutikId,
|
||||||
gender: string;
|
first_name: hiboutikText,
|
||||||
first_name: string;
|
last_name: hiboutikText,
|
||||||
last_name: string;
|
phone: hiboutikText,
|
||||||
creation_date: string;
|
company: hiboutikText,
|
||||||
email: string;
|
address: hiboutikText,
|
||||||
phone: string;
|
zip_code: hiboutikText,
|
||||||
company: string;
|
city: hiboutikText,
|
||||||
address: string;
|
state: hiboutikText,
|
||||||
zip_code: string;
|
country: hiboutikText,
|
||||||
city: string;
|
customers_id: hiboutikRef,
|
||||||
state: string;
|
|
||||||
country: string;
|
|
||||||
customers_id: number;
|
|
||||||
display: number;
|
|
||||||
editable: number;
|
|
||||||
/** 1 for the customer's default address. */
|
/** 1 for the customer's default address. */
|
||||||
default: number;
|
default: hiboutikInt,
|
||||||
other: string;
|
other: hiboutikText,
|
||||||
}
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Address as returned by Hiboutik `GET /customers_addresses/:address_id`: the embedded address plus the company
|
* Address as returned by Hiboutik `GET /customers_addresses/:address_id`: the embedded address plus the company
|
||||||
* details and its last update. One API call per address, so only fetched for addresses with a company.
|
* details. One API call per address, so only fetched for addresses with a company.
|
||||||
*/
|
*/
|
||||||
export interface CustomerAddressDetailsHiboutikDto extends CustomerAddressHiboutikDto {
|
export const customerAddressDetailsHiboutikSchema = customerAddressHiboutikSchema.extend({
|
||||||
tax_number: string;
|
tax_number: hiboutikText,
|
||||||
company_number: string;
|
company_number: hiboutikText,
|
||||||
legal_status: string;
|
legal_status: hiboutikText,
|
||||||
/** Local time, "YYYY-MM-DD HH:mm:ss". */
|
/** French local time, "YYYY-MM-DD HH:mm:ss". */
|
||||||
updated_at: string;
|
updated_at: hiboutikText,
|
||||||
}
|
});
|
||||||
|
|
||||||
/** Sale header embedded in {@link FullCustomerHiboutikDto}: no sale lines, they come from `GET /sales/:sale_id`. */
|
/** Sale header embedded in the full customer: no sale lines, they come from `GET /sales/:sale_id`. */
|
||||||
export interface CustomerSaleHiboutikDto {
|
export const customerSaleHiboutikSchema = z.object({
|
||||||
sale_id: number;
|
sale_id: hiboutikId,
|
||||||
created_at: string;
|
});
|
||||||
completed_at: string;
|
|
||||||
date_z: number;
|
/** Full customer as returned by Hiboutik `GET /customer/:customers_id`, with its addresses and sales. */
|
||||||
store_id: number;
|
export const fullCustomerHiboutikSchema = z.object({
|
||||||
vendor_id: number;
|
customers_id: hiboutikId,
|
||||||
unique_sale_id: string;
|
last_name: hiboutikText,
|
||||||
customer_id: number;
|
first_name: hiboutikText,
|
||||||
currency: string;
|
email: hiboutikText,
|
||||||
/** Decimal as a string. */
|
phone: hiboutikText,
|
||||||
total: string;
|
/** "YYYY-MM-DD", "0000-00-00" when unknown. */
|
||||||
points: number;
|
date_of_birth: hiboutikText,
|
||||||
billing_address: number;
|
/** French local time, "YYYY-MM-DD HH:mm:ss". */
|
||||||
shipping_address: number;
|
updated_at: hiboutikText,
|
||||||
}
|
/** `undefined` when not an array: the stored addresses are then kept. */
|
||||||
|
addresses: hiboutikOptionalList(customerAddressHiboutikSchema),
|
||||||
|
/** `undefined` when not an array: the orders sync is then skipped. */
|
||||||
|
sales: hiboutikOptionalList(customerSaleHiboutikSchema),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type CustomerHiboutikDto = z.output<typeof customerHiboutikSchema>;
|
||||||
|
export type CustomerAddressHiboutikDto = z.output<typeof customerAddressHiboutikSchema>;
|
||||||
|
export type CustomerAddressDetailsHiboutikDto = z.output<typeof customerAddressDetailsHiboutikSchema>;
|
||||||
|
export type CustomerSaleHiboutikDto = z.output<typeof customerSaleHiboutikSchema>;
|
||||||
|
export type FullCustomerHiboutikDto = z.output<typeof fullCustomerHiboutikSchema>;
|
||||||
|
|||||||
87
src/models/hiboutikDTO/hiboutik.schema.spec.ts
Normal file
87
src/models/hiboutikDTO/hiboutik.schema.spec.ts
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import { parseHiboutik } from './hiboutik.schema';
|
||||||
|
import { customerAddressHiboutikSchema, fullCustomerHiboutikSchema } from './customer.hiboutik.dto';
|
||||||
|
import { productHiboutikSchema } from './product.hiboutik.dto';
|
||||||
|
import { saleHiboutikSchema } from './sale.hiboutik.dto';
|
||||||
|
import { stockHiboutikSchema } from './stock.hiboutik.dto';
|
||||||
|
|
||||||
|
const parse = <T extends Parameters<typeof parseHiboutik>[0]>(schema: T, data: unknown) => parseHiboutik(schema, data, '/x');
|
||||||
|
|
||||||
|
describe('Hiboutik schemas', () => {
|
||||||
|
const product = {
|
||||||
|
product_id: 1,
|
||||||
|
product_model: 'BT21 bottle',
|
||||||
|
product_barcode: '8435497280048',
|
||||||
|
product_brand: 1,
|
||||||
|
product_supplier: 2,
|
||||||
|
product_price: '13.99',
|
||||||
|
product_discount_price: '0.00',
|
||||||
|
product_category: 19,
|
||||||
|
product_display_www: 1,
|
||||||
|
product_arch: 0,
|
||||||
|
products_desc: '',
|
||||||
|
updated_at: '2026-06-28 17:02:55',
|
||||||
|
product_specific_rules: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
it('accepts ids, references and flags sent as strings', () => {
|
||||||
|
const parsed = parse(customerAddressHiboutikSchema, { address_id: ' 8 ', customers_id: '43', default: '0' });
|
||||||
|
expect(parsed.address_id).toBe(8);
|
||||||
|
expect(parsed.customers_id).toBe(43);
|
||||||
|
expect(parsed.default).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('turns empty references into 0 and empty text into ""', () => {
|
||||||
|
const parsed = parse(productHiboutikSchema, { ...product, product_brand: '', product_category: null, products_desc: null });
|
||||||
|
expect(parsed.product_brand).toBe(0);
|
||||||
|
expect(parsed.product_category).toBe(0);
|
||||||
|
expect(parsed.products_desc).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps numeric text (zip code) and prices as strings', () => {
|
||||||
|
expect(parse(customerAddressHiboutikSchema, { address_id: 1, zip_code: 75002 }).zip_code).toBe('75002');
|
||||||
|
expect(parse(productHiboutikSchema, { ...product, product_price: 13.5 }).product_price).toBe('13.5');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('defaults missing lists to [] and strips unknown fields', () => {
|
||||||
|
const parsed = parse(productHiboutikSchema, { ...product, product_specific_rules: '', product_supply_price: '7.49' });
|
||||||
|
expect(parsed.product_specific_rules).toEqual([]);
|
||||||
|
expect(parsed.product_size_details).toEqual([]);
|
||||||
|
expect(parsed).not.toHaveProperty('product_supply_price');
|
||||||
|
expect(parse(saleHiboutikSchema, { sale_id: 5, total: '0.00' }).line_items).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('leaves addresses and sales undefined when they are not arrays, so that stored data is kept', () => {
|
||||||
|
const parsed = parse(fullCustomerHiboutikSchema, { customers_id: 1, addresses: '', sales: { error: 'x' } });
|
||||||
|
expect(parsed.addresses).toBeUndefined();
|
||||||
|
expect(parsed.sales).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses stocks sent as decimal strings', () => {
|
||||||
|
expect(parse(stockHiboutikSchema, { product_id: '1', product_size: '0', stock_available: '3.000' })).toEqual({
|
||||||
|
product_id: 1,
|
||||||
|
product_size: 0,
|
||||||
|
stock_available: 3,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
['a missing id', { ...product, product_id: undefined }],
|
||||||
|
['a zero id', { ...product, product_id: 0 }],
|
||||||
|
['a non-numeric id', { ...product, product_id: 'abc' }],
|
||||||
|
['a negative reference', { ...product, product_brand: -1 }],
|
||||||
|
['an invalid price', { ...product, product_price: 'free' }],
|
||||||
|
['a list that is not an array', { ...product, product_size_details: { size_id: 1 } }],
|
||||||
|
])('rejects %s', (_, data) => {
|
||||||
|
expect(() => parse(productHiboutikSchema, data)).toThrow('Unexpected answer from Hiboutik /x');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('names the faulty fields, never their values', () => {
|
||||||
|
expect.assertions(2);
|
||||||
|
try {
|
||||||
|
parse(fullCustomerHiboutikSchema, { customers_id: 'jane@example.com', addresses: [{ address_id: 'Jane Doe' }] });
|
||||||
|
} catch (error) {
|
||||||
|
expect((error as Error).message).toMatch(/customers_id.*addresses\.0\.address_id/);
|
||||||
|
expect((error as Error).message).not.toMatch(/jane|Doe/i);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
58
src/models/hiboutikDTO/hiboutik.schema.ts
Normal file
58
src/models/hiboutikDTO/hiboutik.schema.ts
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Field types shared by the Hiboutik payload schemas. Hiboutik sends numbers either as JSON numbers or as numeric
|
||||||
|
* strings, and empty values as "", null or a missing key: the schemas normalize all that, so that the rest of the
|
||||||
|
* code gets real numbers and strings. Strict on what the sync relies on (identifiers, relations, prices),
|
||||||
|
* lenient (empty string) on free text.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const trimmed = (value: unknown) => (typeof value === 'string' ? value.trim() : value);
|
||||||
|
const emptyAsZero = (value: unknown) => {
|
||||||
|
const v = trimmed(value);
|
||||||
|
return v == null || v === '' ? 0 : v;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Identifier of a Hiboutik entity (join key): a positive integer. */
|
||||||
|
export const hiboutikId = z.preprocess(trimmed, z.coerce.number().int().positive());
|
||||||
|
|
||||||
|
/** Reference to another entity: its id, or 0 (also sent as "" or null) when none. */
|
||||||
|
export const hiboutikRef = z.preprocess(emptyAsZero, z.coerce.number().int().nonnegative());
|
||||||
|
|
||||||
|
/** Integer, e.g. a 1/0 flag, a position or a quantity (0 when empty). */
|
||||||
|
export const hiboutikInt = z.preprocess(emptyAsZero, z.coerce.number().int());
|
||||||
|
|
||||||
|
/** Number, e.g. a stock that may be sent as "3.000" (0 when empty). */
|
||||||
|
export const hiboutikNumber = z.preprocess(emptyAsZero, z.coerce.number());
|
||||||
|
|
||||||
|
/** Free text, "" when empty. */
|
||||||
|
export const hiboutikText = z.preprocess((value) => (value == null ? '' : typeof value === 'number' ? String(value) : value), z.string());
|
||||||
|
|
||||||
|
/** Required decimal kept as a string for Prisma `Decimal`, e.g. "13.99". */
|
||||||
|
export const hiboutikDecimal = z.preprocess(
|
||||||
|
(value) => (typeof value === 'number' ? String(value) : trimmed(value)),
|
||||||
|
z.string().regex(/^-?\d+(\.\d+)?$/, 'decimal expected'),
|
||||||
|
);
|
||||||
|
|
||||||
|
/** List that is empty when missing ("", null or no key). */
|
||||||
|
export const hiboutikList = <T extends z.ZodType>(item: T) =>
|
||||||
|
z.preprocess((value) => (value == null || value === '' ? [] : value), z.array(item));
|
||||||
|
|
||||||
|
/** List that is `undefined` when it is not an array: the caller then keeps the stored data instead of emptying it. */
|
||||||
|
export const hiboutikOptionalList = <T extends z.ZodType>(item: T) =>
|
||||||
|
z.preprocess((value) => (Array.isArray(value) ? value : undefined), z.array(item).optional());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates a Hiboutik payload. The error names the faulty fields (path and issue code) but never their values,
|
||||||
|
* which may be personal data.
|
||||||
|
*/
|
||||||
|
export function parseHiboutik<T extends z.ZodType>(schema: T, data: unknown, path: string): z.output<T> {
|
||||||
|
const result = schema.safeParse(data);
|
||||||
|
if (result.success) return result.data;
|
||||||
|
|
||||||
|
const issues = result.error.issues
|
||||||
|
.slice(0, 5)
|
||||||
|
.map((issue) => `${issue.path.join('.') || '(root)'} (${issue.code})`)
|
||||||
|
.join(', ');
|
||||||
|
throw new Error(`Unexpected answer from Hiboutik ${path}: invalid ${issues}`);
|
||||||
|
}
|
||||||
@@ -1,46 +1,51 @@
|
|||||||
/** Product as returned by Hiboutik `GET /products` (archived products included). */
|
import { z } from 'zod';
|
||||||
export interface ProductHiboutikDto {
|
import { hiboutikDecimal, hiboutikId, hiboutikInt, hiboutikList, hiboutikRef, hiboutikText } from './hiboutik.schema';
|
||||||
product_id: number;
|
|
||||||
/** Product name. */
|
|
||||||
product_model: string;
|
|
||||||
/** Empty string when the product has no barcode. */
|
|
||||||
product_barcode: string;
|
|
||||||
/** Brand id, 0 when none. */
|
|
||||||
product_brand: number;
|
|
||||||
/** Supplier id, 0 when none. */
|
|
||||||
product_supplier: number;
|
|
||||||
/** Decimal as a string, e.g. "13.99". */
|
|
||||||
product_price: string;
|
|
||||||
/** "0.00" when the product has no discount. */
|
|
||||||
product_discount_price: string;
|
|
||||||
product_supply_price: string;
|
|
||||||
/** Category id, 0 when none. */
|
|
||||||
product_category: number;
|
|
||||||
/** Displayed on the website: 1 = yes, 0 = no. */
|
|
||||||
product_display_www: number;
|
|
||||||
/** Archived: 1 = yes, 0 = no. */
|
|
||||||
product_arch: number;
|
|
||||||
products_desc: string;
|
|
||||||
/** Local time, "YYYY-MM-DD HH:mm:ss". */
|
|
||||||
updated_at: string;
|
|
||||||
/** Per-size overrides of the product's price, discount and barcode. */
|
|
||||||
product_specific_rules: ProductSpecificRulesHiboutikDto[];
|
|
||||||
/** Sizes of the product; missing or empty for a product without sizes. */
|
|
||||||
product_size_details?: ProductSizeDetailsHiboutikDto[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Override of a product's values for one size. Prices are "0.00" (or missing) when not overridden. */
|
/** Override of a product's values for one size. Prices are "0.00" (or missing) when not overridden. */
|
||||||
export interface ProductSpecificRulesHiboutikDto {
|
export const productSpecificRulesHiboutikSchema = z.object({
|
||||||
size_id: number;
|
size_id: hiboutikRef,
|
||||||
barcode: string;
|
product_price: hiboutikText,
|
||||||
product_price?: string;
|
product_discount_price: hiboutikText,
|
||||||
product_discount_price?: string;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
/** One size of a product (e.g. "S", "M"). */
|
/** One size of a product (e.g. "S", "M"). */
|
||||||
export interface ProductSizeDetailsHiboutikDto {
|
export const productSizeDetailsHiboutikSchema = z.object({
|
||||||
size_id: number;
|
size_id: hiboutikId,
|
||||||
size_name: string;
|
size_name: hiboutikText,
|
||||||
/** Empty string when the size has no barcode. */
|
/** Empty when the size has no barcode. */
|
||||||
barcode: string;
|
barcode: hiboutikText,
|
||||||
}
|
});
|
||||||
|
|
||||||
|
/** Product as returned by Hiboutik `GET /products`, archived products included (only the fields used by the sync). */
|
||||||
|
export const productHiboutikSchema = z.object({
|
||||||
|
product_id: hiboutikId,
|
||||||
|
/** Product name. */
|
||||||
|
product_model: hiboutikText,
|
||||||
|
/** Empty when the product has no barcode. */
|
||||||
|
product_barcode: hiboutikText,
|
||||||
|
/** Brand id, 0 when none. */
|
||||||
|
product_brand: hiboutikRef,
|
||||||
|
/** Supplier id, 0 when none. */
|
||||||
|
product_supplier: hiboutikRef,
|
||||||
|
/** e.g. "13.99". */
|
||||||
|
product_price: hiboutikDecimal,
|
||||||
|
/** "0.00" when the product has no discount. */
|
||||||
|
product_discount_price: hiboutikText,
|
||||||
|
/** Category id, 0 when none. */
|
||||||
|
product_category: hiboutikRef,
|
||||||
|
/** Displayed on the website: 1 = yes, 0 = no. */
|
||||||
|
product_display_www: hiboutikInt,
|
||||||
|
/** Archived: 1 = yes, 0 = no. */
|
||||||
|
product_arch: hiboutikInt,
|
||||||
|
products_desc: hiboutikText,
|
||||||
|
/** French local time, "YYYY-MM-DD HH:mm:ss". */
|
||||||
|
updated_at: hiboutikText,
|
||||||
|
/** Per-size overrides of the product's price, discount and barcode. */
|
||||||
|
product_specific_rules: hiboutikList(productSpecificRulesHiboutikSchema),
|
||||||
|
/** Sizes of the product; empty for a product without sizes. */
|
||||||
|
product_size_details: hiboutikList(productSizeDetailsHiboutikSchema),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type ProductSpecificRulesHiboutikDto = z.output<typeof productSpecificRulesHiboutikSchema>;
|
||||||
|
export type ProductSizeDetailsHiboutikDto = z.output<typeof productSizeDetailsHiboutikSchema>;
|
||||||
|
export type ProductHiboutikDto = z.output<typeof productHiboutikSchema>;
|
||||||
|
|||||||
@@ -1,40 +1,39 @@
|
|||||||
/**
|
import { z } from 'zod';
|
||||||
* Sale as returned by Hiboutik `GET /sales/:sale_id`, with its lines.
|
import { hiboutikDecimal, hiboutikId, hiboutikInt, hiboutikList, hiboutikRef, hiboutikText } from './hiboutik.schema';
|
||||||
* Only the fields used by the sync are typed: the payload also carries many computed copies (dates split in parts,
|
|
||||||
* totals by tax, customer and store details...).
|
|
||||||
*/
|
|
||||||
export interface SaleHiboutikDto {
|
|
||||||
sale_id: number;
|
|
||||||
/** Local time, "YYYY-MM-DD HH:mm:ss". */
|
|
||||||
created_at: string;
|
|
||||||
/** Local time, "0000-00-00 00:00:00" while the sale is not completed. */
|
|
||||||
completed_at: string;
|
|
||||||
store_id: number;
|
|
||||||
customer_id: number;
|
|
||||||
currency: string;
|
|
||||||
/** Decimal as a string, taxes included. */
|
|
||||||
total: string;
|
|
||||||
/** Payment type code, e.g. "CB", "ESP", "WEB". */
|
|
||||||
payment: string;
|
|
||||||
/** 1 for a quotation. */
|
|
||||||
quotation: number;
|
|
||||||
line_items: SaleLineHiboutikDto[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Line of a {@link SaleHiboutikDto}: a snapshot of the product at sale time. */
|
/** Line of a sale: a snapshot of the product at sale time. */
|
||||||
export interface SaleLineHiboutikDto {
|
export const saleLineHiboutikSchema = z.object({
|
||||||
line_item_id: number;
|
product_id: hiboutikRef,
|
||||||
product_id: number;
|
|
||||||
/** Hiboutik size id, 0 for a product without sizes. */
|
/** Hiboutik size id, 0 for a product without sizes. */
|
||||||
product_size: number;
|
product_size: hiboutikRef,
|
||||||
/** Negative for a returned item. */
|
/** Negative for a returned item. */
|
||||||
quantity: number;
|
quantity: hiboutikInt,
|
||||||
product_model: string;
|
product_model: hiboutikText,
|
||||||
product_barcode: string;
|
product_barcode: hiboutikText,
|
||||||
/** Size name, empty for a product without sizes. */
|
/** Size name, empty for a product without sizes. */
|
||||||
size_name: string;
|
size_name: hiboutikText,
|
||||||
/** Unit price before discount, taxes included (decimal as a string). */
|
/** Unit price actually paid, discount deducted, taxes included. */
|
||||||
item_unit_gross_wo_discount: string;
|
item_unit_gross: hiboutikDecimal,
|
||||||
/** Unit price actually paid, discount deducted, taxes included (decimal as a string). */
|
});
|
||||||
item_unit_gross: string;
|
|
||||||
}
|
/**
|
||||||
|
* Sale as returned by Hiboutik `GET /sales/:sale_id`, with its lines. Only the fields used by the sync: the payload
|
||||||
|
* also carries many computed copies (dates split in parts, totals by tax, customer and store details...).
|
||||||
|
*/
|
||||||
|
export const saleHiboutikSchema = z.object({
|
||||||
|
sale_id: hiboutikId,
|
||||||
|
/** French local time, "YYYY-MM-DD HH:mm:ss". */
|
||||||
|
created_at: hiboutikText,
|
||||||
|
/** French local time, "0000-00-00 00:00:00" while the sale is not completed. */
|
||||||
|
completed_at: hiboutikText,
|
||||||
|
/** 0 for an anonymous sale. */
|
||||||
|
customer_id: hiboutikRef,
|
||||||
|
/** Taxes included. */
|
||||||
|
total: hiboutikDecimal,
|
||||||
|
/** 1 for a quotation. */
|
||||||
|
quotation: hiboutikInt,
|
||||||
|
line_items: hiboutikList(saleLineHiboutikSchema),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type SaleLineHiboutikDto = z.output<typeof saleLineHiboutikSchema>;
|
||||||
|
export type SaleHiboutikDto = z.output<typeof saleHiboutikSchema>;
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { hiboutikId, hiboutikNumber, hiboutikRef } from './hiboutik.schema';
|
||||||
|
|
||||||
/** Available stock of one product size, as returned by Hiboutik `GET /stock_available/all_wh` (all warehouses). */
|
/** Available stock of one product size, as returned by Hiboutik `GET /stock_available/all_wh` (all warehouses). */
|
||||||
export interface StockHiboutikDto {
|
export const stockHiboutikSchema = z.object({
|
||||||
product_id: number;
|
product_id: hiboutikId,
|
||||||
/** Size id, 0 for a product without sizes. */
|
/** Size id, 0 for a product without sizes. */
|
||||||
product_size: number;
|
product_size: hiboutikRef,
|
||||||
/** Decimal as a string, e.g. "3". Stays at 0 while stock management is disabled in Hiboutik. */
|
/** Sent as a decimal string, e.g. "3". Stays at 0 while stock management is disabled in Hiboutik. */
|
||||||
stock_available: string;
|
stock_available: hiboutikNumber,
|
||||||
product_barcode: string;
|
});
|
||||||
product_supplier_reference: string;
|
|
||||||
}
|
export type StockHiboutikDto = z.output<typeof stockHiboutikSchema>;
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
/** Supplier as returned by Hiboutik `GET /suppliers`. */
|
import { z } from 'zod';
|
||||||
export interface SupplierHiboutikDto {
|
import { hiboutikId, hiboutikInt, hiboutikText } from './hiboutik.schema';
|
||||||
supplier_id: number;
|
|
||||||
supplier_name: string;
|
/** Supplier as returned by Hiboutik `GET /suppliers` (only the fields used by the sync). */
|
||||||
|
export const supplierHiboutikSchema = z.object({
|
||||||
|
supplier_id: hiboutikId,
|
||||||
|
supplier_name: hiboutikText,
|
||||||
/** 1 = enabled, 0 = disabled. */
|
/** 1 = enabled, 0 = disabled. */
|
||||||
supplier_enabled: number;
|
supplier_enabled: hiboutikInt,
|
||||||
}
|
});
|
||||||
|
|
||||||
|
export type SupplierHiboutikDto = z.output<typeof supplierHiboutikSchema>;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { BrandHiboutikRepository } from '../../hiboutik/brand.hiboutik.repositor
|
|||||||
import { BrandDatabaseRepository } from '../../database/brand.database.repository';
|
import { BrandDatabaseRepository } from '../../database/brand.database.repository';
|
||||||
import { BrandConverter } from '../../models/converter/brand.converter';
|
import { BrandConverter } from '../../models/converter/brand.converter';
|
||||||
import { forEachSafely } from '../../utils/sync';
|
import { forEachSafely } from '../../utils/sync';
|
||||||
|
import { SlugService } from '../slug/slug.service';
|
||||||
|
import { SlugEntityType } from '../../../prisma/generated/client';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BrandHiboutikService {
|
export class BrandHiboutikService {
|
||||||
@@ -12,9 +14,10 @@ export class BrandHiboutikService {
|
|||||||
private readonly brandHiboutikRepository: BrandHiboutikRepository,
|
private readonly brandHiboutikRepository: BrandHiboutikRepository,
|
||||||
private readonly brandDatabaseRepository: BrandDatabaseRepository,
|
private readonly brandDatabaseRepository: BrandDatabaseRepository,
|
||||||
private readonly brandConverter: BrandConverter,
|
private readonly brandConverter: BrandConverter,
|
||||||
|
private readonly slugService: SlugService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/** Mirrors Hiboutik brands: upserts each of them, then deletes the brands missing from Hiboutik. */
|
/** Mirrors Hiboutik brands: upserts each of them, deletes the brands missing from Hiboutik, then gives a slug to the new ones. */
|
||||||
async syncBrands() {
|
async syncBrands() {
|
||||||
const hiboutikBrands = await this.brandHiboutikRepository.getBrands();
|
const hiboutikBrands = await this.brandHiboutikRepository.getBrands();
|
||||||
// One upsert per brand: an invalid brand must not prevent the others from being saved
|
// One upsert per brand: an invalid brand must not prevent the others from being saved
|
||||||
@@ -26,7 +29,8 @@ export class BrandHiboutikService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
||||||
const nbDeleted = await this.brandDatabaseRepository.deleteMissing(hiboutikBrands.map((dto) => Number(dto.brand_id)));
|
const nbDeleted = await this.brandDatabaseRepository.deleteMissing(hiboutikBrands.map((dto) => dto.brand_id));
|
||||||
|
await this.slugService.assignMissingSlugs(SlugEntityType.BRAND);
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Brands sync ended: ${hiboutikBrands.length - nbFailed}/${hiboutikBrands.length} synced, ${nbFailed} failed, ${nbDeleted} deleted.`,
|
`Brands sync ended: ${hiboutikBrands.length - nbFailed}/${hiboutikBrands.length} synced, ${nbFailed} failed, ${nbDeleted} deleted.`,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,20 +3,20 @@ import { CategoryHiboutikService } from './category.hiboutik.service';
|
|||||||
import { CategoryConverter } from '../../models/converter/category.converter';
|
import { CategoryConverter } from '../../models/converter/category.converter';
|
||||||
import { CategoryHiboutikDto } from '../../models/hiboutikDTO/category.hiboutik.dto';
|
import { CategoryHiboutikDto } from '../../models/hiboutikDTO/category.hiboutik.dto';
|
||||||
|
|
||||||
const hiboutikCategory = (id: number, parentId: number, overrides: Partial<CategoryHiboutikDto> = {}): CategoryHiboutikDto =>
|
const hiboutikCategory = (id: number, parentId: number, overrides: Partial<CategoryHiboutikDto> = {}): CategoryHiboutikDto => ({
|
||||||
({
|
category_id: id,
|
||||||
category_id: id,
|
category_name: `Category ${id}`,
|
||||||
category_name: `Category ${id}`,
|
category_id_parent: parentId,
|
||||||
category_id_parent: parentId,
|
category_enabled: 1,
|
||||||
category_enabled: 1,
|
category_enabled_www: 0,
|
||||||
category_enabled_www: 0,
|
category_position: 3,
|
||||||
category_position: 3,
|
...overrides,
|
||||||
...overrides,
|
});
|
||||||
}) as CategoryHiboutikDto;
|
|
||||||
|
|
||||||
describe('CategoryHiboutikService', () => {
|
describe('CategoryHiboutikService', () => {
|
||||||
const categoryHiboutikRepository = { getCategories: jest.fn() };
|
const categoryHiboutikRepository = { getCategories: jest.fn() };
|
||||||
const categoryDatabaseRepository = { upsert: jest.fn(), setParent: jest.fn(), deleteMissing: jest.fn() };
|
const categoryDatabaseRepository = { upsert: jest.fn(), setParent: jest.fn(), deleteMissing: jest.fn() };
|
||||||
|
const slugService = { assignMissingSlugs: jest.fn() };
|
||||||
let service: CategoryHiboutikService;
|
let service: CategoryHiboutikService;
|
||||||
let calls: string[];
|
let calls: string[];
|
||||||
|
|
||||||
@@ -42,6 +42,7 @@ describe('CategoryHiboutikService', () => {
|
|||||||
categoryHiboutikRepository as never,
|
categoryHiboutikRepository as never,
|
||||||
categoryDatabaseRepository as never,
|
categoryDatabaseRepository as never,
|
||||||
new CategoryConverter(),
|
new CategoryConverter(),
|
||||||
|
slugService as never,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -94,4 +95,16 @@ describe('CategoryHiboutikService', () => {
|
|||||||
|
|
||||||
expect(categoryDatabaseRepository.deleteMissing).not.toHaveBeenCalled();
|
expect(categoryDatabaseRepository.deleteMissing).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
it('generates the slugs of new categories once they are saved', async () => {
|
||||||
|
categoryHiboutikRepository.getCategories.mockResolvedValue([hiboutikCategory(1, 0)]);
|
||||||
|
slugService.assignMissingSlugs.mockImplementation(() => {
|
||||||
|
calls.push('slugs');
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
await service.syncCategories();
|
||||||
|
|
||||||
|
expect(slugService.assignMissingSlugs).toHaveBeenCalledWith('CATEGORY');
|
||||||
|
expect(calls[calls.length - 1]).toBe('slugs');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { CategoryHiboutikRepository } from '../../hiboutik/category.hiboutik.rep
|
|||||||
import { CategoryDatabaseRepository } from '../../database/category.database.repository';
|
import { CategoryDatabaseRepository } from '../../database/category.database.repository';
|
||||||
import { CategoryConverter } from '../../models/converter/category.converter';
|
import { CategoryConverter } from '../../models/converter/category.converter';
|
||||||
import { forEachSafely } from '../../utils/sync';
|
import { forEachSafely } from '../../utils/sync';
|
||||||
|
import { SlugService } from '../slug/slug.service';
|
||||||
|
import { SlugEntityType } from '../../../prisma/generated/client';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CategoryHiboutikService {
|
export class CategoryHiboutikService {
|
||||||
@@ -12,11 +14,12 @@ export class CategoryHiboutikService {
|
|||||||
private readonly categoryHiboutikRepository: CategoryHiboutikRepository,
|
private readonly categoryHiboutikRepository: CategoryHiboutikRepository,
|
||||||
private readonly categoryDatabaseRepository: CategoryDatabaseRepository,
|
private readonly categoryDatabaseRepository: CategoryDatabaseRepository,
|
||||||
private readonly categoryConverter: CategoryConverter,
|
private readonly categoryConverter: CategoryConverter,
|
||||||
|
private readonly slugService: SlugService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mirrors Hiboutik categories and their tree: upserts each of them, links them to their parent,
|
* Mirrors Hiboutik categories and their tree: upserts each of them, links them to their parent,
|
||||||
* then deletes the categories missing from Hiboutik.
|
* deletes the categories missing from Hiboutik, then gives a slug to the new ones.
|
||||||
*/
|
*/
|
||||||
async syncCategories() {
|
async syncCategories() {
|
||||||
const hiboutikCategories = await this.categoryHiboutikRepository.getCategories();
|
const hiboutikCategories = await this.categoryHiboutikRepository.getCategories();
|
||||||
@@ -32,13 +35,14 @@ export class CategoryHiboutikService {
|
|||||||
// Pass 2: parent links, once every category exists (a child can come before its parent in Hiboutik's list)
|
// Pass 2: parent links, once every category exists (a child can come before its parent in Hiboutik's list)
|
||||||
const nbParentsFailed = await forEachSafely(
|
const nbParentsFailed = await forEachSafely(
|
||||||
hiboutikCategories,
|
hiboutikCategories,
|
||||||
(dto) => this.categoryDatabaseRepository.setParent(Number(dto.category_id), Number(dto.category_id_parent) || null),
|
(dto) => this.categoryDatabaseRepository.setParent(dto.category_id, dto.category_id_parent || null),
|
||||||
this.logger,
|
this.logger,
|
||||||
(dto) => `parent of Hiboutik category ${dto.category_id}`,
|
(dto) => `parent of Hiboutik category ${dto.category_id}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
||||||
const nbDeleted = await this.categoryDatabaseRepository.deleteMissing(hiboutikCategories.map((dto) => Number(dto.category_id)));
|
const nbDeleted = await this.categoryDatabaseRepository.deleteMissing(hiboutikCategories.map((dto) => dto.category_id));
|
||||||
|
await this.slugService.assignMissingSlugs(SlugEntityType.CATEGORY);
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Categories sync ended: ${hiboutikCategories.length - nbFailed}/${hiboutikCategories.length} synced, ${nbFailed} failed, ` +
|
`Categories sync ended: ${hiboutikCategories.length - nbFailed}/${hiboutikCategories.length} synced, ${nbFailed} failed, ` +
|
||||||
`${nbParentsFailed} parent links failed, ${nbDeleted} deleted.`,
|
`${nbParentsFailed} parent links failed, ${nbDeleted} deleted.`,
|
||||||
|
|||||||
@@ -2,8 +2,13 @@ import { Logger } from '@nestjs/common';
|
|||||||
import { CustomerHiboutikService } from './customer.hiboutik.service';
|
import { CustomerHiboutikService } from './customer.hiboutik.service';
|
||||||
|
|
||||||
describe('CustomerHiboutikService', () => {
|
describe('CustomerHiboutikService', () => {
|
||||||
const customerHiboutikRepository = { getCustomers: jest.fn(), getCustomerById: jest.fn(), getAddressById: jest.fn() };
|
const customerHiboutikRepository = {
|
||||||
const customerDatabaseRepository = { upsert: jest.fn(), deleteMissing: jest.fn() };
|
getCustomers: jest.fn(),
|
||||||
|
getCustomerById: jest.fn(),
|
||||||
|
findCustomerById: jest.fn(),
|
||||||
|
getAddressById: jest.fn(),
|
||||||
|
};
|
||||||
|
const customerDatabaseRepository = { upsert: jest.fn(), getAllHiboutikIds: jest.fn(), deleteByHiboutikIds: jest.fn() };
|
||||||
const customerConverter = { fromHiboutik: jest.fn() };
|
const customerConverter = { fromHiboutik: jest.fn() };
|
||||||
const customerAddressDatabaseRepository = { upsert: jest.fn(), deleteMissing: jest.fn() };
|
const customerAddressDatabaseRepository = { upsert: jest.fn(), deleteMissing: jest.fn() };
|
||||||
const customerAddressConverter = { fromHiboutik: jest.fn((dto: unknown) => dto) };
|
const customerAddressConverter = { fromHiboutik: jest.fn((dto: unknown) => dto) };
|
||||||
@@ -23,7 +28,9 @@ describe('CustomerHiboutikService', () => {
|
|||||||
customerHiboutikRepository.getCustomers.mockResolvedValue([]);
|
customerHiboutikRepository.getCustomers.mockResolvedValue([]);
|
||||||
customerHiboutikRepository.getCustomerById.mockResolvedValue({ customers_id: 1, addresses: [] });
|
customerHiboutikRepository.getCustomerById.mockResolvedValue({ customers_id: 1, addresses: [] });
|
||||||
customerDatabaseRepository.upsert.mockResolvedValue({ id: 'customer-uuid' });
|
customerDatabaseRepository.upsert.mockResolvedValue({ id: 'customer-uuid' });
|
||||||
customerDatabaseRepository.deleteMissing.mockResolvedValue(0);
|
customerDatabaseRepository.getAllHiboutikIds.mockResolvedValue([]);
|
||||||
|
customerDatabaseRepository.deleteByHiboutikIds.mockImplementation((ids: number[]) => Promise.resolve(ids.length));
|
||||||
|
customerHiboutikRepository.findCustomerById.mockResolvedValue(null);
|
||||||
customerAddressDatabaseRepository.deleteMissing.mockResolvedValue(0);
|
customerAddressDatabaseRepository.deleteMissing.mockResolvedValue(0);
|
||||||
orderDatabaseRepository.getCompletedSaleIds.mockResolvedValue(new Set());
|
orderDatabaseRepository.getCompletedSaleIds.mockResolvedValue(new Set());
|
||||||
saleHiboutikRepository.getSaleById.mockImplementation((id: number) => Promise.resolve({ sale_id: id, quotation: 0 }));
|
saleHiboutikRepository.getSaleById.mockImplementation((id: number) => Promise.resolve({ sale_id: id, quotation: 0 }));
|
||||||
@@ -39,27 +46,61 @@ describe('CustomerHiboutikService', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('deletes customers missing from Hiboutik', async () => {
|
describe('deletions', () => {
|
||||||
customerHiboutikRepository.getCustomers.mockResolvedValue([{ customers_id: 1 }, { customers_id: '2' }]);
|
it('deletes the stored customers missing from the list once Hiboutik confirms they no longer exist', async () => {
|
||||||
|
customerHiboutikRepository.getCustomers.mockResolvedValue([{ customers_id: 1 }, { customers_id: 2 }]);
|
||||||
|
customerDatabaseRepository.getAllHiboutikIds.mockResolvedValue([1, 2, 3, 4]);
|
||||||
|
|
||||||
await service.syncCustomers();
|
await service.syncCustomers();
|
||||||
|
|
||||||
expect(customerDatabaseRepository.deleteMissing).toHaveBeenCalledWith([1, 2]);
|
expect(customerHiboutikRepository.findCustomerById.mock.calls).toEqual([[3], [4]]);
|
||||||
expect(customerDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
expect(customerDatabaseRepository.deleteByHiboutikIds).toHaveBeenCalledWith([3, 4]);
|
||||||
});
|
expect(customerDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
|
||||||
it('deletes every customer when Hiboutik answers with an empty list', async () => {
|
it('keeps and syncs a customer missing from the list (page shift) that still exists in Hiboutik', async () => {
|
||||||
await service.syncCustomers();
|
customerHiboutikRepository.getCustomers.mockResolvedValue([{ customers_id: 1 }]);
|
||||||
|
customerDatabaseRepository.getAllHiboutikIds.mockResolvedValue([1, 2]);
|
||||||
|
customerHiboutikRepository.findCustomerById.mockResolvedValue({ customers_id: 2 });
|
||||||
|
|
||||||
expect(customerDatabaseRepository.deleteMissing).toHaveBeenCalledWith([]);
|
await service.syncCustomers();
|
||||||
});
|
|
||||||
|
|
||||||
it('deletes nothing when the Hiboutik call fails', async () => {
|
expect(customerDatabaseRepository.deleteByHiboutikIds).toHaveBeenCalledWith([]);
|
||||||
customerHiboutikRepository.getCustomers.mockRejectedValue(new Error('Request failed with status code 500'));
|
expect(customerHiboutikRepository.getCustomerById.mock.calls).toEqual([[1], [2]]);
|
||||||
|
});
|
||||||
|
|
||||||
await expect(service.syncCustomers()).rejects.toThrow('500');
|
it('keeps a customer whose check fails', async () => {
|
||||||
|
customerDatabaseRepository.getAllHiboutikIds.mockResolvedValue([3, 4]);
|
||||||
|
customerHiboutikRepository.findCustomerById.mockRejectedValueOnce(new Error('Request failed with status code 500'));
|
||||||
|
|
||||||
expect(customerDatabaseRepository.deleteMissing).not.toHaveBeenCalled();
|
await service.syncCustomers();
|
||||||
|
|
||||||
|
expect(customerDatabaseRepository.deleteByHiboutikIds).toHaveBeenCalledWith([4]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('deletes every confirmed customer when Hiboutik answers with an empty list', async () => {
|
||||||
|
customerDatabaseRepository.getAllHiboutikIds.mockResolvedValue([1, 2]);
|
||||||
|
|
||||||
|
await service.syncCustomers();
|
||||||
|
|
||||||
|
expect(customerDatabaseRepository.deleteByHiboutikIds).toHaveBeenCalledWith([1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('deletes nothing when the Hiboutik list call fails', async () => {
|
||||||
|
customerHiboutikRepository.getCustomers.mockRejectedValue(new Error('Request failed with status code 500'));
|
||||||
|
|
||||||
|
await expect(service.syncCustomers()).rejects.toThrow('500');
|
||||||
|
|
||||||
|
expect(customerDatabaseRepository.deleteByHiboutikIds).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('syncs once a customer listed twice', async () => {
|
||||||
|
customerHiboutikRepository.getCustomers.mockResolvedValue([{ customers_id: 1 }, { customers_id: 1 }]);
|
||||||
|
|
||||||
|
await service.syncCustomers();
|
||||||
|
|
||||||
|
expect(customerHiboutikRepository.getCustomerById).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('keeps syncing the other customers when one fails', async () => {
|
it('keeps syncing the other customers when one fails', async () => {
|
||||||
@@ -80,8 +121,8 @@ describe('CustomerHiboutikService', () => {
|
|||||||
customerHiboutikRepository.getCustomerById.mockResolvedValue({
|
customerHiboutikRepository.getCustomerById.mockResolvedValue({
|
||||||
customers_id: 1,
|
customers_id: 1,
|
||||||
addresses: [
|
addresses: [
|
||||||
{ address_id: 10, customers_id: 1 },
|
{ address_id: 10, customers_id: 1, company: '' },
|
||||||
{ address_id: '11', customers_id: 999 },
|
{ address_id: 11, customers_id: 999, company: '' },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -89,7 +130,7 @@ describe('CustomerHiboutikService', () => {
|
|||||||
|
|
||||||
expect(customerAddressDatabaseRepository.deleteMissing).toHaveBeenCalledWith('customer-uuid', [10, 11]);
|
expect(customerAddressDatabaseRepository.deleteMissing).toHaveBeenCalledWith('customer-uuid', [10, 11]);
|
||||||
expect(customerAddressDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
expect(customerAddressDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
||||||
expect(customerAddressConverter.fromHiboutik).toHaveBeenLastCalledWith({ address_id: '11', customers_id: 1 });
|
expect(customerAddressConverter.fromHiboutik).toHaveBeenLastCalledWith({ address_id: 11, customers_id: 1, company: '' });
|
||||||
expect(customerAddressDatabaseRepository.deleteMissing.mock.invocationCallOrder[0]).toBeLessThan(
|
expect(customerAddressDatabaseRepository.deleteMissing.mock.invocationCallOrder[0]).toBeLessThan(
|
||||||
customerAddressDatabaseRepository.upsert.mock.invocationCallOrder[0],
|
customerAddressDatabaseRepository.upsert.mock.invocationCallOrder[0],
|
||||||
);
|
);
|
||||||
@@ -155,7 +196,10 @@ describe('CustomerHiboutikService', () => {
|
|||||||
it('keeps syncing the other addresses when one fails', async () => {
|
it('keeps syncing the other addresses when one fails', async () => {
|
||||||
customerHiboutikRepository.getCustomerById.mockResolvedValue({
|
customerHiboutikRepository.getCustomerById.mockResolvedValue({
|
||||||
customers_id: 1,
|
customers_id: 1,
|
||||||
addresses: [{ address_id: 10 }, { address_id: 11 }],
|
addresses: [
|
||||||
|
{ address_id: 10, company: '' },
|
||||||
|
{ address_id: 11, company: '' },
|
||||||
|
],
|
||||||
});
|
});
|
||||||
customerAddressDatabaseRepository.upsert.mockRejectedValueOnce(new Error('boom'));
|
customerAddressDatabaseRepository.upsert.mockRejectedValueOnce(new Error('boom'));
|
||||||
|
|
||||||
@@ -171,7 +215,7 @@ describe('CustomerHiboutikService', () => {
|
|||||||
customerHiboutikRepository.getCustomerById.mockResolvedValue({
|
customerHiboutikRepository.getCustomerById.mockResolvedValue({
|
||||||
customers_id: 1,
|
customers_id: 1,
|
||||||
addresses: [],
|
addresses: [],
|
||||||
sales: [{ sale_id: 5 }, { sale_id: '6' }, { sale_id: 7 }],
|
sales: [{ sale_id: 5 }, { sale_id: 6 }, { sale_id: 7 }],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -180,7 +224,7 @@ describe('CustomerHiboutikService', () => {
|
|||||||
|
|
||||||
await service.syncCustomers();
|
await service.syncCustomers();
|
||||||
|
|
||||||
expect(orderDatabaseRepository.getCompletedSaleIds).toHaveBeenCalledWith([5, 6, 7]);
|
expect(orderDatabaseRepository.getCompletedSaleIds).toHaveBeenCalledWith(1, [5, 6, 7]);
|
||||||
expect(saleHiboutikRepository.getSaleById.mock.calls.map((call: unknown[]) => call[0])).toEqual([5, 7]);
|
expect(saleHiboutikRepository.getSaleById.mock.calls.map((call: unknown[]) => call[0])).toEqual([5, 7]);
|
||||||
expect(orderDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
expect(orderDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
||||||
expect(orderConverter.fromHiboutik).toHaveBeenCalledWith({ sale_id: 5, quotation: 0, customer_id: 1 });
|
expect(orderConverter.fromHiboutik).toHaveBeenCalledWith({ sale_id: 5, quotation: 0, customer_id: 1 });
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import { forEachSafely } from '../../utils/sync';
|
|||||||
import { SaleHiboutikRepository } from '../../hiboutik/sale.hiboutik.repository';
|
import { SaleHiboutikRepository } from '../../hiboutik/sale.hiboutik.repository';
|
||||||
import { OrderDatabaseRepository } from '../../database/order.database.repository';
|
import { OrderDatabaseRepository } from '../../database/order.database.repository';
|
||||||
import { OrderConverter } from '../../models/converter/order.converter';
|
import { OrderConverter } from '../../models/converter/order.converter';
|
||||||
|
import { rethrowIfRateLimited } from '../../hiboutik/hiboutik.client';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CustomerHiboutikService {
|
export class CustomerHiboutikService {
|
||||||
@@ -30,35 +31,66 @@ export class CustomerHiboutikService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mirrors Hiboutik customers: deletes the customers missing from Hiboutik, then fetches and upserts each of them
|
* Mirrors Hiboutik customers: deletes the customers Hiboutik confirms as deleted, then fetches and upserts each of
|
||||||
* with its addresses and orders (one API call per customer, as the list endpoint lacks addresses and sales, plus one
|
* them with its addresses and orders (one API call per customer, as the list endpoint lacks addresses and sales, plus
|
||||||
* per sale not yet stored as completed).
|
* one per sale not yet stored as completed).
|
||||||
*/
|
*/
|
||||||
async syncCustomers() {
|
async syncCustomers() {
|
||||||
const hiboutikCustomers = await this.customerHiboutikRepository.getCustomers();
|
const hiboutikCustomers = await this.customerHiboutikRepository.getCustomers();
|
||||||
|
// A page shift can list a customer twice: sync it once
|
||||||
|
const listedIds = [...new Set(hiboutikCustomers.map((dto) => dto.customers_id))];
|
||||||
|
|
||||||
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
const { nbDeleted, unlistedIds } = await this.deleteMissingCustomers(listedIds);
|
||||||
const nbDeleted = await this.customerDatabaseRepository.deleteMissing(hiboutikCustomers.map((dto) => Number(dto.customers_id)));
|
const customerIds = [...listedIds, ...unlistedIds];
|
||||||
|
|
||||||
let nbFailed = 0;
|
let nbFailed = 0;
|
||||||
let nbFailedAddresses = 0;
|
let nbFailedAddresses = 0;
|
||||||
let nbFailedOrders = 0;
|
let nbFailedOrders = 0;
|
||||||
for (const hiboutikCustomer of hiboutikCustomers) {
|
for (const customerId of customerIds) {
|
||||||
try {
|
try {
|
||||||
const hiboutikFullCustomer = await this.customerHiboutikRepository.getCustomerById(hiboutikCustomer.customers_id);
|
const hiboutikFullCustomer = await this.customerHiboutikRepository.getCustomerById(customerId);
|
||||||
const customer = await this.customerDatabaseRepository.upsert(this.customerConverter.fromHiboutik(hiboutikFullCustomer));
|
const customer = await this.customerDatabaseRepository.upsert(this.customerConverter.fromHiboutik(hiboutikFullCustomer));
|
||||||
nbFailedAddresses += await this.syncAddresses(customer.id, hiboutikFullCustomer);
|
nbFailedAddresses += await this.syncAddresses(customer.id, hiboutikFullCustomer);
|
||||||
nbFailedOrders += await this.syncOrders(hiboutikFullCustomer);
|
nbFailedOrders += await this.syncOrders(hiboutikFullCustomer);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
rethrowIfRateLimited(error);
|
||||||
nbFailed++;
|
nbFailed++;
|
||||||
this.logger.error(`Failed to sync Hiboutik customer ${hiboutikCustomer.customers_id}`, (error as Error)?.stack);
|
this.logger.error(`Failed to sync Hiboutik customer ${customerId}`, (error as Error)?.stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Customers sync ended: ${hiboutikCustomers.length - nbFailed}/${hiboutikCustomers.length} synced, ${nbFailed} failed, ${nbDeleted} deleted, ${nbFailedAddresses} addresses failed, ${nbFailedOrders} orders failed.`,
|
`Customers sync ended: ${customerIds.length - nbFailed}/${customerIds.length} synced, ${nbFailed} failed, ${nbDeleted} deleted, ` +
|
||||||
|
`${unlistedIds.length} missing from the list but still in Hiboutik, ${nbFailedAddresses} addresses failed, ${nbFailedOrders} orders failed.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes the stored customers missing from the Hiboutik list, once Hiboutik confirms one by one that they no longer
|
||||||
|
* exist. The list is paginated: a customer created or deleted in Hiboutik while it is walked shifts the following
|
||||||
|
* pages, so a customer can be missing from it without being deleted. Hiboutik is trusted: a 404 or an empty 2xx
|
||||||
|
* answer is a confirmation. A customer whose check fails is kept.
|
||||||
|
* @returns the number of deleted customers, and the ids of the customers that still exist although not listed.
|
||||||
|
*/
|
||||||
|
private async deleteMissingCustomers(listedIds: number[]): Promise<{ nbDeleted: number; unlistedIds: number[] }> {
|
||||||
|
const listed = new Set(listedIds);
|
||||||
|
const missingIds = (await this.customerDatabaseRepository.getAllHiboutikIds()).filter((id) => !listed.has(id));
|
||||||
|
|
||||||
|
const deletedIds: number[] = [];
|
||||||
|
const unlistedIds: number[] = [];
|
||||||
|
for (const id of missingIds) {
|
||||||
|
try {
|
||||||
|
const customer = await this.customerHiboutikRepository.findCustomerById(id);
|
||||||
|
(customer ? unlistedIds : deletedIds).push(id);
|
||||||
|
} catch (error) {
|
||||||
|
rethrowIfRateLimited(error);
|
||||||
|
this.logger.warn(`Failed to check whether Hiboutik customer ${id} still exists, it is kept: ${(error as Error)?.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const nbDeleted = await this.customerDatabaseRepository.deleteByHiboutikIds(deletedIds);
|
||||||
|
return { nbDeleted, unlistedIds };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mirrors the addresses embedded in a full Hiboutik customer: deletes the ones missing from Hiboutik, then upserts
|
* Mirrors the addresses embedded in a full Hiboutik customer: deletes the ones missing from Hiboutik, then upserts
|
||||||
* each of them (a failing address is logged and skipped).
|
* each of them (a failing address is logged and skipped).
|
||||||
@@ -73,13 +105,13 @@ export class CustomerHiboutikService {
|
|||||||
|
|
||||||
await this.customerAddressDatabaseRepository.deleteMissing(
|
await this.customerAddressDatabaseRepository.deleteMissing(
|
||||||
customerId,
|
customerId,
|
||||||
hiboutikCustomer.addresses.map((dto) => Number(dto.address_id)),
|
hiboutikCustomer.addresses.map((dto) => dto.address_id),
|
||||||
);
|
);
|
||||||
return forEachSafely(
|
return forEachSafely(
|
||||||
hiboutikCustomer.addresses,
|
hiboutikCustomer.addresses,
|
||||||
// Linked to the parent customer, whatever the embedded customers_id says
|
// Linked to the parent customer, whatever the embedded customers_id says
|
||||||
async (dto) => {
|
async (dto) => {
|
||||||
const address = dto.company?.trim() ? await this.withCompanyDetails(dto) : dto;
|
const address = dto.company.trim() ? await this.withCompanyDetails(dto) : dto;
|
||||||
await this.customerAddressDatabaseRepository.upsert(
|
await this.customerAddressDatabaseRepository.upsert(
|
||||||
this.customerAddressConverter.fromHiboutik({ ...address, customers_id: hiboutikCustomer.customers_id }),
|
this.customerAddressConverter.fromHiboutik({ ...address, customers_id: hiboutikCustomer.customers_id }),
|
||||||
);
|
);
|
||||||
@@ -106,6 +138,7 @@ export class CustomerHiboutikService {
|
|||||||
updated_at: details.updated_at,
|
updated_at: details.updated_at,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
rethrowIfRateLimited(error);
|
||||||
this.logger.warn(`Failed to fetch company details of Hiboutik address ${dto.address_id}: ${(error as Error)?.message}`);
|
this.logger.warn(`Failed to fetch company details of Hiboutik address ${dto.address_id}: ${(error as Error)?.message}`);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
@@ -124,15 +157,16 @@ export class CustomerHiboutikService {
|
|||||||
|
|
||||||
// A completed sale no longer changes in Hiboutik: no need to fetch it again
|
// A completed sale no longer changes in Hiboutik: no need to fetch it again
|
||||||
const completedSaleIds = await this.orderDatabaseRepository.getCompletedSaleIds(
|
const completedSaleIds = await this.orderDatabaseRepository.getCompletedSaleIds(
|
||||||
hiboutikCustomer.sales.map((dto) => Number(dto.sale_id)),
|
hiboutikCustomer.customers_id,
|
||||||
|
hiboutikCustomer.sales.map((dto) => dto.sale_id),
|
||||||
);
|
);
|
||||||
const salesToSync = hiboutikCustomer.sales.filter((dto) => !completedSaleIds.has(Number(dto.sale_id)));
|
const salesToSync = hiboutikCustomer.sales.filter((dto) => !completedSaleIds.has(dto.sale_id));
|
||||||
|
|
||||||
return forEachSafely(
|
return forEachSafely(
|
||||||
salesToSync,
|
salesToSync,
|
||||||
async (dto) => {
|
async (dto) => {
|
||||||
const sale = await this.saleHiboutikRepository.getSaleById(dto.sale_id);
|
const sale = await this.saleHiboutikRepository.getSaleById(dto.sale_id);
|
||||||
if (Number(sale.quotation) === 1) return;
|
if (sale.quotation === 1) return;
|
||||||
// Linked to the parent customer, whatever the sale says
|
// Linked to the parent customer, whatever the sale says
|
||||||
await this.orderDatabaseRepository.upsert(
|
await this.orderDatabaseRepository.upsert(
|
||||||
this.orderConverter.fromHiboutik({ ...sale, customer_id: hiboutikCustomer.customers_id }),
|
this.orderConverter.fromHiboutik({ ...sale, customer_id: hiboutikCustomer.customers_id }),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Logger } from '@nestjs/common';
|
import { Logger, ServiceUnavailableException } from '@nestjs/common';
|
||||||
import { HiboutikService } from './hiboutik.service';
|
import { HiboutikService } from './hiboutik.service';
|
||||||
|
import { HiboutikRateLimitError } from '../../hiboutik/hiboutik.client';
|
||||||
|
|
||||||
describe('HiboutikService', () => {
|
describe('HiboutikService', () => {
|
||||||
const brandService = { syncBrands: jest.fn() };
|
const brandService = { syncBrands: jest.fn() };
|
||||||
@@ -9,12 +10,16 @@ describe('HiboutikService', () => {
|
|||||||
const customerService = { syncCustomers: jest.fn() };
|
const customerService = { syncCustomers: jest.fn() };
|
||||||
const syncLock = { tryAcquire: jest.fn() };
|
const syncLock = { tryAcquire: jest.fn() };
|
||||||
const release = jest.fn();
|
const release = jest.fn();
|
||||||
|
// SYNC_SHUTDOWN_TIMEOUT of 1 s
|
||||||
|
const configService = { get: jest.fn().mockReturnValue(1) };
|
||||||
let service: HiboutikService;
|
let service: HiboutikService;
|
||||||
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
||||||
|
|
||||||
|
let logWarn: jest.SpyInstance;
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
|
jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
|
||||||
jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
|
logWarn = jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
|
||||||
jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
|
jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -34,6 +39,7 @@ describe('HiboutikService', () => {
|
|||||||
productService as never,
|
productService as never,
|
||||||
supplierService as never,
|
supplierService as never,
|
||||||
syncLock as never,
|
syncLock as never,
|
||||||
|
configService as never,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -45,7 +51,8 @@ describe('HiboutikService', () => {
|
|||||||
productService.syncProducts.mockImplementation(() => Promise.resolve(order.push('products')));
|
productService.syncProducts.mockImplementation(() => Promise.resolve(order.push('products')));
|
||||||
customerService.syncCustomers.mockImplementation(() => Promise.resolve(order.push('customers')));
|
customerService.syncCustomers.mockImplementation(() => Promise.resolve(order.push('customers')));
|
||||||
|
|
||||||
await service.syncAll();
|
// private method, reached through an index access on purpose
|
||||||
|
await service['syncAll']();
|
||||||
|
|
||||||
expect(order).toEqual(['brands', 'categories', 'suppliers', 'products', 'customers']);
|
expect(order).toEqual(['brands', 'categories', 'suppliers', 'products', 'customers']);
|
||||||
});
|
});
|
||||||
@@ -53,7 +60,8 @@ describe('HiboutikService', () => {
|
|||||||
it('continues with the next steps when one fails', async () => {
|
it('continues with the next steps when one fails', async () => {
|
||||||
brandService.syncBrands.mockRejectedValue(new Error('boom'));
|
brandService.syncBrands.mockRejectedValue(new Error('boom'));
|
||||||
|
|
||||||
await service.syncAll();
|
// private method, reached through an index access on purpose
|
||||||
|
await service['syncAll']();
|
||||||
|
|
||||||
expect(productService.syncProducts).toHaveBeenCalled();
|
expect(productService.syncProducts).toHaveBeenCalled();
|
||||||
expect(customerService.syncCustomers).toHaveBeenCalled();
|
expect(customerService.syncCustomers).toHaveBeenCalled();
|
||||||
@@ -62,30 +70,17 @@ describe('HiboutikService', () => {
|
|||||||
it('refuses a second synchronization while one is running, and unlocks at the end', async () => {
|
it('refuses a second synchronization while one is running, and unlocks at the end', async () => {
|
||||||
let finishBrands: () => void = () => undefined;
|
let finishBrands: () => void = () => undefined;
|
||||||
brandService.syncBrands.mockReturnValue(new Promise<void>((resolve) => (finishBrands = resolve)));
|
brandService.syncBrands.mockReturnValue(new Promise<void>((resolve) => (finishBrands = resolve)));
|
||||||
|
// The lock is the only guard: the second request does not get it, even from the same instance
|
||||||
|
syncLock.tryAcquire.mockResolvedValueOnce(release).mockResolvedValueOnce(null);
|
||||||
|
|
||||||
await expect(service.startSync()).resolves.toBe(true);
|
await expect(service.startSync()).resolves.toBe(true);
|
||||||
expect(service.isSyncInProgress).toBe(true);
|
|
||||||
await expect(service.startSync()).resolves.toBe(false);
|
await expect(service.startSync()).resolves.toBe(false);
|
||||||
expect(syncLock.tryAcquire).toHaveBeenCalledTimes(1);
|
expect(brandService.syncBrands).toHaveBeenCalledTimes(1);
|
||||||
expect(release).not.toHaveBeenCalled();
|
expect(release).not.toHaveBeenCalled();
|
||||||
|
|
||||||
finishBrands();
|
finishBrands();
|
||||||
await flush();
|
await flush();
|
||||||
expect(release).toHaveBeenCalledTimes(1);
|
expect(release).toHaveBeenCalledTimes(1);
|
||||||
expect(service.isSyncInProgress).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('refuses concurrent calls on the same instance while the lock is being requested', async () => {
|
|
||||||
let grantLock: (value: typeof release) => void = () => undefined;
|
|
||||||
syncLock.tryAcquire.mockReturnValue(new Promise((resolve) => (grantLock = resolve)));
|
|
||||||
|
|
||||||
const first = service.startSync();
|
|
||||||
await expect(service.startSync()).resolves.toBe(false);
|
|
||||||
|
|
||||||
grantLock(release);
|
|
||||||
await expect(first).resolves.toBe(true);
|
|
||||||
await flush();
|
|
||||||
expect(syncLock.tryAcquire).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('starts nothing when another instance holds the lock', async () => {
|
it('starts nothing when another instance holds the lock', async () => {
|
||||||
@@ -93,7 +88,6 @@ describe('HiboutikService', () => {
|
|||||||
|
|
||||||
await expect(service.startSync()).resolves.toBe(false);
|
await expect(service.startSync()).resolves.toBe(false);
|
||||||
|
|
||||||
expect(service.isSyncInProgress).toBe(false);
|
|
||||||
expect(brandService.syncBrands).not.toHaveBeenCalled();
|
expect(brandService.syncBrands).not.toHaveBeenCalled();
|
||||||
expect(release).not.toHaveBeenCalled();
|
expect(release).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
@@ -102,19 +96,75 @@ describe('HiboutikService', () => {
|
|||||||
syncLock.tryAcquire.mockRejectedValueOnce(new Error('database unreachable'));
|
syncLock.tryAcquire.mockRejectedValueOnce(new Error('database unreachable'));
|
||||||
|
|
||||||
await expect(service.startSync()).rejects.toThrow('database unreachable');
|
await expect(service.startSync()).rejects.toThrow('database unreachable');
|
||||||
expect(service.isSyncInProgress).toBe(false);
|
expect(brandService.syncBrands).not.toHaveBeenCalled();
|
||||||
|
|
||||||
await expect(service.startSync()).resolves.toBe(true);
|
await expect(service.startSync()).resolves.toBe(true);
|
||||||
await flush();
|
await flush();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('releases the lock even when the synchronization crashes', async () => {
|
it('releases the lock even when the synchronization crashes', async () => {
|
||||||
jest.spyOn(service, 'syncAll').mockRejectedValue(new Error('crash'));
|
jest.spyOn(service as unknown as { syncAll: () => Promise<void> }, 'syncAll').mockRejectedValue(new Error('crash'));
|
||||||
|
|
||||||
await expect(service.startSync()).resolves.toBe(true);
|
await expect(service.startSync()).resolves.toBe(true);
|
||||||
await flush();
|
await flush();
|
||||||
|
|
||||||
expect(release).toHaveBeenCalledTimes(1);
|
expect(release).toHaveBeenCalledTimes(1);
|
||||||
expect(service.isSyncInProgress).toBe(false);
|
});
|
||||||
|
|
||||||
|
it('stops the synchronization when the Hiboutik rate limit is reached', async () => {
|
||||||
|
productService.syncProducts.mockRejectedValue(new HiboutikRateLimitError('/products'));
|
||||||
|
|
||||||
|
await service['syncAll']();
|
||||||
|
|
||||||
|
expect(customerService.syncCustomers).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('shutdown', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
jest.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('waits for the running synchronization to end, and refuses new ones meanwhile', async () => {
|
||||||
|
let finishBrands: () => void = () => undefined;
|
||||||
|
brandService.syncBrands.mockReturnValue(new Promise<void>((resolve) => (finishBrands = resolve)));
|
||||||
|
await service.startSync();
|
||||||
|
|
||||||
|
let shutDown = false;
|
||||||
|
const shutdown = service.beforeApplicationShutdown().then(() => (shutDown = true));
|
||||||
|
await flush();
|
||||||
|
expect(shutDown).toBe(false);
|
||||||
|
await expect(service.startSync()).rejects.toThrow(ServiceUnavailableException);
|
||||||
|
|
||||||
|
finishBrands();
|
||||||
|
await shutdown;
|
||||||
|
expect(release).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gives up waiting after the timeout', async () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
brandService.syncBrands.mockReturnValue(new Promise<void>(() => undefined));
|
||||||
|
await service.startSync();
|
||||||
|
|
||||||
|
const shutdown = service.beforeApplicationShutdown();
|
||||||
|
await jest.advanceTimersByTimeAsync(1_000);
|
||||||
|
|
||||||
|
await expect(shutdown).resolves.toBeUndefined();
|
||||||
|
expect(logWarn).toHaveBeenCalledWith(expect.stringContaining('did not end in time'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns at once when no synchronization is running', async () => {
|
||||||
|
await expect(service.beforeApplicationShutdown()).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('releases the lock and refuses when the shutdown began while the lock was requested', async () => {
|
||||||
|
syncLock.tryAcquire.mockImplementation(async () => {
|
||||||
|
await service.beforeApplicationShutdown();
|
||||||
|
return release;
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(service.startSync()).rejects.toThrow(ServiceUnavailableException);
|
||||||
|
expect(release).toHaveBeenCalledTimes(1);
|
||||||
|
expect(brandService.syncBrands).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { BeforeApplicationShutdown, Injectable, Logger, ServiceUnavailableException } from '@nestjs/common';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { BrandHiboutikService } from './brand.hiboutik.service';
|
import { BrandHiboutikService } from './brand.hiboutik.service';
|
||||||
import { CategoryHiboutikService } from './category.hiboutik.service';
|
import { CategoryHiboutikService } from './category.hiboutik.service';
|
||||||
import { CustomerHiboutikService } from './customer.hiboutik.service';
|
import { CustomerHiboutikService } from './customer.hiboutik.service';
|
||||||
import { SupplierHiboutikService } from './supplier.hiboutik.service';
|
import { SupplierHiboutikService } from './supplier.hiboutik.service';
|
||||||
import { ProductHiboutikService } from './product.hiboutik.service';
|
import { ProductHiboutikService } from './product.hiboutik.service';
|
||||||
import { ReleaseLock, SyncLockDatabaseRepository } from '../../database/syncLock.database.repository';
|
import { ReleaseLock, SyncLockDatabaseRepository } from '../../database/syncLock.database.repository';
|
||||||
|
import { HiboutikRateLimitError } from '../../hiboutik/hiboutik.client';
|
||||||
|
|
||||||
|
/** Default of `SYNC_SHUTDOWN_TIMEOUT`: below the 30 s grace period that Kubernetes gives before killing a container. */
|
||||||
|
const DEFAULT_SHUTDOWN_TIMEOUT_SECONDS = 25;
|
||||||
|
|
||||||
/** Entry point of the Hiboutik → database synchronization: runs each entity sync in dependency order. */
|
/** Entry point of the Hiboutik → database synchronization: runs each entity sync in dependency order. */
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HiboutikService {
|
export class HiboutikService implements BeforeApplicationShutdown {
|
||||||
private readonly logger = new Logger('HiboutikService');
|
private readonly logger = new Logger('HiboutikService');
|
||||||
/** Cheap in-process guard, checked before the database lock (which is the one that holds across instances). */
|
private readonly shutdownTimeoutMs: number;
|
||||||
private syncInProgress = false;
|
/** Synchronization running on this instance, if any. */
|
||||||
|
private running: Promise<void> | null = null;
|
||||||
|
private stopping = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly brandHiboutikService: BrandHiboutikService,
|
private readonly brandHiboutikService: BrandHiboutikService,
|
||||||
@@ -20,39 +27,60 @@ export class HiboutikService {
|
|||||||
private readonly productHiboutikService: ProductHiboutikService,
|
private readonly productHiboutikService: ProductHiboutikService,
|
||||||
private readonly supplierHiboutikService: SupplierHiboutikService,
|
private readonly supplierHiboutikService: SupplierHiboutikService,
|
||||||
private readonly syncLock: SyncLockDatabaseRepository,
|
private readonly syncLock: SyncLockDatabaseRepository,
|
||||||
) {}
|
configService: ConfigService,
|
||||||
|
) {
|
||||||
get isSyncInProgress(): boolean {
|
this.shutdownTimeoutMs = (configService.get<number>('SYNC_SHUTDOWN_TIMEOUT') ?? DEFAULT_SHUTDOWN_TIMEOUT_SECONDS) * 1000;
|
||||||
return this.syncInProgress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts a full synchronization in the background, if no other one is running on this instance or any other
|
* Starts a full synchronization in the background, if no other one is running on this instance or any other.
|
||||||
* (Postgres advisory lock, held until the synchronization ends).
|
* The Postgres advisory lock is the only guard: each request takes it on its own connection (its own session),
|
||||||
|
* so it also refuses concurrent calls on this very instance. It is held until the synchronization ends.
|
||||||
* @returns false if a synchronization is already running (nothing is started).
|
* @returns false if a synchronization is already running (nothing is started).
|
||||||
|
* @throws ServiceUnavailableException if this instance is shutting down (another instance can take it).
|
||||||
* @throws if the lock cannot be requested (e.g. database unreachable).
|
* @throws if the lock cannot be requested (e.g. database unreachable).
|
||||||
*/
|
*/
|
||||||
async startSync(): Promise<boolean> {
|
async startSync(): Promise<boolean> {
|
||||||
if (this.syncInProgress) return false;
|
this.refuseIfStopping();
|
||||||
// Set before the first await so that concurrent calls on this instance are refused while the lock is requested
|
const release = await this.syncLock.tryAcquire();
|
||||||
this.syncInProgress = true;
|
if (!release) return false;
|
||||||
|
|
||||||
let release: ReleaseLock | null;
|
// The shutdown may have begun while the lock was requested: it would not wait for this synchronization
|
||||||
try {
|
if (this.stopping) {
|
||||||
release = await this.syncLock.tryAcquire();
|
await release();
|
||||||
} catch (error) {
|
this.refuseIfStopping();
|
||||||
this.syncInProgress = false;
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
if (!release) {
|
|
||||||
this.syncInProgress = false;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void this.runLocked(release);
|
this.running = this.runLocked(release).finally(() => (this.running = null));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shutdown hook: refuses new synchronizations and waits for the running one to end, up to `SYNC_SHUTDOWN_TIMEOUT`
|
||||||
|
* seconds. Runs before Prisma disconnects (`PrismaService.onApplicationShutdown`). Past the timeout, the shutdown goes
|
||||||
|
* on: the synchronization then fails on its next database call, without corrupting anything (one transaction per
|
||||||
|
* product, the lock is released by Postgres with the connection).
|
||||||
|
*/
|
||||||
|
async beforeApplicationShutdown(): Promise<void> {
|
||||||
|
this.stopping = true;
|
||||||
|
const running = this.running;
|
||||||
|
if (!running) return;
|
||||||
|
|
||||||
|
this.logger.log(`Shutdown requested: waiting for the running Hiboutik synchronization (up to ${this.shutdownTimeoutMs / 1000} s)`);
|
||||||
|
let timer: NodeJS.Timeout | undefined;
|
||||||
|
const timedOut = new Promise<true>((resolve) => (timer = setTimeout(() => resolve(true), this.shutdownTimeoutMs)));
|
||||||
|
const result = await Promise.race([running.then(() => false as const), timedOut]);
|
||||||
|
clearTimeout(timer);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
this.logger.warn('The Hiboutik synchronization did not end in time: shutting down anyway, it will be interrupted');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private refuseIfStopping(): void {
|
||||||
|
if (this.stopping) throw new ServiceUnavailableException('This instance is shutting down, retry in a moment');
|
||||||
|
}
|
||||||
|
|
||||||
/** Runs the synchronization, then always releases the lock. */
|
/** Runs the synchronization, then always releases the lock. */
|
||||||
private async runLocked(release: ReleaseLock): Promise<void> {
|
private async runLocked(release: ReleaseLock): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@@ -61,12 +89,14 @@ export class HiboutikService {
|
|||||||
this.logger.error('Global Hiboutik synchronization crashed', (error as Error)?.stack);
|
this.logger.error('Global Hiboutik synchronization crashed', (error as Error)?.stack);
|
||||||
} finally {
|
} finally {
|
||||||
await release();
|
await release();
|
||||||
this.syncInProgress = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs every sync step in order. A failing step is logged and does not stop the next ones. */
|
/**
|
||||||
async syncAll() {
|
* Runs every sync step in order. A failing step is logged and does not stop the next ones, except when the Hiboutik
|
||||||
|
* rate limit is reached: the next steps would only fail too, so the synchronization stops there.
|
||||||
|
*/
|
||||||
|
private async syncAll() {
|
||||||
this.logger.log('Starting global Hiboutik synchronization');
|
this.logger.log('Starting global Hiboutik synchronization');
|
||||||
// Order matters: products connect to brands, categories and suppliers by hiboutikId.
|
// Order matters: products connect to brands, categories and suppliers by hiboutikId.
|
||||||
const steps: [string, () => Promise<void>][] = [
|
const steps: [string, () => Promise<void>][] = [
|
||||||
@@ -83,6 +113,10 @@ export class HiboutikService {
|
|||||||
await step();
|
await step();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
failedSteps.push(name);
|
failedSteps.push(name);
|
||||||
|
if (error instanceof HiboutikRateLimitError) {
|
||||||
|
this.logger.error(`Hiboutik synchronization stopped during the ${name} step: ${error.message}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.logger.error(`Hiboutik ${name} sync failed`, (error as Error)?.stack);
|
this.logger.error(`Hiboutik ${name} sync failed`, (error as Error)?.stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,22 +12,20 @@ const hiboutikProduct = (id: number, overrides: Partial<ProductHiboutikDto> = {}
|
|||||||
product_supplier: 0,
|
product_supplier: 0,
|
||||||
product_price: '10.00',
|
product_price: '10.00',
|
||||||
product_discount_price: '0.00',
|
product_discount_price: '0.00',
|
||||||
product_supply_price: '5.00',
|
|
||||||
product_category: 0,
|
product_category: 0,
|
||||||
product_display_www: 1,
|
product_display_www: 1,
|
||||||
product_arch: 0,
|
product_arch: 0,
|
||||||
products_desc: '',
|
products_desc: '',
|
||||||
updated_at: '2026-06-28 17:02:55',
|
updated_at: '2026-06-28 17:02:55',
|
||||||
product_specific_rules: [],
|
product_specific_rules: [],
|
||||||
|
product_size_details: [],
|
||||||
...overrides,
|
...overrides,
|
||||||
});
|
});
|
||||||
|
|
||||||
const stock = (productId: number, size: number, available: string): StockHiboutikDto => ({
|
const stock = (productId: number, size: number, available: number): StockHiboutikDto => ({
|
||||||
product_id: productId,
|
product_id: productId,
|
||||||
product_size: size,
|
product_size: size,
|
||||||
stock_available: available,
|
stock_available: available,
|
||||||
product_barcode: '',
|
|
||||||
product_supplier_reference: '',
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ProductHiboutikService', () => {
|
describe('ProductHiboutikService', () => {
|
||||||
@@ -37,6 +35,10 @@ describe('ProductHiboutikService', () => {
|
|||||||
const stockHiboutikRepository = { getStocks: jest.fn() };
|
const stockHiboutikRepository = { getStocks: jest.fn() };
|
||||||
const productDatabaseRepository = { getAll: jest.fn(), upsert: jest.fn(), deleteAll: jest.fn() };
|
const productDatabaseRepository = { getAll: jest.fn(), upsert: jest.fn(), deleteAll: jest.fn() };
|
||||||
const productVersionDatabaseRepository = { upsert: jest.fn(), deleteMissing: jest.fn() };
|
const productVersionDatabaseRepository = { upsert: jest.fn(), deleteMissing: jest.fn() };
|
||||||
|
// The transaction client handed to the repositories inside $transaction
|
||||||
|
const tx = { transaction: true };
|
||||||
|
const prisma = { $transaction: jest.fn() };
|
||||||
|
const slugService = { assignMissingSlugs: jest.fn() };
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
|
jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
|
||||||
@@ -47,6 +49,7 @@ describe('ProductHiboutikService', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
calls = [];
|
calls = [];
|
||||||
|
prisma.$transaction.mockImplementation((run: (client: unknown) => Promise<unknown>) => run(tx));
|
||||||
stockHiboutikRepository.getStocks.mockResolvedValue([]);
|
stockHiboutikRepository.getStocks.mockResolvedValue([]);
|
||||||
productDatabaseRepository.getAll.mockResolvedValue([]);
|
productDatabaseRepository.getAll.mockResolvedValue([]);
|
||||||
productDatabaseRepository.deleteAll.mockImplementation((ids: string[]) => {
|
productDatabaseRepository.deleteAll.mockImplementation((ids: string[]) => {
|
||||||
@@ -73,6 +76,8 @@ describe('ProductHiboutikService', () => {
|
|||||||
productVersionDatabaseRepository as never,
|
productVersionDatabaseRepository as never,
|
||||||
new ProductConverter(),
|
new ProductConverter(),
|
||||||
stockHiboutikRepository as never,
|
stockHiboutikRepository as never,
|
||||||
|
prisma as never,
|
||||||
|
slugService as never,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -109,16 +114,17 @@ describe('ProductHiboutikService', () => {
|
|||||||
|
|
||||||
it('creates a single version for a product without sizes', async () => {
|
it('creates a single version for a product without sizes', async () => {
|
||||||
productHiboutikRepository.getProducts.mockResolvedValue([hiboutikProduct(1)]);
|
productHiboutikRepository.getProducts.mockResolvedValue([hiboutikProduct(1)]);
|
||||||
stockHiboutikRepository.getStocks.mockResolvedValue([stock(1, 0, '4'), stock(2, 0, '99')]);
|
stockHiboutikRepository.getStocks.mockResolvedValue([stock(1, 0, 4), stock(2, 0, 99)]);
|
||||||
|
|
||||||
await service.syncProducts();
|
await service.syncProducts();
|
||||||
|
|
||||||
expect(productDatabaseRepository.upsert).toHaveBeenCalledWith(expect.objectContaining({ hiboutikId: 1, globalStock: 4 }));
|
expect(productDatabaseRepository.upsert).toHaveBeenCalledWith(expect.objectContaining({ hiboutikId: 1, globalStock: 4 }), tx);
|
||||||
expect(productVersionDatabaseRepository.deleteMissing).toHaveBeenCalledWith('uuid-1', [0]);
|
expect(productVersionDatabaseRepository.deleteMissing).toHaveBeenCalledWith('uuid-1', [0], tx);
|
||||||
expect(productVersionDatabaseRepository.upsert).toHaveBeenCalledTimes(1);
|
expect(productVersionDatabaseRepository.upsert).toHaveBeenCalledTimes(1);
|
||||||
expect(productVersionDatabaseRepository.upsert).toHaveBeenCalledWith(
|
expect(productVersionDatabaseRepository.upsert).toHaveBeenCalledWith(
|
||||||
'uuid-1',
|
'uuid-1',
|
||||||
expect.objectContaining({ hiboutikSizeId: 0, barcode: 'barcode-1', stock: 4, versionName: 'Product 1', discountPrice: null }),
|
expect.objectContaining({ hiboutikSizeId: 0, barcode: 'barcode-1', stock: 4, versionName: 'Product 1', discountPrice: null }),
|
||||||
|
tx,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -129,34 +135,44 @@ describe('ProductHiboutikService', () => {
|
|||||||
{ size_id: 10, size_name: 'S', barcode: 'b-s' },
|
{ size_id: 10, size_name: 'S', barcode: 'b-s' },
|
||||||
{ size_id: 11, size_name: 'M', barcode: '' },
|
{ size_id: 11, size_name: 'M', barcode: '' },
|
||||||
],
|
],
|
||||||
product_specific_rules: [{ size_id: 10, barcode: 'b-s', product_price: '12.00', product_discount_price: '0.00' }],
|
product_specific_rules: [{ size_id: 10, product_price: '12.00', product_discount_price: '0.00' }],
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
stockHiboutikRepository.getStocks.mockResolvedValue([stock(1, 10, '3'), stock(1, 11, '2')]);
|
stockHiboutikRepository.getStocks.mockResolvedValue([stock(1, 10, 3), stock(1, 11, 2)]);
|
||||||
|
|
||||||
await service.syncProducts();
|
await service.syncProducts();
|
||||||
|
|
||||||
expect(productDatabaseRepository.upsert).toHaveBeenCalledWith(expect.objectContaining({ globalStock: 5 }));
|
expect(productDatabaseRepository.upsert).toHaveBeenCalledWith(expect.objectContaining({ globalStock: 5 }), tx);
|
||||||
expect(productVersionDatabaseRepository.deleteMissing).toHaveBeenCalledWith('uuid-1', [10, 11]);
|
expect(productVersionDatabaseRepository.deleteMissing).toHaveBeenCalledWith('uuid-1', [10, 11], tx);
|
||||||
expect(calls).toEqual(['deleteProducts', 'upsertProduct', 'deleteVersions', 'upsertVersion', 'upsertVersion']);
|
expect(calls).toEqual(['deleteProducts', 'upsertProduct', 'deleteVersions', 'upsertVersion', 'upsertVersion']);
|
||||||
expect(productVersionDatabaseRepository.upsert).toHaveBeenNthCalledWith(1, 'uuid-1', {
|
expect(productVersionDatabaseRepository.upsert).toHaveBeenNthCalledWith(
|
||||||
barcode: 'b-s',
|
1,
|
||||||
discountPrice: null,
|
'uuid-1',
|
||||||
hiboutikSizeId: 10,
|
{
|
||||||
price: '12.00',
|
barcode: 'b-s',
|
||||||
product: { connect: { id: 'uuid-1' } },
|
discountPrice: null,
|
||||||
stock: 3,
|
hiboutikSizeId: 10,
|
||||||
versionName: 'S',
|
price: '12.00',
|
||||||
});
|
product: { connect: { id: 'uuid-1' } },
|
||||||
expect(productVersionDatabaseRepository.upsert).toHaveBeenNthCalledWith(2, 'uuid-1', {
|
stock: 3,
|
||||||
barcode: null,
|
versionName: 'S',
|
||||||
discountPrice: null,
|
},
|
||||||
hiboutikSizeId: 11,
|
tx,
|
||||||
price: null,
|
);
|
||||||
product: { connect: { id: 'uuid-1' } },
|
expect(productVersionDatabaseRepository.upsert).toHaveBeenNthCalledWith(
|
||||||
stock: 2,
|
2,
|
||||||
versionName: 'M',
|
'uuid-1',
|
||||||
});
|
{
|
||||||
|
barcode: null,
|
||||||
|
discountPrice: null,
|
||||||
|
hiboutikSizeId: 11,
|
||||||
|
price: null,
|
||||||
|
product: { connect: { id: 'uuid-1' } },
|
||||||
|
stock: 2,
|
||||||
|
versionName: 'M',
|
||||||
|
},
|
||||||
|
tx,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('keeps syncing the other products when one fails', async () => {
|
it('keeps syncing the other products when one fails', async () => {
|
||||||
@@ -168,4 +184,28 @@ describe('ProductHiboutikService', () => {
|
|||||||
expect(productDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
expect(productDatabaseRepository.upsert).toHaveBeenCalledTimes(2);
|
||||||
expect(productVersionDatabaseRepository.upsert).toHaveBeenCalledTimes(1);
|
expect(productVersionDatabaseRepository.upsert).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('writes a product and its versions in one transaction per product, and counts a failed one', async () => {
|
||||||
|
productHiboutikRepository.getProducts.mockResolvedValue([hiboutikProduct(1), hiboutikProduct(2)]);
|
||||||
|
productVersionDatabaseRepository.upsert.mockRejectedValueOnce(new Error('boom'));
|
||||||
|
|
||||||
|
await expect(service.syncProducts()).resolves.toBeUndefined();
|
||||||
|
|
||||||
|
expect(prisma.$transaction).toHaveBeenCalledTimes(2);
|
||||||
|
// the failure inside the transaction is what makes Prisma roll the product back
|
||||||
|
await expect(prisma.$transaction.mock.results[0].value).rejects.toThrow('boom');
|
||||||
|
await expect(prisma.$transaction.mock.results[1].value).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
it('generates the slugs of new products once they are saved', async () => {
|
||||||
|
productHiboutikRepository.getProducts.mockResolvedValue([hiboutikProduct(1)]);
|
||||||
|
slugService.assignMissingSlugs.mockImplementation(() => {
|
||||||
|
calls.push('slugs');
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
await service.syncProducts();
|
||||||
|
|
||||||
|
expect(slugService.assignMissingSlugs).toHaveBeenCalledWith('PRODUCT');
|
||||||
|
expect(calls[calls.length - 1]).toBe('slugs');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ import { ProductConverter } from '../../models/converter/product.converter';
|
|||||||
import { StockHiboutikRepository } from '../../hiboutik/stock.hiboutik.repository';
|
import { StockHiboutikRepository } from '../../hiboutik/stock.hiboutik.repository';
|
||||||
import { ProductHiboutikDto } from '../../models/hiboutikDTO/product.hiboutik.dto';
|
import { ProductHiboutikDto } from '../../models/hiboutikDTO/product.hiboutik.dto';
|
||||||
import { StockHiboutikDto } from '../../models/hiboutikDTO/stock.hiboutik.dto';
|
import { StockHiboutikDto } from '../../models/hiboutikDTO/stock.hiboutik.dto';
|
||||||
import { Product } from '../../../prisma/generated/client';
|
import { Product, SlugEntityType } from '../../../prisma/generated/client';
|
||||||
import { ProductVersionCreateInput } from '../../../prisma/generated/models/ProductVersion';
|
import { ProductVersionCreateInput } from '../../../prisma/generated/models/ProductVersion';
|
||||||
import { parseOptionalHiboutikPrice } from '../../utils/price';
|
import { parseOptionalHiboutikPrice } from '../../utils/price';
|
||||||
|
import { PrismaService } from '../../../prisma/prisma.service';
|
||||||
|
import { rethrowIfRateLimited } from '../../hiboutik/hiboutik.client';
|
||||||
|
import { SlugService } from '../slug/slug.service';
|
||||||
|
|
||||||
/** Version id used for products that have no sizes in Hiboutik. */
|
/** Version id used for products that have no sizes in Hiboutik. */
|
||||||
const NO_SIZE_ID = 0;
|
const NO_SIZE_ID = 0;
|
||||||
@@ -23,9 +26,11 @@ export class ProductHiboutikService {
|
|||||||
private readonly productVersionDatabaseRepository: ProductVersionDatabaseRepository,
|
private readonly productVersionDatabaseRepository: ProductVersionDatabaseRepository,
|
||||||
private readonly productConverter: ProductConverter,
|
private readonly productConverter: ProductConverter,
|
||||||
private readonly stockHiboutikRepository: StockHiboutikRepository,
|
private readonly stockHiboutikRepository: StockHiboutikRepository,
|
||||||
|
private readonly prisma: PrismaService,
|
||||||
|
private readonly slugService: SlugService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/** Mirrors Hiboutik products, their versions (sizes) and stocks. Products missing from Hiboutik are deleted. */
|
/** Mirrors Hiboutik products, their versions (sizes) and stocks. Products missing from Hiboutik are deleted, new ones get a slug. */
|
||||||
async syncProducts() {
|
async syncProducts() {
|
||||||
const [productsHiboutik, hiboutikStocks, existingProducts] = await Promise.all([
|
const [productsHiboutik, hiboutikStocks, existingProducts] = await Promise.all([
|
||||||
this.productHiboutikRepository.getProducts(),
|
this.productHiboutikRepository.getProducts(),
|
||||||
@@ -33,21 +38,30 @@ export class ProductHiboutikService {
|
|||||||
this.productDatabaseRepository.getAll(),
|
this.productDatabaseRepository.getAll(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Deletion happens before upserts so that a product recreated in Hiboutik (new id, same barcode)
|
// Deletion happens before the upserts, so that order lines of a deleted product are unlinked before anything else changes
|
||||||
// does not collide with the old one on the unique barcode.
|
|
||||||
const nbDeleted = await this.deleteMissingProducts(productsHiboutik, existingProducts);
|
const nbDeleted = await this.deleteMissingProducts(productsHiboutik, existingProducts);
|
||||||
|
|
||||||
|
// Grouped once by product: one pass over the stocks instead of one per product
|
||||||
|
const stocksByProduct = new Map<number, StockHiboutikDto[]>();
|
||||||
|
for (const stock of hiboutikStocks) {
|
||||||
|
const productStocks = stocksByProduct.get(stock.product_id);
|
||||||
|
if (productStocks) productStocks.push(stock);
|
||||||
|
else stocksByProduct.set(stock.product_id, [stock]);
|
||||||
|
}
|
||||||
|
|
||||||
let nbFailed = 0;
|
let nbFailed = 0;
|
||||||
for (const productHiboutik of productsHiboutik) {
|
for (const productHiboutik of productsHiboutik) {
|
||||||
const hiboutikProductStocks = hiboutikStocks.filter((stock) => stock.product_id === Number(productHiboutik.product_id));
|
const hiboutikProductStocks = stocksByProduct.get(productHiboutik.product_id) ?? [];
|
||||||
try {
|
try {
|
||||||
await this.updateProduct(productHiboutik, hiboutikProductStocks);
|
await this.updateProduct(productHiboutik, hiboutikProductStocks);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
rethrowIfRateLimited(error);
|
||||||
nbFailed++;
|
nbFailed++;
|
||||||
this.logger.error(`Failed to sync Hiboutik product ${productHiboutik.product_id}`, (error as Error)?.stack);
|
this.logger.error(`Failed to sync Hiboutik product ${productHiboutik.product_id}`, (error as Error)?.stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.slugService.assignMissingSlugs(SlugEntityType.PRODUCT);
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Products sync ended: ${productsHiboutik.length - nbFailed}/${productsHiboutik.length} synced, ${nbFailed} failed, ${nbDeleted} deleted.`,
|
`Products sync ended: ${productsHiboutik.length - nbFailed}/${productsHiboutik.length} synced, ${nbFailed} failed, ${nbDeleted} deleted.`,
|
||||||
);
|
);
|
||||||
@@ -56,27 +70,35 @@ export class ProductHiboutikService {
|
|||||||
/** @returns the number of deleted products. */
|
/** @returns the number of deleted products. */
|
||||||
private async deleteMissingProducts(productsHiboutik: ProductHiboutikDto[], existingProducts: Product[]): Promise<number> {
|
private async deleteMissingProducts(productsHiboutik: ProductHiboutikDto[], existingProducts: Product[]): Promise<number> {
|
||||||
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
||||||
const hiboutikIds = new Set(productsHiboutik.map((product) => Number(product.product_id)));
|
const hiboutikIds = new Set(productsHiboutik.map((product) => product.product_id));
|
||||||
const missingProductIds = existingProducts.filter((product) => !hiboutikIds.has(product.hiboutikId)).map((product) => product.id);
|
const missingProductIds = existingProducts.filter((product) => !hiboutikIds.has(product.hiboutikId)).map((product) => product.id);
|
||||||
return this.productDatabaseRepository.deleteAll(missingProductIds);
|
return this.productDatabaseRepository.deleteAll(missingProductIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Upserts a product with its global stock, then replaces its versions. */
|
/**
|
||||||
|
* Upserts a product with its global stock, then replaces its versions, in a single transaction: on failure, nothing
|
||||||
|
* is changed (no global stock out of step with the versions).
|
||||||
|
*/
|
||||||
private async updateProduct(productHiboutik: ProductHiboutikDto, productStocks: StockHiboutikDto[]): Promise<void> {
|
private async updateProduct(productHiboutik: ProductHiboutikDto, productStocks: StockHiboutikDto[]): Promise<void> {
|
||||||
const newProduct = this.productConverter.fromHiboutik(productHiboutik);
|
const newProduct = this.productConverter.fromHiboutik(productHiboutik);
|
||||||
newProduct.globalStock = productStocks.map((stock) => Number(stock.stock_available)).reduce((sum, stock) => sum + stock, 0);
|
newProduct.globalStock = productStocks.reduce((sum, stock) => sum + stock.stock_available, 0);
|
||||||
|
|
||||||
const savedProduct: Product = await this.productDatabaseRepository.upsert(newProduct);
|
await this.prisma.$transaction(
|
||||||
const versions = this.buildVersions(productHiboutik, productStocks, savedProduct);
|
async (tx) => {
|
||||||
|
const savedProduct: Product = await this.productDatabaseRepository.upsert(newProduct, tx);
|
||||||
|
const versions = this.buildVersions(productHiboutik, productStocks, savedProduct);
|
||||||
|
|
||||||
// Same reason as for products: delete removed versions first to free their barcodes
|
await this.productVersionDatabaseRepository.deleteMissing(
|
||||||
await this.productVersionDatabaseRepository.deleteMissing(
|
savedProduct.id,
|
||||||
savedProduct.id,
|
versions.map((version) => version.hiboutikSizeId),
|
||||||
versions.map((version) => version.hiboutikSizeId),
|
tx,
|
||||||
|
);
|
||||||
|
for (const version of versions) {
|
||||||
|
await this.productVersionDatabaseRepository.upsert(savedProduct.id, version, tx);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ timeout: 15_000 },
|
||||||
);
|
);
|
||||||
for (const version of versions) {
|
|
||||||
await this.productVersionDatabaseRepository.upsert(savedProduct.id, version);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,8 +110,8 @@ export class ProductHiboutikService {
|
|||||||
productStocks: StockHiboutikDto[],
|
productStocks: StockHiboutikDto[],
|
||||||
savedProduct: Product,
|
savedProduct: Product,
|
||||||
): ProductVersionCreateInput[] {
|
): ProductVersionCreateInput[] {
|
||||||
const specificRules = productHiboutik.product_specific_rules ?? [];
|
const specificRules = productHiboutik.product_specific_rules;
|
||||||
const sizesDetails = productHiboutik.product_size_details ?? [];
|
const sizesDetails = productHiboutik.product_size_details;
|
||||||
|
|
||||||
if (sizesDetails.length === 0) {
|
if (sizesDetails.length === 0) {
|
||||||
return [
|
return [
|
||||||
@@ -106,8 +128,8 @@ export class ProductHiboutikService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return sizesDetails.map((sizeDetails) => {
|
return sizesDetails.map((sizeDetails) => {
|
||||||
const sizeId = Number(sizeDetails.size_id);
|
const sizeId = sizeDetails.size_id;
|
||||||
const specificRule = specificRules.find((rule) => Number(rule.size_id) === sizeId);
|
const specificRule = specificRules.find((rule) => rule.size_id === sizeId);
|
||||||
const stock = productStocks.find((stock) => stock.product_size === sizeId);
|
const stock = productStocks.find((stock) => stock.product_size === sizeId);
|
||||||
// Values are always set explicitly (null / 0 rather than undefined), otherwise Prisma would keep
|
// Values are always set explicitly (null / 0 rather than undefined), otherwise Prisma would keep
|
||||||
// the previous value on update when a rule or a stock is removed in Hiboutik.
|
// the previous value on update when a rule or a stock is removed in Hiboutik.
|
||||||
@@ -117,7 +139,7 @@ export class ProductHiboutikService {
|
|||||||
hiboutikSizeId: sizeId,
|
hiboutikSizeId: sizeId,
|
||||||
price: parseOptionalHiboutikPrice(specificRule?.product_price),
|
price: parseOptionalHiboutikPrice(specificRule?.product_price),
|
||||||
product: { connect: { id: savedProduct.id } },
|
product: { connect: { id: savedProduct.id } },
|
||||||
stock: stock ? Number(stock.stock_available) : 0,
|
stock: stock?.stock_available ?? 0,
|
||||||
versionName: sizeDetails.size_name,
|
versionName: sizeDetails.size_name,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export class SupplierHiboutikService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
// Hiboutik is trusted: a successful (2xx) answer, even an empty list, is the truth. An HTTP error throws before this point.
|
||||||
const nbDeleted = await this.supplierDatabaseRepository.deleteMissing(hiboutikSuppliers.map((dto) => Number(dto.supplier_id)));
|
const nbDeleted = await this.supplierDatabaseRepository.deleteMissing(hiboutikSuppliers.map((dto) => dto.supplier_id));
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Suppliers sync ended: ${hiboutikSuppliers.length - nbFailed}/${hiboutikSuppliers.length} synced, ${nbFailed} failed, ${nbDeleted} deleted.`,
|
`Suppliers sync ended: ${hiboutikSuppliers.length - nbFailed}/${hiboutikSuppliers.length} synced, ${nbFailed} failed, ${nbDeleted} deleted.`,
|
||||||
);
|
);
|
||||||
|
|||||||
156
src/services/slug/slug.service.spec.ts
Normal file
156
src/services/slug/slug.service.spec.ts
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
import { BadRequestException, ConflictException, Logger, NotFoundException } from '@nestjs/common';
|
||||||
|
import { SlugService } from './slug.service';
|
||||||
|
import { Prisma, SlugEntityType } from '../../../prisma/generated/client';
|
||||||
|
import { SlugCandidateRow } from '../../database/slug.database.repository';
|
||||||
|
|
||||||
|
const row = (hiboutikId: number, name: string, parentName: string | null = null): SlugCandidateRow => ({
|
||||||
|
id: `uuid-${hiboutikId}`,
|
||||||
|
hiboutikId,
|
||||||
|
name,
|
||||||
|
parentName,
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SlugService', () => {
|
||||||
|
const slugDatabaseRepository = {
|
||||||
|
getWithoutSlug: jest.fn(),
|
||||||
|
getTakenSlugs: jest.fn(),
|
||||||
|
setFirstSlug: jest.fn(),
|
||||||
|
changeSlug: jest.fn(),
|
||||||
|
findRedirect: jest.fn(),
|
||||||
|
};
|
||||||
|
let service: SlugService;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);
|
||||||
|
jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);
|
||||||
|
jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
slugDatabaseRepository.getTakenSlugs.mockResolvedValue(new Set());
|
||||||
|
slugDatabaseRepository.setFirstSlug.mockResolvedValue(true);
|
||||||
|
service = new SlugService(slugDatabaseRepository as never);
|
||||||
|
});
|
||||||
|
|
||||||
|
/** Slugs given by assignMissingSlugs, by entity id. */
|
||||||
|
const assigned = (): Record<string, string> =>
|
||||||
|
Object.fromEntries((slugDatabaseRepository.setFirstSlug.mock.calls as [string, string, string][]).map(([, id, slug]) => [id, slug]));
|
||||||
|
|
||||||
|
describe('assignMissingSlugs', () => {
|
||||||
|
it('gives the plain slug to the oldest of two homonyms, the Hiboutik id to the other', async () => {
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockResolvedValue([row(3, 'Stray Kids'), row(8, 'STRAY KIDS')]);
|
||||||
|
|
||||||
|
await service.assignMissingSlugs(SlugEntityType.BRAND);
|
||||||
|
|
||||||
|
expect(assigned()).toEqual({ 'uuid-3': 'stray-kids', 'uuid-8': 'stray-kids-8' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not take a slug already used, by an entity or a redirect', async () => {
|
||||||
|
slugDatabaseRepository.getTakenSlugs.mockResolvedValue(new Set(['bts']));
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockResolvedValue([row(5, 'BTS')]);
|
||||||
|
|
||||||
|
await service.assignMissingSlugs(SlugEntityType.BRAND);
|
||||||
|
|
||||||
|
expect(assigned()).toEqual({ 'uuid-5': 'bts-5' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('suffixes a homonym category or product with its Hiboutik id', async () => {
|
||||||
|
slugDatabaseRepository.getTakenSlugs.mockResolvedValue(new Set(['albums']));
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockResolvedValue([row(4, 'Albums'), row(6, 'ATE (Digipack)')]);
|
||||||
|
|
||||||
|
await service.assignMissingSlugs(SlugEntityType.CATEGORY);
|
||||||
|
|
||||||
|
expect(assigned()).toEqual({ 'uuid-4': 'albums-4', 'uuid-6': 'ate-digipack' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('prefixes a homonym sub-category with its parent before falling back to the Hiboutik id', async () => {
|
||||||
|
slugDatabaseRepository.getTakenSlugs.mockResolvedValue(new Set(['albums']));
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockResolvedValue([
|
||||||
|
row(4, 'Albums', 'Stray Kids'),
|
||||||
|
row(5, 'Albums', 'BTS'),
|
||||||
|
row(6, 'Albums', 'Stray Kids'),
|
||||||
|
row(7, 'Albums'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await service.assignMissingSlugs(SlugEntityType.CATEGORY);
|
||||||
|
|
||||||
|
expect(assigned()).toEqual({
|
||||||
|
'uuid-4': 'stray-kids-albums',
|
||||||
|
'uuid-5': 'bts-albums',
|
||||||
|
'uuid-6': 'albums-6',
|
||||||
|
'uuid-7': 'albums-7',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skips the parent prefix when it does not help', async () => {
|
||||||
|
slugDatabaseRepository.getTakenSlugs.mockResolvedValue(new Set(['bts-albums', 'goods', 'photocards']));
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockResolvedValue([
|
||||||
|
row(1, 'BTS Albums', 'BTS'), // already starts with the parent
|
||||||
|
row(2, 'Goods', '방탄소년단'), // parent without latin letters
|
||||||
|
row(3, 'Photocards', 'x'.repeat(75)), // too long to keep the whole name
|
||||||
|
]);
|
||||||
|
|
||||||
|
await service.assignMissingSlugs(SlugEntityType.CATEGORY);
|
||||||
|
|
||||||
|
expect(assigned()).toEqual({ 'uuid-1': 'bts-albums-1', 'uuid-2': 'goods-2', 'uuid-3': 'photocards-3' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to the type and Hiboutik id for a name without latin letters', async () => {
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockResolvedValue([row(9, '방탄소년단')]);
|
||||||
|
|
||||||
|
await service.assignMissingSlugs(SlugEntityType.BRAND);
|
||||||
|
|
||||||
|
expect(assigned()).toEqual({ 'uuid-9': 'brand-9' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps going when one slug fails, and never throws', async () => {
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockResolvedValue([row(1, 'A'), row(2, 'B')]);
|
||||||
|
slugDatabaseRepository.setFirstSlug.mockRejectedValueOnce(new Error('boom'));
|
||||||
|
|
||||||
|
await expect(service.assignMissingSlugs(SlugEntityType.BRAND)).resolves.toBeUndefined();
|
||||||
|
expect(slugDatabaseRepository.setFirstSlug).toHaveBeenCalledTimes(2);
|
||||||
|
|
||||||
|
slugDatabaseRepository.getWithoutSlug.mockRejectedValue(new Error('database down'));
|
||||||
|
await expect(service.assignMissingSlugs(SlugEntityType.BRAND)).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('changeSlug', () => {
|
||||||
|
it('rejects an invalid slug without touching the database', async () => {
|
||||||
|
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'Stray Kids')).rejects.toThrow(BadRequestException);
|
||||||
|
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'a'.repeat(81))).rejects.toThrow(BadRequestException);
|
||||||
|
expect(slugDatabaseRepository.changeSlug).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('answers 404 for an unknown entity and 409 for a slug in use, even when written concurrently', async () => {
|
||||||
|
slugDatabaseRepository.changeSlug.mockResolvedValueOnce('NOT_FOUND');
|
||||||
|
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toThrow(NotFoundException);
|
||||||
|
|
||||||
|
slugDatabaseRepository.changeSlug.mockResolvedValueOnce('TAKEN');
|
||||||
|
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toThrow(ConflictException);
|
||||||
|
|
||||||
|
slugDatabaseRepository.changeSlug.mockRejectedValueOnce(
|
||||||
|
new Prisma.PrismaClientKnownRequestError('Unique constraint failed', { code: 'P2002', clientVersion: 'test' }),
|
||||||
|
);
|
||||||
|
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toThrow(ConflictException);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rethrows any other error, even one carrying a P2002 code without being a Prisma error', async () => {
|
||||||
|
const notPrisma = Object.assign(new Error('boom'), { code: 'P2002' });
|
||||||
|
slugDatabaseRepository.changeSlug.mockRejectedValueOnce(notPrisma);
|
||||||
|
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toBe(notPrisma);
|
||||||
|
|
||||||
|
const otherPrismaError = new Prisma.PrismaClientKnownRequestError('Record not found', { code: 'P2025', clientVersion: 'test' });
|
||||||
|
slugDatabaseRepository.changeSlug.mockRejectedValueOnce(otherPrismaError);
|
||||||
|
await expect(service.changeSlug(SlugEntityType.BRAND, 'uuid-1', 'bts')).rejects.toBe(otherPrismaError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts a valid new slug', async () => {
|
||||||
|
slugDatabaseRepository.changeSlug.mockResolvedValue('CHANGED');
|
||||||
|
|
||||||
|
await expect(service.changeSlug(SlugEntityType.PRODUCT, 'uuid-1', 'stray-kids-ate')).resolves.toBeUndefined();
|
||||||
|
expect(slugDatabaseRepository.changeSlug).toHaveBeenCalledWith('PRODUCT', 'uuid-1', 'stray-kids-ate');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
113
src/services/slug/slug.service.ts
Normal file
113
src/services/slug/slug.service.ts
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
import { BadRequestException, ConflictException, Injectable, Logger, NotFoundException } from '@nestjs/common';
|
||||||
|
import { Prisma, SlugEntityType } from '../../../prisma/generated/client';
|
||||||
|
import { SlugCandidateRow, SlugDatabaseRepository } from '../../database/slug.database.repository';
|
||||||
|
import { forEachSafely } from '../../utils/sync';
|
||||||
|
import { MAX_SLUG_LENGTH, pickAvailableSlug, SLUG_PATTERN, slugify, withSlugSuffix } from '../../utils/slug';
|
||||||
|
|
||||||
|
/** Prefix of the slug of an entity whose name gives no slug (no latin letter or digit, e.g. a name in hangul). */
|
||||||
|
const FALLBACK_PREFIX: Record<SlugEntityType, string> = {
|
||||||
|
[SlugEntityType.BRAND]: 'brand',
|
||||||
|
[SlugEntityType.CATEGORY]: 'category',
|
||||||
|
[SlugEntityType.PRODUCT]: 'product',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Slugs (SEO URL identifiers) of brands, categories and products.
|
||||||
|
*
|
||||||
|
* A slug is generated once, when an entity has none, and is never regenerated: a URL must not change when the name
|
||||||
|
* changes in Hiboutik. Generation is deterministic: entities are handled oldest first (by `hiboutikId`), so that a
|
||||||
|
* database rebuilt from scratch gets the same slugs, the oldest of two homonyms keeping the plain slug.
|
||||||
|
* A rebuild only gives back the stored slugs as long as their history did not change them: a slug replaced by an admin,
|
||||||
|
* an entity renamed in Hiboutik after its slug was generated, or a homonym deleted since (the younger one, suffixed,
|
||||||
|
* would now get the plain slug) all give a different slug on a rebuild, and the redirects are lost with the database.
|
||||||
|
* An admin can replace a slug ({@link changeSlug}): the former one is kept as a redirect.
|
||||||
|
*/
|
||||||
|
@Injectable()
|
||||||
|
export class SlugService {
|
||||||
|
private readonly logger = new Logger('SlugService');
|
||||||
|
|
||||||
|
constructor(private readonly slugDatabaseRepository: SlugDatabaseRepository) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gives a slug to every entity of this type that has none. Never throws: a failure is logged, the entities left
|
||||||
|
* without a slug get one on the next sync.
|
||||||
|
*/
|
||||||
|
async assignMissingSlugs(type: SlugEntityType): Promise<void> {
|
||||||
|
try {
|
||||||
|
const [rows, taken] = await Promise.all([
|
||||||
|
this.slugDatabaseRepository.getWithoutSlug(type),
|
||||||
|
this.slugDatabaseRepository.getTakenSlugs(type),
|
||||||
|
]);
|
||||||
|
const nbFailed = await forEachSafely(
|
||||||
|
rows,
|
||||||
|
async (row) => {
|
||||||
|
const candidates = this.buildCandidates(type, row);
|
||||||
|
const slug = pickAvailableSlug(candidates, taken);
|
||||||
|
if (slug !== candidates[0]) {
|
||||||
|
// Often a duplicate in Hiboutik ("Stray Kids" and "STRAY KIDS"): worth a look in the back office
|
||||||
|
this.logger.warn(`Slug "${candidates[0]}" already taken: Hiboutik ${type.toLowerCase()} ${row.hiboutikId} gets "${slug}"`);
|
||||||
|
}
|
||||||
|
if (await this.slugDatabaseRepository.setFirstSlug(type, row.id, slug)) taken.add(slug);
|
||||||
|
},
|
||||||
|
this.logger,
|
||||||
|
(row) => `slug of Hiboutik ${type.toLowerCase()} ${row.hiboutikId}`,
|
||||||
|
);
|
||||||
|
if (rows.length > 0) {
|
||||||
|
this.logger.log(`Slugs of ${type.toLowerCase()} entities: ${rows.length - nbFailed}/${rows.length} generated, ${nbFailed} failed.`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`Failed to generate the slugs of ${type.toLowerCase()} entities`, (error as Error)?.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Candidate slugs by order of preference: the name ("albums"); for a sub-category, the name prefixed with its parent's
|
||||||
|
* ("stray-kids-albums"), homonyms being common across the tree; then the name suffixed with the Hiboutik id
|
||||||
|
* ("albums-12"), unique as the id is.
|
||||||
|
*/
|
||||||
|
private buildCandidates(type: SlugEntityType, row: SlugCandidateRow): string[] {
|
||||||
|
const base = slugify(row.name);
|
||||||
|
if (!base) return [`${FALLBACK_PREFIX[type]}-${row.hiboutikId}`];
|
||||||
|
const withParent = this.withParentPrefix(base, row.parentName);
|
||||||
|
return [base, ...(withParent ? [withParent] : []), withSlugSuffix(base, row.hiboutikId)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `<parent>-<name>`, or null when it would not help: no parent, a parent without latin letters, a name that already
|
||||||
|
* starts with the parent's ("BTS Albums" under "BTS"), or a result too long to keep the whole name.
|
||||||
|
*/
|
||||||
|
private withParentPrefix(base: string, parentName: string | null): string | null {
|
||||||
|
const parent = parentName ? slugify(parentName) : '';
|
||||||
|
if (!parent || base === parent || base.startsWith(`${parent}-`)) return null;
|
||||||
|
const slug = `${parent}-${base}`;
|
||||||
|
return slug.length <= MAX_SLUG_LENGTH ? slug : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the slug of an entity (admin choice); its former slug keeps answering through a redirect.
|
||||||
|
* @throws BadRequestException for an invalid slug, NotFoundException for an unknown entity,
|
||||||
|
* ConflictException when another entity of the same type uses this slug.
|
||||||
|
*/
|
||||||
|
async changeSlug(type: SlugEntityType, id: string, slug: string): Promise<void> {
|
||||||
|
if (slug.length > MAX_SLUG_LENGTH || !SLUG_PATTERN.test(slug)) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`A slug is made of lowercase letters, digits and single hyphens, ${MAX_SLUG_LENGTH} characters at most`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let result: Awaited<ReturnType<SlugDatabaseRepository['changeSlug']>>;
|
||||||
|
try {
|
||||||
|
result = await this.slugDatabaseRepository.changeSlug(type, id, slug);
|
||||||
|
} catch (error) {
|
||||||
|
// Unique constraint: the same slug was written at the same time by someone else
|
||||||
|
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') result = 'TAKEN';
|
||||||
|
else throw error;
|
||||||
|
}
|
||||||
|
if (result === 'NOT_FOUND') throw new NotFoundException();
|
||||||
|
if (result === 'TAKEN') throw new ConflictException('This slug is already used');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @returns the current slug of the entity that `slug` used to name, null when it is not a former slug. */
|
||||||
|
async findRedirect(type: SlugEntityType, slug: string): Promise<string | null> {
|
||||||
|
return this.slugDatabaseRepository.findRedirect(type, slug);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/sitemap.controller.ts
Normal file
17
src/sitemap.controller.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { Controller, Get, Header } from '@nestjs/common';
|
||||||
|
import { Sitemap, SitemapDatabaseRepository } from './database/sitemap.database.repository';
|
||||||
|
|
||||||
|
@Controller()
|
||||||
|
export class SitemapController {
|
||||||
|
constructor(private readonly sitemapDatabaseRepository: SitemapDatabaseRepository) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public pages (slug and date of the last change of their content) of brands, categories and products, for the
|
||||||
|
* storefront to build its sitemap.xml. Pages without a slug yet are left out.
|
||||||
|
*/
|
||||||
|
@Get('sitemap')
|
||||||
|
@Header('Cache-Control', 'public, max-age=600')
|
||||||
|
async getSitemap(): Promise<Sitemap> {
|
||||||
|
return this.sitemapDatabaseRepository.getSitemap();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,35 @@
|
|||||||
import { parseHiboutikDate } from './date';
|
import { parseHiboutikDate } from './date';
|
||||||
|
|
||||||
describe('parseHiboutikDate', () => {
|
describe('parseHiboutikDate', () => {
|
||||||
it('parses a Hiboutik datetime', () => {
|
it('parses a Hiboutik datetime as French summer time (UTC+2)', () => {
|
||||||
expect(parseHiboutikDate('2026-06-28 17:02:55')).toEqual(new Date('2026-06-28T17:02:55'));
|
expect(parseHiboutikDate('2026-06-28 17:02:55')).toEqual(new Date('2026-06-28T15:02:55Z'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses a date without time', () => {
|
it('parses a Hiboutik datetime as French winter time (UTC+1)', () => {
|
||||||
expect(parseHiboutikDate('1990-05-12')).toEqual(new Date('1990-05-12'));
|
expect(parseHiboutikDate('2026-01-15 10:00:00')).toEqual(new Date('2026-01-15T09:00:00Z'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it.each(['', '0000-00-00', '0000-00-00 00:00:00', 'not a date', null, undefined])('returns undefined for %p', (value) => {
|
it('switches offset at the DST changes', () => {
|
||||||
|
expect(parseHiboutikDate('2026-03-29 01:59:59')).toEqual(new Date('2026-03-29T00:59:59Z'));
|
||||||
|
expect(parseHiboutikDate('2026-03-29 03:00:00')).toEqual(new Date('2026-03-29T01:00:00Z'));
|
||||||
|
expect(parseHiboutikDate('2026-10-25 03:00:00')).toEqual(new Date('2026-10-25T02:00:00Z'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses a date without time as midnight UTC (calendar date)', () => {
|
||||||
|
expect(parseHiboutikDate('1990-05-12')).toEqual(new Date('1990-05-12T00:00:00Z'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
'',
|
||||||
|
'0000-00-00',
|
||||||
|
'0000-00-00 00:00:00',
|
||||||
|
'2026-02-30',
|
||||||
|
'2026-13-01 10:00:00',
|
||||||
|
'2026-06-28 25:00:00',
|
||||||
|
'not a date',
|
||||||
|
null,
|
||||||
|
undefined,
|
||||||
|
])('returns undefined for %p', (value) => {
|
||||||
expect(parseHiboutikDate(value)).toBeUndefined();
|
expect(parseHiboutikDate(value)).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,53 @@
|
|||||||
|
/** Time zone of the dates sent by Hiboutik: the shop's local (French) time, without any offset. */
|
||||||
|
export const HIBOUTIK_TIME_ZONE = 'Europe/Paris';
|
||||||
|
|
||||||
|
const HIBOUTIK_DATE = /^(\d{4})-(\d{2})-(\d{2})(?:[ T](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
|
||||||
|
|
||||||
|
const timeZoneFormatter = new Intl.DateTimeFormat('en-US', {
|
||||||
|
timeZone: HIBOUTIK_TIME_ZONE,
|
||||||
|
hourCycle: 'h23',
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit',
|
||||||
|
second: '2-digit',
|
||||||
|
});
|
||||||
|
|
||||||
|
/** Offset (ms) of {@link HIBOUTIK_TIME_ZONE} from UTC at the given instant: +1 h in winter, +2 h in summer. */
|
||||||
|
function timeZoneOffset(instant: number): number {
|
||||||
|
const parts = Object.fromEntries(timeZoneFormatter.formatToParts(new Date(instant)).map((part) => [part.type, Number(part.value)]));
|
||||||
|
const wallClockAsUtc = Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute, parts.second);
|
||||||
|
return wallClockAsUtc - instant;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a Hiboutik date ("YYYY-MM-DD HH:mm:ss" or "YYYY-MM-DD").
|
* Parses a Hiboutik date. Returns undefined for empty or invalid dates (e.g. "0000-00-00", "2026-02-30").
|
||||||
* Returns undefined for empty or invalid dates (e.g. "0000-00-00").
|
* - "YYYY-MM-DD HH:mm:ss": French local time ({@link HIBOUTIK_TIME_ZONE}), whatever the time zone of the server.
|
||||||
|
* A time that is ambiguous or skipped at a DST change (02:00-03:00) resolves to a nearby valid instant.
|
||||||
|
* - "YYYY-MM-DD" (calendar date, e.g. a date of birth): midnight UTC, so that the stored date is the same day.
|
||||||
*/
|
*/
|
||||||
export function parseHiboutikDate(value: string | null | undefined): Date | undefined {
|
export function parseHiboutikDate(value: string | null | undefined): Date | undefined {
|
||||||
if (!value) return undefined;
|
const match = HIBOUTIK_DATE.exec(value?.trim() ?? '');
|
||||||
|
if (!match) return undefined;
|
||||||
|
|
||||||
const date = new Date(value.trim().replace(' ', 'T'));
|
const [year, month, day, hour, minute, second] = match.slice(1).map((part) => Number(part ?? 0));
|
||||||
return isNaN(date.getTime()) ? undefined : date;
|
const wallClockAsUtc = Date.UTC(year, month - 1, day, hour, minute, second);
|
||||||
|
|
||||||
|
// Rejects out-of-range values (month 0 or 13, February 30, 25:00...), which Date.UTC would silently roll over
|
||||||
|
const check = new Date(wallClockAsUtc);
|
||||||
|
const isValid =
|
||||||
|
check.getUTCFullYear() === year &&
|
||||||
|
check.getUTCMonth() === month - 1 &&
|
||||||
|
check.getUTCDate() === day &&
|
||||||
|
check.getUTCHours() === hour &&
|
||||||
|
check.getUTCMinutes() === minute &&
|
||||||
|
check.getUTCSeconds() === second;
|
||||||
|
if (!isValid) return undefined;
|
||||||
|
|
||||||
|
if (match[4] === undefined) return check;
|
||||||
|
|
||||||
|
// The offset depends on the instant itself (DST): guess it from the wall clock, then correct it once
|
||||||
|
const firstGuess = wallClockAsUtc - timeZoneOffset(wallClockAsUtc);
|
||||||
|
return new Date(wallClockAsUtc - timeZoneOffset(firstGuess));
|
||||||
}
|
}
|
||||||
|
|||||||
48
src/utils/slug.spec.ts
Normal file
48
src/utils/slug.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import { MAX_SLUG_LENGTH, pickAvailableSlug, SLUG_PATTERN, slugify, withSlugSuffix } from './slug';
|
||||||
|
|
||||||
|
describe('slugify', () => {
|
||||||
|
it.each([
|
||||||
|
['Stray Kids', 'stray-kids'],
|
||||||
|
['STRAY KIDS', 'stray-kids'],
|
||||||
|
['Stray-Kids', 'stray-kids'],
|
||||||
|
['(G)I-DLE', 'g-i-dle'],
|
||||||
|
['NCT 127', 'nct-127'],
|
||||||
|
['&TEAM', 'and-team'],
|
||||||
|
['Œuvres complètes été', 'oeuvres-completes-ete'],
|
||||||
|
[' BTS - Proof ★ ', 'bts-proof'],
|
||||||
|
['NCT DREAM', 'nct-dream'],
|
||||||
|
['방탄소년단', ''],
|
||||||
|
])('%s -> "%s"', (name, slug) => {
|
||||||
|
expect(slugify(name)).toBe(slug);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('cuts a long name at a word boundary', () => {
|
||||||
|
const slug = slugify('word '.repeat(40));
|
||||||
|
expect(slug.length).toBeLessThanOrEqual(MAX_SLUG_LENGTH);
|
||||||
|
expect(slug).toMatch(SLUG_PATTERN);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the last word when the cut falls right before a hyphen', () => {
|
||||||
|
expect(slugify('aa bbb ccc', 6)).toBe('aa-bbb');
|
||||||
|
expect(slugify('aa bbb ccc', 7)).toBe('aa-bbb');
|
||||||
|
expect(slugify('aa bbb ccc', 5)).toBe('aa');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('withSlugSuffix', () => {
|
||||||
|
it('keeps the result within the maximum length', () => {
|
||||||
|
const slug = withSlugSuffix('a'.repeat(MAX_SLUG_LENGTH), 12345);
|
||||||
|
expect(slug.length).toBeLessThanOrEqual(MAX_SLUG_LENGTH);
|
||||||
|
expect(slug.endsWith('-12345')).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('pickAvailableSlug', () => {
|
||||||
|
it('picks the first free candidate', () => {
|
||||||
|
expect(pickAvailableSlug(['albums', 'bts-albums', 'albums-12'], new Set(['albums']))).toBe('bts-albums');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('numbers the last candidate when they are all taken', () => {
|
||||||
|
expect(pickAvailableSlug(['albums', 'albums-12'], new Set(['albums', 'albums-12', 'albums-12-2']))).toBe('albums-12-3');
|
||||||
|
});
|
||||||
|
});
|
||||||
59
src/utils/slug.ts
Normal file
59
src/utils/slug.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
/** Longest slug generated, so that URLs stay readable. */
|
||||||
|
export const MAX_SLUG_LENGTH = 80;
|
||||||
|
|
||||||
|
/** Valid slug: lowercase ASCII words separated by single hyphens. */
|
||||||
|
export const SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
||||||
|
|
||||||
|
/** Letters that Unicode normalization does not split into a base letter and an accent. */
|
||||||
|
const SPECIAL_LETTERS: Record<string, string> = { æ: 'ae', œ: 'oe', ø: 'o', ß: 'ss', đ: 'd', ł: 'l', þ: 'th' };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turns a name into a slug: accents removed, lowercase, every run of other characters replaced by one hyphen,
|
||||||
|
* cut at a word boundary to {@link MAX_SLUG_LENGTH}. "&" becomes "and" ("&TEAM" -> "and-team", not "team").
|
||||||
|
* @returns an empty string when nothing is left (name without any latin letter or digit, e.g. in hangul).
|
||||||
|
*/
|
||||||
|
export function slugify(name: string, maxLength = MAX_SLUG_LENGTH): string {
|
||||||
|
const slug = name
|
||||||
|
.normalize('NFKD')
|
||||||
|
.replace(/[\u0300-\u036f]/g, '')
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/[æœøßđłþ]/g, (letter) => SPECIAL_LETTERS[letter])
|
||||||
|
.replace(/&/g, ' and ')
|
||||||
|
.replace(/[^a-z0-9]+/g, '-')
|
||||||
|
.replace(/^-+|-+$/g, '');
|
||||||
|
return truncateSlug(slug, maxLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cuts a slug to `maxLength` at a word boundary: keeps the cut as is when it ends on a whole word, else cuts at its last
|
||||||
|
* hyphen when there is one. Never leaves a trailing hyphen.
|
||||||
|
*/
|
||||||
|
function truncateSlug(slug: string, maxLength: number): string {
|
||||||
|
if (slug.length <= maxLength) return slug;
|
||||||
|
const cut = slug.slice(0, maxLength);
|
||||||
|
if (slug[maxLength] === '-') return cut;
|
||||||
|
const lastHyphen = cut.lastIndexOf('-');
|
||||||
|
return (lastHyphen > 0 ? cut.slice(0, lastHyphen) : cut).replace(/-+$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Appends `-suffix` to a slug, shortening the slug so that the result fits in {@link MAX_SLUG_LENGTH}. */
|
||||||
|
export function withSlugSuffix(slug: string, suffix: string | number): string {
|
||||||
|
const end = `-${suffix}`;
|
||||||
|
return truncateSlug(slug, MAX_SLUG_LENGTH - end.length) + end;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Picks the first candidate not in `taken`. When they are all taken, numbers the last one (`-2`, `-3`...)
|
||||||
|
* until a free slug is found.
|
||||||
|
* @param candidates slugs by order of preference, at least one, none empty.
|
||||||
|
*/
|
||||||
|
export function pickAvailableSlug(candidates: string[], taken: ReadonlySet<string>): string {
|
||||||
|
const free = candidates.find((candidate) => !taken.has(candidate));
|
||||||
|
if (free) return free;
|
||||||
|
|
||||||
|
const last = candidates[candidates.length - 1];
|
||||||
|
for (let n = 2; ; n++) {
|
||||||
|
const numbered = withSlugSuffix(last, n);
|
||||||
|
if (!taken.has(numbered)) return numbered;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
import { Logger } from '@nestjs/common';
|
import { Logger } from '@nestjs/common';
|
||||||
|
import { rethrowIfRateLimited } from '../hiboutik/hiboutik.client';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs `action` on each item, one at a time: a failing item is logged and skipped, the others are still processed.
|
* Runs `action` on each item, one at a time: a failing item is logged and skipped, the others are still processed.
|
||||||
|
* A Hiboutik rate limit error stops the loop (rethrown): the next items would fail the same way.
|
||||||
|
* @param items
|
||||||
|
* @param action
|
||||||
|
* @param logger
|
||||||
* @param describe label of an item in the error log, e.g. "Hiboutik brand 12".
|
* @param describe label of an item in the error log, e.g. "Hiboutik brand 12".
|
||||||
* @returns the number of failed items.
|
* @returns the number of failed items.
|
||||||
*/
|
*/
|
||||||
@@ -16,6 +21,7 @@ export async function forEachSafely<T>(
|
|||||||
try {
|
try {
|
||||||
await action(item);
|
await action(item);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
rethrowIfRateLimited(error);
|
||||||
nbFailed++;
|
nbFailed++;
|
||||||
logger.error(`Failed to sync ${describe(item)}`, (error as Error)?.stack);
|
logger.error(`Failed to sync ${describe(item)}`, (error as Error)?.stack);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,14 +10,24 @@ import { PrismaService } from './../prisma/prisma.service';
|
|||||||
describe('AppController (e2e)', () => {
|
describe('AppController (e2e)', () => {
|
||||||
let app: INestApplication<App>;
|
let app: INestApplication<App>;
|
||||||
const hiboutikService = { startSync: jest.fn() };
|
const hiboutikService = { startSync: jest.fn() };
|
||||||
|
// Rate limiting counters (ThrottlerDatabaseStorage): the database answers "not blocked" unless a test says otherwise
|
||||||
|
const notBlocked = { totalHits: 1, timeToExpire: 60, isBlocked: false, timeToBlockExpire: 0 };
|
||||||
|
const prisma = {
|
||||||
|
$queryRaw: jest.fn(),
|
||||||
|
throttlerRecord: { deleteMany: jest.fn().mockResolvedValue({ count: 0 }) },
|
||||||
|
brand: { findMany: jest.fn() },
|
||||||
|
category: { findMany: jest.fn() },
|
||||||
|
product: { findMany: jest.fn() },
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
hiboutikService.startSync.mockReset();
|
hiboutikService.startSync.mockReset();
|
||||||
|
prisma.$queryRaw.mockReset().mockResolvedValue([notBlocked]);
|
||||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||||
imports: [AppModule],
|
imports: [AppModule],
|
||||||
})
|
})
|
||||||
.overrideProvider(PrismaService)
|
.overrideProvider(PrismaService)
|
||||||
.useValue({})
|
.useValue(prisma)
|
||||||
.overrideProvider(HiboutikService)
|
.overrideProvider(HiboutikService)
|
||||||
.useValue(hiboutikService)
|
.useValue(hiboutikService)
|
||||||
.compile();
|
.compile();
|
||||||
@@ -36,6 +46,44 @@ describe('AppController (e2e)', () => {
|
|||||||
return request(app.getHttpServer()).post('/sync').expect(409);
|
return request(app.getHttpServer()).post('/sync').expect(409);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('POST /sync answers 429 when the shared rate limiting counter is blocked', async () => {
|
||||||
|
prisma.$queryRaw.mockResolvedValue([{ totalHits: 6, timeToExpire: 30, isBlocked: true, timeToBlockExpire: 30 }]);
|
||||||
|
|
||||||
|
await request(app.getHttpServer()).post('/sync').expect(429).expect('Retry-After', '30');
|
||||||
|
expect(hiboutikService.startSync).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('GET /sitemap lists the public pages', async () => {
|
||||||
|
prisma.brand.findMany.mockResolvedValue([{ slug: 'bts' }]);
|
||||||
|
prisma.category.findMany.mockResolvedValue([{ slug: 'albums' }]);
|
||||||
|
prisma.product.findMany.mockResolvedValue([
|
||||||
|
{ slug: 'bts-proof', hiboutikUpdatedAt: new Date('2026-11-05T08:00:00Z') },
|
||||||
|
{ slug: 'bts-wings', hiboutikUpdatedAt: null },
|
||||||
|
]);
|
||||||
|
|
||||||
|
await request(app.getHttpServer())
|
||||||
|
.get('/sitemap')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Cache-Control', 'public, max-age=600')
|
||||||
|
.expect({
|
||||||
|
brands: [{ slug: 'bts' }],
|
||||||
|
categories: [{ slug: 'albums' }],
|
||||||
|
products: [
|
||||||
|
{ slug: 'bts-proof', updatedAt: '2026-11-05T08:00:00.000Z' },
|
||||||
|
{ slug: 'bts-wings', updatedAt: null },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
// only public rows with a slug are read; products are dated by Hiboutik, not by the last sync
|
||||||
|
expect(prisma.brand.findMany).toHaveBeenCalledWith(expect.objectContaining({ where: { slug: { not: null }, enabled: true } }));
|
||||||
|
expect(prisma.category.findMany).toHaveBeenCalledWith(expect.objectContaining({ where: { slug: { not: null }, webEnabled: true } }));
|
||||||
|
expect(prisma.product.findMany).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
where: { slug: { not: null }, enabled: true, archived: false },
|
||||||
|
select: { slug: true, hiboutikUpdatedAt: true },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await app.close();
|
await app.close();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,10 +16,8 @@
|
|||||||
"baseUrl": "./",
|
"baseUrl": "./",
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strictNullChecks": true,
|
"strict": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"noImplicitAny": false,
|
|
||||||
"strictBindCallApply": false,
|
|
||||||
"noFallthroughCasesInSwitch": false
|
"noFallthroughCasesInSwitch": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user