@rytass/wms-module-core 0.10.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from './lib/constants';
6
6
  export * from './lib/events';
7
7
  export * from './lib/table-prefix/table-prefix';
8
8
  export * from './lib/utils/run-in-transaction';
9
+ export * from './lib/utils/list-sort';
@@ -2,6 +2,8 @@ import { DataSource, EntityManager, EntityTarget } from 'typeorm';
2
2
  import { InventoryCountStatus } from '../constants/inventory-count-status';
3
3
  import { InventoryCountOrderEntityClass, InventoryCountOrderItemEntityClass } from '../models';
4
4
  import { WmsEntities } from '../models/wms-entities';
5
+ import { InventoryCountOrderItemSortField, InventoryCountOrderSortField } from '../typings/find-inventory-count-order.input';
6
+ import { ListSort } from '../typings/list-sort';
5
7
  import { BatchService } from './batch.service';
6
8
  import { HelperService } from './helper.service';
7
9
  import { MaterialService } from './material.service';
@@ -15,6 +17,8 @@ export interface FindInventoryCountOrderInput {
15
17
  readonly locationIds?: readonly string[] | null;
16
18
  readonly offset?: number;
17
19
  readonly limit?: number;
20
+ /** Omitted: latest count date first, then newest created, then `id`. */
21
+ readonly sort?: ListSort<InventoryCountOrderSortField> | null;
18
22
  }
19
23
  /**
20
24
  * Filters for the count sheet. All optional; combined with AND.
@@ -38,6 +42,11 @@ export interface FindInventoryCountOrderItemInput {
38
42
  readonly onlyVariance?: boolean | null;
39
43
  readonly offset?: number;
40
44
  readonly limit?: number;
45
+ /**
46
+ * Omitted: additions first, then material key, batch key, `id`. The CSV
47
+ * export and the upload's row matching read the whole sheet and do not care.
48
+ */
49
+ readonly sort?: ListSort<InventoryCountOrderItemSortField> | null;
41
50
  }
