|
1
|
import { Context } from "@medusajs/framework/types" |
|
2
|
import { |
|
3
|
BigNumber, |
|
4
|
MathBN, |
|
5
|
mikroOrmBaseRepositoryFactory, |
|
6
|
} from "@medusajs/framework/utils" |
|
7
|
import { SqlEntityManager } from "@medusajs/framework/mikro-orm/postgresql" |
|
8
|
import { InventoryLevel } from "@models" |
|
9
|
|
|
10
|
export class InventoryLevelRepository extends mikroOrmBaseRepositoryFactory( |
|
11
|
InventoryLevel |
|
12
|
) { |
|
13
|
async getReservedQuantity( |
|
14
|
inventoryItemId: string, |
|
15
|
locationIds: string[], |
|
16
|
context: Context = {} |
|
17
|
): Promise<BigNumber> { |
|
18
|
const manager = super.getActiveManager<SqlEntityManager>(context) |
|
19
|
|
|
20
|
const result = await manager |
|
21
|
.getKnex()({ il: "inventory_level" }) |
|
22
|
.select("raw_reserved_quantity") |
|
23
|
.whereIn("location_id", locationIds) |
|
24
|
.andWhere("inventory_item_id", inventoryItemId) |
|
25
|
.andWhereRaw("deleted_at IS NULL") |
|
26
|
|
|
27
|
return new BigNumber( |
|
28
|
MathBN.sum(...result.map((r) => r.raw_reserved_quantity)) |
|
29
|
) |
|
30
|
} |
|
31
|
|
|
32
|
async getAvailableQuantity( |
|
33
|
inventoryItemId: string, |
|
34
|
locationIds: string[], |
|
35
|
context: Context = {} |
|
36
|
): Promise<BigNumber> { |
|
37
|
const knex = super.getActiveManager<SqlEntityManager>(context).getKnex() |
|
38
|
|
|
39
|
const result = await knex({ |
|
40
|
il: "inventory_level", |
|
41
|
}) |
|
42
|
.select("raw_stocked_quantity", "raw_reserved_quantity") |
|
43
|
.whereIn("location_id", locationIds) |
|
44
|
.andWhere("inventory_item_id", inventoryItemId) |
|
45
|
.andWhereRaw("deleted_at IS NULL") |
|
46
|
|
|
47
|
return new BigNumber( |
|
48
|
MathBN.sum( |
|
49
|
...result.map((r) => { |
|
50
|
return MathBN.sub(r.raw_stocked_quantity, r.raw_reserved_quantity) |
|
51
|
}) |
|
52
|
) |
|
53
|
) |
|
54
|
} |
|
55
|
|
|
56
|
async getStockedQuantity( |
|
57
|
inventoryItemId: string, |
|
58
|
locationIds: string[], |
|
59
|
context: Context = {} |
|
60
|
): Promise<BigNumber> { |
|
61
|
const knex = super.getActiveManager<SqlEntityManager>(context).getKnex() |
|
62
|
|
|
63
|
const result = await knex({ |
|
64
|
il: "inventory_level", |
|
65
|
}) |
|
66
|
.select("raw_stocked_quantity") |
|
67
|
.whereIn("location_id", locationIds) |
|
68
|
.andWhere("inventory_item_id", inventoryItemId) |
|
69
|
.andWhereRaw("deleted_at IS NULL") |
|
70
|
|
|
71
|
return new BigNumber( |
|
72
|
MathBN.sum(...result.map((r) => r.raw_stocked_quantity)) |
|
73
|
) |
|
74
|
} |
|
75
|
} |