42
51
  export interface CreateInventoryCountOrderInput {
43
52
  readonly name: string;
@@ -9,12 +9,6 @@ import { BatchService } from './batch.service';
9
9
  import { HelperService } from './helper.service';
10
10
  import { StockService } from './stock.service';
11
11
  import { WmsEntities } from '../models/wms-entities';
12
- /**
13
- * Service for managing receive inventory orders and their items
14
- * Supports dynamic entity injection for customization
15
- * T - Receive Inventory Order entity type
16
- * K - Receive Inventory Order Item entity type
17
- */
18
12
  export declare class ReceiveInventoryOrderService<T extends ReceiveInventoryOrderEntityClass = ReceiveInventoryOrderEntityClass, K extends ReceiveInventoryOrderItemEntityClass = ReceiveInventoryOrderItemEntityClass> {
19
13
  private readonly dataSource;
20
14
  private readonly entities;
@@ -10,12 +10,6 @@ import { MaterialService } from './material.service';
10
10
  import { BatchService } from './batch.service';
11
11
  import { StockService } from './stock.service';
12
12
  import { WmsEntities } from '../models/wms-entities';
13
- /**
14
- * Service for managing ship inventory orders and their items
15
- * Supports dynamic entity injection for customization
16
- * T - Ship Inventory Order entity type
17
- * K - Ship Inventory Order Item entity type
18
- */
19
13
  export declare class ShipInventoryOrderService<T extends ShipInventoryOrderEntityClass = ShipInventoryOrderEntityClass, K extends ShipInventoryOrderItemEntityClass = ShipInventoryOrderItemEntityClass> {
20
14
  private readonly dataSource;
21
15
  private readonly entities;
@@ -2,9 +2,11 @@ import { OnModuleInit } from '@nestjs/common';
2
2
  import { StockEntityClass } from '../models/metadata/stock.entity';
3
3
  import { DataSource, EntityManager, Repository, SelectQueryBuilder, type EntityTarget } from 'typeorm';
4
4
  import { FindStockInput } from '../typings/find-stock.input';
5
+ import { FindAggregatedStockInput, FindStockGroupedByMaterialInput } from '../typings/find-stock-sort.input';
5
6
  import { MaterialEntityClass } from '../models/metadata/material.entity';
6
7
  import { StockStatus } from '../constants/stock-status';
7
8
  import { AggregatedStock, ChangeLog, ValidateStockDeductionParams } from '../typings/aggregated-stock';
9
+ import { FindChangeLogInput } from '../typings/find-change-log.input';
8
10
  import type { WmsModuleCoreModuleOptions } from '../typings/core-module-options';
9
11
  import { WmsEntities } from '../models/wms-entities';
10
12
  export declare class StockService<T extends StockEntityClass = StockEntityClass, K extends MaterialEntityClass = MaterialEntityClass> implements OnModuleInit {
@@ -47,7 +49,21 @@ export declare class StockService<T extends StockEntityClass = StockEntityClass,
47
49
  resolveLocationId(locationId: string | null | undefined): string;
48
50
  private pipeSelectOptionsToQueryBuilder;
49
51
  findMany(options: FindStockInput<T>): Promise<[T[], number]>;
50
- findManyGroupedByMaterialId(options: FindStockInput<T>): Promise<[K[], number]>;
52
+ /**
53
+ * Materials with stock, one page of them, in one statement.
54
+ *
55
+ * A material is listed when any of its (material, batch, location, loader,
56
+ * status) positions nets to non-zero after the filters — the aggregated
57
+ * subquery's `HAVING` — not when the material's own total does: +5 in one
58
+ * place and -5 in another is listed. Soft-deleted materials are left out
59
+ * (the material repository is the main alias).
60
+ *
61
+ * The positions are summed per material in the database, joined to the
62
+ * material, sorted and paged there; `total` comes from a window count on the
63
+ * same statement. Only a page past the end, which has no row to carry it,
64
+ * costs a second query.
65
+ */
66
+ findManyGroupedByMaterialId(options: FindStockGroupedByMaterialInput<T>): Promise<[K[], number]>;
51
67
  findAggregatedStock(options: Omit<AggregatedStock, 'quantity'>, manager?: EntityManager): Promise<AggregatedStock | undefined>;
52
68
  /**
53
69
  * Take the transaction-scoped stock locks for `slots`, deduplicated and in a
@@ -81,6 +97,14 @@ export declare class StockService<T extends StockEntityClass = StockEntityClass,
81
97
  * the first commit and the sum is stale even after the wait.
82
98
  */
83
99
  validateStockAvailability(params: ValidateStockDeductionParams, manager: EntityManager): Promise<void>;
84
- findAggregatedStocks(options: FindStockInput<T>, manager?: EntityManager): Promise<[AggregatedStock[], number]>;
85
- findChangeLogs(options: FindStockInput<T>): Promise<[ChangeLog[], number]>;
100
+ /**
101
+ * Join what a sort field of `findAggregatedStocks` orders by, and nothing
102
+ * else: an unsorted call (every internal caller) keeps its plain query.
103
+ * Each join is on a primary key, so the outer query stays one row per
104
+ * aggregated position and LIMIT/OFFSET still cut whole rows. Entities, not
105
+ * table names, so a table prefix and an overridden entity both follow.
106
+ */
107
+ private joinAggregatedStockSortTarget;
108
+ findAggregatedStocks(options: FindAggregatedStockInput<T>, manager?: EntityManager): Promise<[AggregatedStock[], number]>;
109
+ findChangeLogs(options: FindChangeLogInput<T>): Promise<[ChangeLog[], number]>;
86
110
  }
@@ -1,13 +1,51 @@
1
1
  import { OnApplicationBootstrap } from '@nestjs/common';
2
- import { DataSource, Repository } from 'typeorm';
2
+ import { DataSource, EntityManager, Repository } from 'typeorm';
3
3
  import { WarehouseMapEntity } from '@rytass/wms-base-nestjs-module/models/warehouse-map.entity';
4
4
  import { WmsEntities } from '../models/wms-entities';
5
+ /**
6
+ * Where a location is drawn, and the chain of maps that leads to it.
7
+ *
8
+ * `path` runs from the root map down to `mapKey` (inclusive), using raw map ids.
9
+ * In a normal deployment the first element is the seeded root map's id,
10
+ * `DEFAULT_MAP_DATA_KEY`; a UI that labels it differently (react shows `ROOT`)
11
+ * translates on display.
12
+ */
13
+ export interface LocationMapPreview {
14
+ readonly locationKey: string;
15
+ readonly mapKey: string;
16
+ readonly path: readonly string[];
17
+ readonly mapData: WarehouseMapEntity['mapData'];
18
+ }
19
+ /** Ancestor walk limit; a deeper chain than this is treated as corrupt data. */
20
+ export declare const LOCATION_MAP_PREVIEW_MAX_DEPTH = 32;
5
21
  export declare class WarehouseMapDataService implements OnApplicationBootstrap {
6
22
  private readonly dataSource;
7
23
  private readonly entities;
8
24
  readonly defaultMapDataKey = "DEFAULT_MAP_DATA_KEY";
9
25
  readonly warehouseMapDataRepository: Repository<WarehouseMapEntity>;
10
26
  constructor(dataSource: DataSource, entities: WmsEntities);
27
+ /**
28
+ * The map that draws `locationKey`, plus the breadcrumb from the root to it.
29
+ *
30
+ * The map hierarchy is kept apart from `locations.parentId`: a map's id is the
31
+ * key of the location it drills into, and `mapData.ranges[].id` names the
32
+ * child locations drawn on it. So "where is X drawn" is "which map has a range
33
+ * whose id is X", and a map's parent is the map that draws the map's own id.
34
+ *
35
+ * Returns `null` when no map draws the location.
36
+ *
37
+ * When a key is drawn on several maps (the editor does not forbid it), the map
38
+ * with the lowest id wins — byte order (`COLLATE "C"`), so every deployment
39
+ * agrees regardless of its database locale, rather than whatever the planner
40
+ * returns first. The same rule picks a parent at every step of the walk.
41
+ *
42
+ * The walk stops at a map nobody draws, at an id it has already visited
43
+ * (a cycle in hand-edited map data), or after
44
+ * `LOCATION_MAP_PREVIEW_MAX_DEPTH` maps.
45
+ */
46
+ findLocationMapPreview(locationKey: string, manager?: EntityManager): Promise<LocationMapPreview | null>;
47
+ /** The lowest-id map whose ranges include one with `id = rangeId`. */
48
+ private findMapDrawing;
11
49
  /**
12
50
  * Seed the default map row.
13
51
  *
@@ -1,4 +1,5 @@
1
1
  import { PaginationInput } from './paginations';
2
+ import { ListSort } from './list-sort';
2
3
  export type CreateConsumptionOrderItemInput = {
3
4
  materialId: string;
4
5
  batchId: string;
@@ -39,6 +40,18 @@ export type ConsumptionOrderActionInput = {
39
40
  /** Omit to act on every line of the order. */
40
41
  items?: ConsumptionOrderLineInput[];
41
42
  };
43
+ /**
44
+ * Columns `ConsumptionOrderService.findMany` can sort by. Values are part of
45
+ * the public GraphQL schema (`ConsumptionOrderSortField`): add freely, never
46
+ * rename or remove. Item-derived columns (sources, targets, item count,
47
+ * issued/outstanding/settled totals) are left out: they aggregate many lines
48
+ * and have no column to order by.
49
+ */
50
+ export declare enum ConsumptionOrderSortField {
51
+ KEY = "KEY",
52
+ DESCRIPTION = "DESCRIPTION",
53
+ CREATED_AT = "CREATED_AT"
54
+ }
42
55
  export type FindConsumptionOrderInput = PaginationInput & {
43
56
  ids?: string[];
44
57
  key?: string;
@@ -50,6 +63,8 @@ export type FindConsumptionOrderInput = PaginationInput & {
50
63
  createdAtFrom?: Date;
51
64
  createdAtTo?: Date;
52
65
  includeRemoved?: boolean;
66
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
67
+ sort?: ListSort<ConsumptionOrderSortField> | null;
53
68
  };
54
69
  /**
55
70
  * How much of one line has moved, **in base units** — the unit the stock ledger
@@ -0,0 +1,28 @@
1
+ import { FindStockInput } from './find-stock.input';
2
+ import { ListSort } from './list-sort';
3
+ /**
4
+ * Columns `StockService.findChangeLogs` can sort by. Values are part of the
5
+ * public GraphQL schema (`ChangeLogSortField`): add freely, never rename or
6
+ * remove.
7
+ */
8
+ export declare enum ChangeLogSortField {
9
+ /** The computed change type, in `ChangeType` declaration order. */
10
+ CHANGE_TYPE = "CHANGE_TYPE",
11
+ MATERIAL_KEY = "MATERIAL_KEY",
12
+ MATERIAL_DESCRIPTION = "MATERIAL_DESCRIPTION",
13
+ BATCH_KEY = "BATCH_KEY",
14
+ LOCATION_KEY = "LOCATION_KEY",
15
+ /** The quantity's magnitude, as `changeLogs` returns it. */
16
+ QUANTITY = "QUANTITY",
17
+ /** The material's base unit `key`. */
18
+ BASE_UNIT = "BASE_UNIT",
19
+ CREATED_AT = "CREATED_AT"
20
+ }
21
+ /**
22
+ * The stock filters plus the change log's own sort. `sort` is omitted from the
23
+ * stock input first so a sort added to stock lists never collides with this one.
24
+ */
25
+ export type FindChangeLogInput<U extends object> = Omit<FindStockInput<U>, 'sort'> & {
26
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
27
+ sort?: ListSort<ChangeLogSortField> | null;
28
+ };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Columns `InventoryCountOrderService.findMany` can sort by. Values are part of
3
+ * the public GraphQL schema (`InventoryCountOrderSortField`): add freely, never
4
+ * rename or remove. The scope (盤點範圍) is left out: a count covers many
5
+ * locations, so it has no single value to sort by.
6
+ */
7
+ export declare enum InventoryCountOrderSortField {
8
+ NAME = "NAME",
9
+ /** The PostgreSQL enum's declaration order: pending → counting → completed → cancelled. */
10
+ STATUS = "STATUS",
11
+ COUNT_DATE = "COUNT_DATE",
12
+ CREATED_BY = "CREATED_BY",
13
+ CREATED_AT = "CREATED_AT"
14
+ }
15
+ /**
16
+ * Columns `InventoryCountOrderService.findItems` (the count sheet) can sort by
17
+ * (`InventoryCountOrderItemSortField`). The quantity columns are left out: the
18
+ * sheet shows them in the line's selected unit, which the operator can change
19
+ * locally before saving, so no server order could match what is on screen.
20
+ */
21
+ export declare enum InventoryCountOrderItemSortField {
22
+ MATERIAL_KEY = "MATERIAL_KEY",
23
+ MATERIAL_DESCRIPTION = "MATERIAL_DESCRIPTION",
24
+ BATCH_KEY = "BATCH_KEY",
25
+ LOCATION_KEY = "LOCATION_KEY",
26
+ LOADER_SERIAL_ID = "LOADER_SERIAL_ID",
27
+ VENDOR_ID = "VENDOR_ID",
28
+ CUSTOMER_ID = "CUSTOMER_ID",
29
+ SALE_ID = "SALE_ID",
30
+ SALE_SERIAL_ID = "SALE_SERIAL_ID",
31
+ /** Lines the operator added during the count. */
32
+ IS_ADDITION = "IS_ADDITION",
33
+ /** The 凍結 icon: `held`, a NULL snapshot read as not held. */
34
+ HELD = "HELD",
35
+ /** The 調撥 icon: the line had a `transferId`. */
36
+ IN_TRANSFER = "IN_TRANSFER",
37
+ /**
38
+ * The 待檢驗 icon, ranked: no mark (0) < failed inspection (1) < awaiting
39
+ * inspection (2, `requiresInspection` and no result yet).
40
+ */
41
+ INSPECTION = "INSPECTION"
42
+ }
@@ -1,4 +1,34 @@
1
1
  import { PaginationInput } from './paginations';
2
+ import { ListSort } from './list-sort';
3
+ /**
4
+ * Columns `LoaderService.findManyLoaders` can sort by. Values are part of the
5
+ * public GraphQL schema (`LoaderSortField`): add freely, never rename or remove.
6
+ */
7
+ export declare enum LoaderSortField {
8
+ SERIAL_ID = "SERIAL_ID",
9
+ /** The loader type's `description`. */
10
+ TYPE_DESCRIPTION = "TYPE_DESCRIPTION",
11
+ /** The location's `key`. */
12
+ LOCATION_KEY = "LOCATION_KEY",
13
+ LENGTH = "LENGTH",
14
+ WIDTH = "WIDTH",
15
+ HEIGHT = "HEIGHT",
16
+ SIZE_UNIT = "SIZE_UNIT",
17
+ VOLUME = "VOLUME",
18
+ VOLUME_UNIT = "VOLUME_UNIT",
19
+ NOTE = "NOTE",
20
+ IS_ACTIVE = "IS_ACTIVE",
21
+ CREATED_AT = "CREATED_AT"
22
+ }
23
+ /**
24
+ * Columns `LoaderService.findManyLoaderTypes` can sort by (`LoaderTypeSortField`).
25
+ */
26
+ export declare enum LoaderTypeSortField {
27
+ KEY = "KEY",
28
+ DESCRIPTION = "DESCRIPTION",
29
+ IS_ACTIVE = "IS_ACTIVE",
30
+ CREATED_AT = "CREATED_AT"
31
+ }
2
32
  export type FindLoaderInput<U> = PaginationInput & {
3
33
  typeId?: string;
4
34
  typeIds?: string[];
@@ -18,6 +48,8 @@ export type FindLoaderInput<U> = PaginationInput & {
18
48
  key: keyof U;
19
49
  value: string;
20
50
  };
51
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
52
+ sort?: ListSort<LoaderSortField> | null;
21
53
  };
22
54
  export type FindLoaderTypeInput<U> = PaginationInput & {
23
55
  description?: string;
@@ -30,4 +62,6 @@ export type FindLoaderTypeInput<U> = PaginationInput & {
30
62
  key: keyof U;
31
63
  value: string;
32
64
  };
65
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
66
+ sort?: ListSort<LoaderTypeSortField> | null;
33
67
  };
@@ -1,4 +1,15 @@
1
1
  import { PaginationInput } from './paginations';
2
+ import { ListSort } from './list-sort';
3
+ /**
4
+ * Columns `LocationService.findMany` can sort by. Values are part of the public
5
+ * GraphQL schema (`LocationSortField`): add freely, never rename or remove.
6
+ */
7
+ export declare enum LocationSortField {
8
+ KEY = "KEY",
9
+ DESCRIPTION = "DESCRIPTION",
10
+ IS_ACTIVE = "IS_ACTIVE",
11
+ CREATED_AT = "CREATED_AT"
12
+ }
2
13
  export type FindLocationInput<U extends object> = PaginationInput & {
3
14
  description?: string;
4
15
  isActive?: boolean;
@@ -7,4 +18,6 @@ export type FindLocationInput<U extends object> = PaginationInput & {
7
18
  key: keyof U;
8
19
  value: string;
9
20
  };
21
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
22
+ sort?: ListSort<LocationSortField> | null;
10
23
  };
@@ -1,4 +1,18 @@
1
1
  import { PaginationInput } from './paginations';
2
+ import { ListSort } from './list-sort';
3
+ /**
4
+ * Columns `MaterialService.findMany` can sort by. Values are part of the public
5
+ * GraphQL schema (`MaterialSortField`): adding one is a minor change, renaming
6
+ * or removing one is breaking.
7
+ */
8
+ export declare enum MaterialSortField {
9
+ KEY = "KEY",
10
+ DESCRIPTION = "DESCRIPTION",
11
+ /** The base unit's `key` (the column stores the unit's id). */
12
+ BASE_UNIT = "BASE_UNIT",
13
+ IS_ACTIVE = "IS_ACTIVE",
14
+ CREATED_AT = "CREATED_AT"
15
+ }
2
16
  export type FindMaterialInput<U> = PaginationInput & {
3
17
  materialId?: string;
4
18
  materialIds?: string[];
@@ -7,4 +21,6 @@ export type FindMaterialInput<U> = PaginationInput & {
7
21
  key: keyof U;
8
22
  value: string;
9
23
  };
24
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
25
+ sort?: ListSort<MaterialSortField> | null;
10
26
  };
@@ -1,10 +1,28 @@
1
1
  import { PaginationInput } from './paginations';
2
+ import { ListSort } from './list-sort';
2
3
  export declare enum ReceiveInventoryOrderStatus {
3
4
  PENDING = "PENDING",
4
5
  RECEIVED = "RECEIVED",
5
6
  REMOVED = "REMOVED",
6
7
  PARTIAL_RECEIVED = "PARTIAL_RECEIVED"
7
8
  }
9
+ /**
10
+ * Columns `ReceiveInventoryOrderService.findMany` can sort by. Values are part
11
+ * of the public GraphQL schema (`ReceiveInventoryOrderSortField`): add freely, never rename or
12
+ * remove. Per-item columns (material, batch, location, loader) are left out:
13
+ * an order has many items, so they have no single value to sort by.
14
+ */
15
+ export declare enum ReceiveInventoryOrderSortField {
16
+ /**
17
+ * The computed status, in lifecycle order: pending → partially received →
18
+ * received → removed.
19
+ */
20
+ STATUS = "STATUS",
21
+ KEY = "KEY",
22
+ CREATED_BY = "CREATED_BY",
23
+ CREATED_AT = "CREATED_AT",
24
+ DESCRIPTION = "DESCRIPTION"
25
+ }
8
26
  export type FindReceiveInventoryOrderInput<T> = PaginationInput & {
9
27
  id?: string;
10
28
  createdBy?: string;
@@ -23,4 +41,6 @@ export type FindReceiveInventoryOrderInput<T> = PaginationInput & {
23
41
  key: keyof T;
24
42
  value: string;
25
43
  };
44
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
45
+ sort?: ListSort<ReceiveInventoryOrderSortField> | null;
26
46
  };
@@ -1,10 +1,28 @@
1
1
  import { PaginationInput } from './paginations';
2
+ import { ListSort } from './list-sort';
2
3
  export declare enum ShipInventoryOrderStatus {
3
4
  PENDING = "PENDING",
4
5
  SHIPPED = "SHIPPED",
5
6
  REMOVED = "REMOVED",
6
7
  PARTIAL_SHIPPED = "PARTIAL_SHIPPED"
7
8
  }
9
+ /**
10
+ * Columns `ShipInventoryOrderService.findMany` can sort by. Values are part
11
+ * of the public GraphQL schema (`ShipInventoryOrderSortField`): add freely, never rename or
12
+ * remove. Per-item columns (material, batch, location, loader) are left out:
13
+ * an order has many items, so they have no single value to sort by.
14
+ */
15
+ export declare enum ShipInventoryOrderSortField {
16
+ /**
17
+ * The computed status, in lifecycle order: pending → partially shipped →
18
+ * shipped → removed.
19
+ */
20
+ STATUS = "STATUS",
21
+ KEY = "KEY",
22
+ CREATED_BY = "CREATED_BY",
23
+ CREATED_AT = "CREATED_AT",
24
+ DESCRIPTION = "DESCRIPTION"
25
+ }
8
26
  export type FindShipInventoryOrderInput<T> = PaginationInput & {
9
27
  id?: string;
10
28
  createdBy?: string;
@@ -23,4 +41,6 @@ export type FindShipInventoryOrderInput<T> = PaginationInput & {
23
41
  key: keyof T;
24
42
  value: string;
25
43
  };
44
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
45
+ sort?: ListSort<ShipInventoryOrderSortField> | null;
26
46
  };
@@ -0,0 +1,52 @@
1
+ import type { FindStockInput } from './find-stock.input';
2
+ import type { ListSort } from './list-sort';
3
+ /**
4
+ * Columns `StockService.findAggregatedStocks` can sort by. Values are part of
5
+ * the public GraphQL schema (`AggregatedStockSortField`): adding one is a minor
6
+ * change, renaming or removing one is breaking.
7
+ *
8
+ * Every one is single-valued per aggregated row: a row is one (material, batch,
9
+ * location, loader, status) position, and each joined key is one-to-one with
10
+ * its id.
11
+ */
12
+ export declare enum AggregatedStockSortField {
13
+ /** The derived status code (`FREE`, `HELD`, …), compared as text. */
14
+ STATUS = "STATUS",
15
+ MATERIAL_KEY = "MATERIAL_KEY",
16
+ MATERIAL_DESCRIPTION = "MATERIAL_DESCRIPTION",
17
+ /** The batch *group*'s key — what the `batch { key }` field shows. */
18
+ BATCH_KEY = "BATCH_KEY",
19
+ LOCATION_KEY = "LOCATION_KEY",
20
+ LOADER_SERIAL_ID = "LOADER_SERIAL_ID",
21
+ QUANTITY = "QUANTITY",
22
+ /** The material's base unit `key`. */
23
+ BASE_UNIT = "BASE_UNIT",
24
+ VENDOR_ID = "VENDOR_ID",
25
+ CUSTOMER_ID = "CUSTOMER_ID",
26
+ SALE_ID = "SALE_ID",
27
+ SALE_SERIAL_ID = "SALE_SERIAL_ID"
28
+ }
29
+ export type FindAggregatedStockInput<U extends object> = Omit<FindStockInput<U>, 'sort'> & {
30
+ /**
31
+ * Omitted: by the group key — material id, batch id, location id, loader id,
32
+ * status — which is stable but not meaningful to a reader. A sort is
33
+ * followed by that same group key as the tiebreaker.
34
+ */
35
+ sort?: ListSort<AggregatedStockSortField> | null;
36
+ };
37
+ /**
38
+ * Columns `StockService.findManyGroupedByMaterialId` can sort by. Values are
39
+ * part of the public GraphQL schema (`StockGroupedByMaterialSortField`).
40
+ */
41
+ export declare enum StockGroupedByMaterialSortField {
42
+ MATERIAL_KEY = "MATERIAL_KEY",
43
+ MATERIAL_DESCRIPTION = "MATERIAL_DESCRIPTION",
44
+ /** The material's total over the matching positions (`SUM(quantity)`). */
45
+ QUANTITY = "QUANTITY",
46
+ /** The material's base unit `key`. */
47
+ BASE_UNIT = "BASE_UNIT"
48
+ }
49
+ export type FindStockGroupedByMaterialInput<U extends object> = Omit<FindStockInput<U>, 'sort'> & {
50
+ /** Omitted: material key ascending, then material id. */
51
+ sort?: ListSort<StockGroupedByMaterialSortField> | null;
52
+ };
@@ -1,4 +1,18 @@
1
1
  import { PaginationInput } from './paginations';
2
+ import { ListSort } from './list-sort';
3
+ /**
4
+ * Columns `TransferOrderService.findMany` can sort by. Values are part of the
5
+ * public GraphQL schema (`TransferOrderSortField`): add freely, never rename or
6
+ * remove. Per-item columns (material, batch, locations, loaders) are left out:
7
+ * an order has many items, so they have no single value to sort by.
8
+ */
9
+ export declare enum TransferOrderSortField {
10
+ KEY = "KEY",
11
+ RECEIVER = "RECEIVER",
12
+ DESCRIPTION = "DESCRIPTION",
13
+ CREATED_BY = "CREATED_BY",
14
+ CREATED_AT = "CREATED_AT"
15
+ }
2
16
  export type FindTransferOrderInput = PaginationInput & {
3
17
  key?: string;
4
18
  receiver?: string;
@@ -18,4 +32,6 @@ export type FindTransferOrderInput = PaginationInput & {
18
32
  loaderKey?: string;
19
33
  targetLocationKey?: string;
20
34
  targetLoaderKey?: string;
35
+ /** Omitted: newest first (`createdAt DESC`), then `id`. */
36
+ sort?: ListSort<TransferOrderSortField> | null;
21
37
  };
@@ -12,6 +12,7 @@ export * from './find-material.input';
12
12
  export * from './find-receive-inventory-order.input';
13
13
  export * from './find-ship-inventory-order.input';
14
14
  export * from './find-stock.input';
15
+ export * from './find-change-log.input';
15
16
  export * from './consumption-order.input';
16
17
  export * from './custom-change-table';
17
18
  export * from './order-reference.input';
@@ -29,6 +30,7 @@ export * from './deallocate-inventory-order';
29
30
  export * from './find-allocate-inventory-order.input';
30
31
  export * from './find-batch.input';
31
32
  export * from './find-hold-inventory-order.input';
33
+ export * from './find-inventory-count-order.input';
32
34
  export * from './find-location.input';
33
35
  export * from './find-transfer-order.input';
34
36
  export type { GetBatchInput, SplitBatchInput as BatchSplitAttributesInput, } from './get-batch.input';
@@ -39,3 +41,5 @@ export * from './scrape.input';
39
41
  export * from './ship-inventory-order.input';
40
42
  export * from './split-batch.input';
41
43
  export * from './transfer-order.input';
44
+ export * from './list-sort';
45
+ export * from './find-stock-sort.input';
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Direction of a list sort. Exposed through GraphQL as the `SortOrder` enum.
3
+ */
4
+ export declare enum SortOrder {
5
+ ASC = "ASC",
6
+ DESC = "DESC"
7
+ }
8
+ /**
9
+ * One sort criterion a caller asks a list for. `F` is that list's own
10
+ * `<List>SortField` enum, so a field that list does not whitelist is a type
11
+ * error — and, for a caller that bypasses the types, a runtime error from
12
+ * `applyListSort`.
13
+ */
14
+ export interface ListSort<F extends string> {
15
+ field: F;
16
+ order: SortOrder;
17
+ }
@@ -0,0 +1,78 @@
1
+ import type { ObjectLiteral, SelectQueryBuilder } from 'typeorm';
2
+ import { ListSort, SortOrder } from '../typings/list-sort';
3
+ /**
4
+ * How the list is read, which decides what an ORDER BY expression may be.
5
+ *
6
+ * - `entity` — `getMany()` / `getManyAndCount()`. Every expression must be an
7
+ * `alias.propertyPath` naming a mapped column. With `skip`/`take` and a join
8
+ * TypeORM wraps the query in a `DISTINCT` subquery and rewrites each ORDER BY
9
+ * key into a column of that subquery; it can only do that for a property path
10
+ * on an alias with metadata, and only if that column is in the inner SELECT.
11
+ * Anything else (a function call, a raw column name, an unselected join) is
12
+ * either rejected by TypeORM or silently resolves to a column that does not
13
+ * exist, so `applyListSort` checks it up front.
14
+ * - `raw` — `getRawMany()` paginated with `offset`/`limit`. Expressions are
15
+ * used verbatim: a selected alias (`status`), or SQL over the query's aliases.
16
+ */
17
+ export type ListSortMode = 'entity' | 'raw';
18
+ export interface ListSortOptions<F extends string> {
19
+ /** What the caller asked for. `null`/`undefined` falls back to `defaultOrder`. */
20
+ sort?: ListSort<F> | null;
21
+ /**
22
+ * Whitelist: sort field → ORDER BY expression. A `Record` over the whole
23
+ * enum, so adding an enum member without mapping it does not compile.
24
+ */
25
+ fields: Readonly<Record<F, string>>;
26
+ /** Used when `sort` is absent. One criterion or several, in order. */
27
+ defaultOrder: ListSort<F> | readonly ListSort<F>[];
28
+ /**
29
+ * A unique expression (normally the primary key) appended last, ascending,
30
+ * so rows that tie on every criterion still come back in one fixed order —
31
+ * without it `LIMIT`/`OFFSET` pages can repeat or skip rows.
32
+ *
33
+ * A list without a single-column key (an aggregate, whose row is identified
34
+ * by its whole group key) passes the key's columns in order; together they
35
+ * must be unique.
36
+ */
37
+ tiebreaker: string | readonly string[];
38
+ /** Defaults to `entity`. */
39
+ mode?: ListSortMode;
40
+ }
41
+ export interface ListSortClause {
42
+ expression: string;
43
+ order: SortOrder;
44
+ nulls?: 'NULLS FIRST' | 'NULLS LAST';
45
+ }
46
+ /**
47
+ * NULLs sort last in **both** directions.
48
+ *
49
+ * PostgreSQL's own default flips with the direction (NULLS LAST for ASC, NULLS
50
+ * FIRST for DESC), so a column with gaps would open every descending page with
51
+ * its empty rows. A missing value is "no value", not "the largest value": it
52
+ * belongs at the end whichever way the user sorts.
53
+ */
54
+ export declare const LIST_SORT_NULLS: "NULLS LAST";
55
+ /**
56
+ * The ORDER BY clauses `applyListSort` would apply, without a query builder.
57
+ * Pure — this is what the unit tests pin down.
58
+ *
59
+ * Throws on a field outside the whitelist or an unknown direction: the GraphQL
60
+ * enums already reject both, so reaching here with one means a host called the
61
+ * service directly with a bad value, and falling back to the default order would
62
+ * hand it a plausible-looking wrong answer.
63
+ */
64
+ export declare function resolveListSort<F extends string>(options: Omit<ListSortOptions<F>, 'mode'>): ListSortClause[];
65
+ /**
66
+ * Replace the query's ORDER BY with the requested sort (or `defaultOrder`),
67
+ * then the tiebreaker. Call it after every join the sort expressions need.
68
+ *
69
+ * ```ts
70
+ * applyListSort(qb, {
71
+ * sort: options.sort,
72
+ * fields: { [MaterialSortField.KEY]: 'material.key', ... },
73
+ * defaultOrder: { field: MaterialSortField.CREATED_AT, order: SortOrder.DESC },
74
+ * tiebreaker: 'material.id',
75
+ * });
76
+ * ```
77
+ */
78
+ export declare function applyListSort<E extends ObjectLiteral, F extends string>(qb: SelectQueryBuilder<E>, options: ListSortOptions<F>): SelectQueryBuilder<E>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rytass/wms-module-core",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "description": "Rytass WMS Module — NestJS services and TypeORM entities for warehouse management",
5
5
  "keywords": [
6
6
  "rytass",