@tailor-platform/erp-kit 0.0.1 → 0.1.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/README.md +196 -28
- package/dist/cli.js +894 -0
- package/package.json +65 -8
- package/rules/app-compose/backend/auth.md +78 -0
- package/rules/app-compose/frontend/auth.md +55 -0
- package/rules/app-compose/frontend/component.md +55 -0
- package/rules/app-compose/frontend/page.md +86 -0
- package/rules/app-compose/frontend/screen-detailview.md +112 -0
- package/rules/app-compose/frontend/screen-form.md +145 -0
- package/rules/app-compose/frontend/screen-listview.md +159 -0
- package/rules/app-compose/structure.md +32 -0
- package/rules/module-development/commands.md +54 -0
- package/rules/module-development/cross-module-type-injection.md +28 -0
- package/rules/module-development/dependency-modules.md +24 -0
- package/rules/module-development/errors.md +12 -0
- package/rules/module-development/executors.md +67 -0
- package/rules/module-development/exports.md +13 -0
- package/rules/module-development/models.md +34 -0
- package/rules/module-development/structure.md +27 -0
- package/rules/module-development/sync-vs-async-operations.md +83 -0
- package/rules/module-development/testing.md +43 -0
- package/rules/sdk-best-practices/db-relations.md +74 -0
- package/rules/sdk-best-practices/sdk-docs.md +14 -0
- package/schemas/app-compose/actors.yml +34 -0
- package/schemas/app-compose/business-flow.yml +50 -0
- package/schemas/app-compose/requirements.yml +33 -0
- package/schemas/app-compose/resolver.yml +47 -0
- package/schemas/app-compose/screen.yml +81 -0
- package/schemas/app-compose/story.yml +67 -0
- package/schemas/module/command.yml +52 -0
- package/schemas/module/feature.yml +58 -0
- package/schemas/module/model.yml +70 -0
- package/schemas/module/module.yml +50 -0
- package/skills/1-module-docs/SKILL.md +107 -0
- package/skills/2-module-feature-breakdown/SKILL.md +66 -0
- package/skills/3-module-doc-review/SKILL.md +230 -0
- package/skills/4-module-tdd-implementation/SKILL.md +56 -0
- package/skills/5-module-implementation-review/SKILL.md +400 -0
- package/skills/app-compose-1-requirement-analysis/SKILL.md +85 -0
- package/skills/app-compose-2-requirements-breakdown/SKILL.md +88 -0
- package/skills/app-compose-3-doc-review/SKILL.md +112 -0
- package/skills/app-compose-4-design-mock/SKILL.md +248 -0
- package/skills/app-compose-5-design-mock-review/SKILL.md +283 -0
- package/skills/app-compose-6-implementation-spec/SKILL.md +122 -0
- package/skills/mock-scenario/SKILL.md +118 -0
- package/src/app.ts +1 -0
- package/src/cli.ts +120 -0
- package/src/commands/check.test.ts +30 -0
- package/src/commands/check.ts +66 -0
- package/src/commands/init.test.ts +77 -0
- package/src/commands/init.ts +87 -0
- package/src/commands/mock/index.ts +53 -0
- package/src/commands/mock/start.ts +179 -0
- package/src/commands/mock/validate.test.ts +185 -0
- package/src/commands/mock/validate.ts +198 -0
- package/src/commands/scaffold.test.ts +76 -0
- package/src/commands/scaffold.ts +119 -0
- package/src/commands/sync-check.test.ts +125 -0
- package/src/commands/sync-check.ts +182 -0
- package/src/integration.test.ts +63 -0
- package/src/mdschema.ts +48 -0
- package/src/mockServer.ts +55 -0
- package/src/module.ts +86 -0
- package/src/modules/accounting/.gitkeep +0 -0
- package/src/modules/coa-management/.gitkeep +0 -0
- package/src/modules/inventory/.gitkeep +0 -0
- package/src/modules/manufacturing/.gitkeep +0 -0
- package/src/modules/primitives/README.md +39 -0
- package/src/modules/primitives/command/activateCategory.test.ts +75 -0
- package/src/modules/primitives/command/activateCategory.ts +50 -0
- package/src/modules/primitives/command/activateCurrency.test.ts +70 -0
- package/src/modules/primitives/command/activateCurrency.ts +50 -0
- package/src/modules/primitives/command/activateUnit.test.ts +53 -0
- package/src/modules/primitives/command/activateUnit.ts +50 -0
- package/src/modules/primitives/command/convertAmount.test.ts +275 -0
- package/src/modules/primitives/command/convertAmount.ts +126 -0
- package/src/modules/primitives/command/convertQuantity.test.ts +219 -0
- package/src/modules/primitives/command/convertQuantity.ts +73 -0
- package/src/modules/primitives/command/createCategory.test.ts +126 -0
- package/src/modules/primitives/command/createCategory.ts +89 -0
- package/src/modules/primitives/command/createCurrency.test.ts +191 -0
- package/src/modules/primitives/command/createCurrency.ts +77 -0
- package/src/modules/primitives/command/createExchangeRate.test.ts +216 -0
- package/src/modules/primitives/command/createExchangeRate.ts +91 -0
- package/src/modules/primitives/command/createUnit.test.ts +214 -0
- package/src/modules/primitives/command/createUnit.ts +88 -0
- package/src/modules/primitives/command/deactivateCategory.test.ts +97 -0
- package/src/modules/primitives/command/deactivateCategory.ts +62 -0
- package/src/modules/primitives/command/deactivateCurrency.test.ts +85 -0
- package/src/modules/primitives/command/deactivateCurrency.ts +55 -0
- package/src/modules/primitives/command/deactivateUnit.test.ts +78 -0
- package/src/modules/primitives/command/deactivateUnit.ts +62 -0
- package/src/modules/primitives/command/setBaseCurrency.test.ts +98 -0
- package/src/modules/primitives/command/setBaseCurrency.ts +74 -0
- package/src/modules/primitives/command/setReferenceUnit.test.ts +108 -0
- package/src/modules/primitives/command/setReferenceUnit.ts +84 -0
- package/src/modules/primitives/db/currency.ts +30 -0
- package/src/modules/primitives/db/exchangeRate.ts +28 -0
- package/src/modules/primitives/db/unit.ts +32 -0
- package/src/modules/primitives/db/uomCategory.ts +32 -0
- package/src/modules/primitives/docs/commands/ActivateCategory.md +34 -0
- package/src/modules/primitives/docs/commands/ActivateCurrency.md +33 -0
- package/src/modules/primitives/docs/commands/ActivateUnit.md +34 -0
- package/src/modules/primitives/docs/commands/ConvertAmount.md +50 -0
- package/src/modules/primitives/docs/commands/ConvertQuantity.md +43 -0
- package/src/modules/primitives/docs/commands/CreateCategory.md +44 -0
- package/src/modules/primitives/docs/commands/CreateCurrency.md +47 -0
- package/src/modules/primitives/docs/commands/CreateExchangeRate.md +48 -0
- package/src/modules/primitives/docs/commands/CreateUnit.md +48 -0
- package/src/modules/primitives/docs/commands/DeactivateCategory.md +38 -0
- package/src/modules/primitives/docs/commands/DeactivateCurrency.md +38 -0
- package/src/modules/primitives/docs/commands/DeactivateUnit.md +38 -0
- package/src/modules/primitives/docs/commands/SetBaseCurrency.md +39 -0
- package/src/modules/primitives/docs/commands/SetReferenceUnit.md +43 -0
- package/src/modules/primitives/docs/features/currency-definitions.md +55 -0
- package/src/modules/primitives/docs/features/exchange-rates.md +61 -0
- package/src/modules/primitives/docs/features/unit-conversion.md +66 -0
- package/src/modules/primitives/docs/features/uom-categories.md +52 -0
- package/src/modules/primitives/docs/models/Currency.md +45 -0
- package/src/modules/primitives/docs/models/ExchangeRate.md +33 -0
- package/src/modules/primitives/docs/models/Unit.md +46 -0
- package/src/modules/primitives/docs/models/UoMCategory.md +44 -0
- package/src/modules/primitives/generated/kysely-tailordb.ts +95 -0
- package/src/modules/primitives/index.ts +40 -0
- package/src/modules/primitives/lib/errors.ts +138 -0
- package/src/modules/primitives/lib/types.ts +20 -0
- package/src/modules/primitives/module.ts +66 -0
- package/src/modules/primitives/permissions.ts +18 -0
- package/src/modules/primitives/tailor.config.ts +11 -0
- package/src/modules/primitives/testing/fixtures.ts +161 -0
- package/src/modules/product-management/.gitkeep +0 -0
- package/src/modules/purchase/.gitkeep +0 -0
- package/src/modules/sales/.gitkeep +0 -0
- package/src/modules/shared/createContext.test.ts +39 -0
- package/src/modules/shared/createContext.ts +15 -0
- package/src/modules/shared/defineCommand.test.ts +42 -0
- package/src/modules/shared/defineCommand.ts +19 -0
- package/src/modules/shared/definePermissions.test.ts +146 -0
- package/src/modules/shared/definePermissions.ts +94 -0
- package/src/modules/shared/entityTypes.ts +15 -0
- package/src/modules/shared/errors.ts +22 -0
- package/src/modules/shared/index.ts +1 -0
- package/src/modules/shared/internal.ts +13 -0
- package/src/modules/shared/requirePermission.test.ts +47 -0
- package/src/modules/shared/requirePermission.ts +8 -0
- package/src/modules/shared/types.ts +4 -0
- package/src/modules/supplier-management/.gitkeep +0 -0
- package/src/modules/supplier-portal/.gitkeep +0 -0
- package/src/modules/testing/index.ts +120 -0
- package/src/modules/user-management/README.md +38 -0
- package/src/modules/user-management/command/activateUser.test.ts +112 -0
- package/src/modules/user-management/command/activateUser.ts +67 -0
- package/src/modules/user-management/command/assignPermissionToRole.test.ts +119 -0
- package/src/modules/user-management/command/assignPermissionToRole.ts +87 -0
- package/src/modules/user-management/command/assignRoleToUser.test.ts +162 -0
- package/src/modules/user-management/command/assignRoleToUser.ts +93 -0
- package/src/modules/user-management/command/createPermission.test.ts +143 -0
- package/src/modules/user-management/command/createPermission.ts +66 -0
- package/src/modules/user-management/command/createRole.test.ts +115 -0
- package/src/modules/user-management/command/createRole.ts +52 -0
- package/src/modules/user-management/command/createUser.test.ts +198 -0
- package/src/modules/user-management/command/createUser.ts +85 -0
- package/src/modules/user-management/command/deactivateUser.test.ts +112 -0
- package/src/modules/user-management/command/deactivateUser.ts +67 -0
- package/src/modules/user-management/command/logAuditEvent.test.ts +179 -0
- package/src/modules/user-management/command/logAuditEvent.ts +59 -0
- package/src/modules/user-management/command/reactivateUser.test.ts +115 -0
- package/src/modules/user-management/command/reactivateUser.ts +67 -0
- package/src/modules/user-management/command/revokePermissionFromRole.test.ts +112 -0
- package/src/modules/user-management/command/revokePermissionFromRole.ts +81 -0
- package/src/modules/user-management/command/revokeRoleFromUser.test.ts +112 -0
- package/src/modules/user-management/command/revokeRoleFromUser.ts +81 -0
- package/src/modules/user-management/db/auditEvent.ts +47 -0
- package/src/modules/user-management/db/permission.ts +31 -0
- package/src/modules/user-management/db/role.ts +28 -0
- package/src/modules/user-management/db/rolePermission.ts +44 -0
- package/src/modules/user-management/db/user.ts +38 -0
- package/src/modules/user-management/db/userRole.ts +44 -0
- package/src/modules/user-management/docs/commands/ActivateUser.md +36 -0
- package/src/modules/user-management/docs/commands/AssignPermissionToRole.md +39 -0
- package/src/modules/user-management/docs/commands/AssignRoleToUser.md +43 -0
- package/src/modules/user-management/docs/commands/CreatePermission.md +35 -0
- package/src/modules/user-management/docs/commands/CreateRole.md +35 -0
- package/src/modules/user-management/docs/commands/CreateUser.md +41 -0
- package/src/modules/user-management/docs/commands/DeactivateUser.md +38 -0
- package/src/modules/user-management/docs/commands/LogAuditEvent.md +37 -0
- package/src/modules/user-management/docs/commands/ReactivateUser.md +37 -0
- package/src/modules/user-management/docs/commands/RevokePermissionFromRole.md +40 -0
- package/src/modules/user-management/docs/commands/RevokeRoleFromUser.md +40 -0
- package/src/modules/user-management/docs/features/audit-trail.md +80 -0
- package/src/modules/user-management/docs/features/role-based-access-control.md +76 -0
- package/src/modules/user-management/docs/features/user-account-management.md +64 -0
- package/src/modules/user-management/docs/models/AuditEvent.md +34 -0
- package/src/modules/user-management/docs/models/Permission.md +31 -0
- package/src/modules/user-management/docs/models/Role.md +31 -0
- package/src/modules/user-management/docs/models/RolePermission.md +33 -0
- package/src/modules/user-management/docs/models/User.md +47 -0
- package/src/modules/user-management/docs/models/UserRole.md +34 -0
- package/src/modules/user-management/docs/plans/2026-01-30-flattened-permissions-design.md +52 -0
- package/src/modules/user-management/executor/recomputeOnRolePermissionChange.ts +61 -0
- package/src/modules/user-management/generated/enums.ts +24 -0
- package/src/modules/user-management/generated/kysely-tailordb.ts +112 -0
- package/src/modules/user-management/index.ts +32 -0
- package/src/modules/user-management/lib/errors.ts +81 -0
- package/src/modules/user-management/lib/recomputeUserPermissions.ts +53 -0
- package/src/modules/user-management/lib/types.ts +31 -0
- package/src/modules/user-management/module.ts +77 -0
- package/src/modules/user-management/permissions.ts +15 -0
- package/src/modules/user-management/tailor.config.ts +11 -0
- package/src/modules/user-management/testing/fixtures.ts +98 -0
- package/src/schemas.ts +25 -0
- package/src/testing.ts +10 -0
- package/src/util.ts +3 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { defineCommand } from "../../shared/internal";
|
|
2
|
+
import { DB } from "../generated/kysely-tailordb";
|
|
3
|
+
import {
|
|
4
|
+
CurrencyNotFoundError,
|
|
5
|
+
ExchangeRateNotFoundError,
|
|
6
|
+
InactiveCurrencyError,
|
|
7
|
+
} from "../lib/errors";
|
|
8
|
+
import { permissions } from "../permissions";
|
|
9
|
+
|
|
10
|
+
export interface ConvertAmountInput {
|
|
11
|
+
amount: number;
|
|
12
|
+
sourceCurrencyCode: string;
|
|
13
|
+
targetCurrencyCode: string;
|
|
14
|
+
conversionDate: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Function: convertAmount
|
|
19
|
+
*
|
|
20
|
+
* Converts a monetary amount from one currency to another using the applicable
|
|
21
|
+
* exchange rate for a given date. Currencies are identified by their ISO 4217
|
|
22
|
+
* code (e.g., "USD", "EUR", "JPY"). The function looks up the most recent exchange
|
|
23
|
+
* rate on or before the specified date. If no direct rate exists, it calculates
|
|
24
|
+
* the inverse rate. Result is rounded to the target currency's decimal precision.
|
|
25
|
+
*/
|
|
26
|
+
export const convertAmount = defineCommand(
|
|
27
|
+
permissions.convertAmount,
|
|
28
|
+
async (db: DB, input: ConvertAmountInput) => {
|
|
29
|
+
// 1. Validate source currency exists
|
|
30
|
+
const sourceCurrency = await db
|
|
31
|
+
.selectFrom("Currency")
|
|
32
|
+
.selectAll()
|
|
33
|
+
.where("code", "=", input.sourceCurrencyCode)
|
|
34
|
+
.executeTakeFirst();
|
|
35
|
+
|
|
36
|
+
if (!sourceCurrency) {
|
|
37
|
+
throw new CurrencyNotFoundError(input.sourceCurrencyCode);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 2. Validate target currency exists
|
|
41
|
+
const targetCurrency = await db
|
|
42
|
+
.selectFrom("Currency")
|
|
43
|
+
.selectAll()
|
|
44
|
+
.where("code", "=", input.targetCurrencyCode)
|
|
45
|
+
.executeTakeFirst();
|
|
46
|
+
|
|
47
|
+
if (!targetCurrency) {
|
|
48
|
+
throw new CurrencyNotFoundError(input.targetCurrencyCode);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 3. Validate both currencies are active
|
|
52
|
+
if (!sourceCurrency.isActive) {
|
|
53
|
+
throw new InactiveCurrencyError(input.sourceCurrencyCode);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!targetCurrency.isActive) {
|
|
57
|
+
throw new InactiveCurrencyError(input.targetCurrencyCode);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 4. Same currency - return original amount
|
|
61
|
+
if (sourceCurrency.id === targetCurrency.id) {
|
|
62
|
+
return {
|
|
63
|
+
convertedAmount: input.amount,
|
|
64
|
+
exchangeRate: 1,
|
|
65
|
+
sourceCurrency,
|
|
66
|
+
targetCurrency,
|
|
67
|
+
exchangeRateRecord: null,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 5. Find direct exchange rate (most recent on or before conversion date)
|
|
72
|
+
const conversionDate = new Date(input.conversionDate);
|
|
73
|
+
const directRate = await db
|
|
74
|
+
.selectFrom("ExchangeRate")
|
|
75
|
+
.selectAll()
|
|
76
|
+
.where("sourceCurrencyId", "=", sourceCurrency.id)
|
|
77
|
+
.where("targetCurrencyId", "=", targetCurrency.id)
|
|
78
|
+
.where("effectiveDate", "<=", conversionDate)
|
|
79
|
+
.orderBy("effectiveDate", "desc")
|
|
80
|
+
.executeTakeFirst();
|
|
81
|
+
|
|
82
|
+
let exchangeRate: number;
|
|
83
|
+
let exchangeRateRecord = null;
|
|
84
|
+
|
|
85
|
+
if (directRate) {
|
|
86
|
+
exchangeRate = directRate.rate;
|
|
87
|
+
exchangeRateRecord = directRate;
|
|
88
|
+
} else {
|
|
89
|
+
// 6. Try inverse rate
|
|
90
|
+
const inverseRate = await db
|
|
91
|
+
.selectFrom("ExchangeRate")
|
|
92
|
+
.selectAll()
|
|
93
|
+
.where("sourceCurrencyId", "=", targetCurrency.id)
|
|
94
|
+
.where("targetCurrencyId", "=", sourceCurrency.id)
|
|
95
|
+
.where("effectiveDate", "<=", conversionDate)
|
|
96
|
+
.orderBy("effectiveDate", "desc")
|
|
97
|
+
.executeTakeFirst();
|
|
98
|
+
|
|
99
|
+
if (!inverseRate) {
|
|
100
|
+
throw new ExchangeRateNotFoundError(
|
|
101
|
+
input.sourceCurrencyCode,
|
|
102
|
+
input.targetCurrencyCode,
|
|
103
|
+
input.conversionDate,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
exchangeRate = 1 / inverseRate.rate;
|
|
108
|
+
exchangeRateRecord = inverseRate;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 7. Calculate converted amount
|
|
112
|
+
const rawResult = input.amount * exchangeRate;
|
|
113
|
+
|
|
114
|
+
// 8. Round to target currency's decimal places
|
|
115
|
+
const roundingFactor = Math.pow(10, targetCurrency.decimalPlaces);
|
|
116
|
+
const convertedAmount = Math.round(rawResult * roundingFactor) / roundingFactor;
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
convertedAmount,
|
|
120
|
+
exchangeRate,
|
|
121
|
+
sourceCurrency,
|
|
122
|
+
targetCurrency,
|
|
123
|
+
exchangeRateRecord,
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
);
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createMockDb } from "../../testing/index";
|
|
3
|
+
import { InsufficientPermissionError, type CommandContext } from "../../shared/internal";
|
|
4
|
+
import { DB } from "../generated/kysely-tailordb";
|
|
5
|
+
import { InactiveUnitError, IncompatibleUnitsError, UnitNotFoundError } from "../lib/errors";
|
|
6
|
+
import {
|
|
7
|
+
baseUnitGram,
|
|
8
|
+
baseUnitKg,
|
|
9
|
+
baseUnitLiter,
|
|
10
|
+
baseUnitPound,
|
|
11
|
+
inactiveUnit,
|
|
12
|
+
} from "../testing/fixtures";
|
|
13
|
+
import { convertQuantity } from "./convertQuantity";
|
|
14
|
+
|
|
15
|
+
describe("convertQuantity", () => {
|
|
16
|
+
const ctx: CommandContext = {
|
|
17
|
+
actorId: "test-actor",
|
|
18
|
+
permissions: ["primitives:convertQuantity"],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Error cases first
|
|
22
|
+
it("throws when source unit doesn't exist", async () => {
|
|
23
|
+
const { db, spies } = createMockDb<DB>();
|
|
24
|
+
spies.select.mockReturnValue(undefined);
|
|
25
|
+
|
|
26
|
+
await expect(
|
|
27
|
+
convertQuantity(
|
|
28
|
+
db,
|
|
29
|
+
{
|
|
30
|
+
quantity: 10,
|
|
31
|
+
sourceUnitSymbol: "xyz",
|
|
32
|
+
targetUnitSymbol: baseUnitGram.symbol,
|
|
33
|
+
},
|
|
34
|
+
ctx,
|
|
35
|
+
),
|
|
36
|
+
).rejects.toBeInstanceOf(UnitNotFoundError);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("throws when target unit doesn't exist", async () => {
|
|
40
|
+
const { db, spies } = createMockDb<DB>();
|
|
41
|
+
spies.select
|
|
42
|
+
.mockReturnValueOnce(baseUnitKg) // source unit exists
|
|
43
|
+
.mockReturnValueOnce(undefined); // target unit doesn't exist
|
|
44
|
+
|
|
45
|
+
await expect(
|
|
46
|
+
convertQuantity(
|
|
47
|
+
db,
|
|
48
|
+
{
|
|
49
|
+
quantity: 10,
|
|
50
|
+
sourceUnitSymbol: baseUnitKg.symbol,
|
|
51
|
+
targetUnitSymbol: "xyz",
|
|
52
|
+
},
|
|
53
|
+
ctx,
|
|
54
|
+
),
|
|
55
|
+
).rejects.toBeInstanceOf(UnitNotFoundError);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("throws when source unit is inactive", async () => {
|
|
59
|
+
const { db, spies } = createMockDb<DB>();
|
|
60
|
+
spies.select
|
|
61
|
+
.mockReturnValueOnce(inactiveUnit) // source unit inactive
|
|
62
|
+
.mockReturnValueOnce(baseUnitGram); // target unit exists
|
|
63
|
+
|
|
64
|
+
await expect(
|
|
65
|
+
convertQuantity(
|
|
66
|
+
db,
|
|
67
|
+
{
|
|
68
|
+
quantity: 10,
|
|
69
|
+
sourceUnitSymbol: inactiveUnit.symbol,
|
|
70
|
+
targetUnitSymbol: baseUnitGram.symbol,
|
|
71
|
+
},
|
|
72
|
+
ctx,
|
|
73
|
+
),
|
|
74
|
+
).rejects.toBeInstanceOf(InactiveUnitError);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("throws when target unit is inactive", async () => {
|
|
78
|
+
const { db, spies } = createMockDb<DB>();
|
|
79
|
+
spies.select
|
|
80
|
+
.mockReturnValueOnce(baseUnitKg) // source unit exists
|
|
81
|
+
.mockReturnValueOnce(inactiveUnit); // target unit inactive
|
|
82
|
+
|
|
83
|
+
await expect(
|
|
84
|
+
convertQuantity(
|
|
85
|
+
db,
|
|
86
|
+
{
|
|
87
|
+
quantity: 10,
|
|
88
|
+
sourceUnitSymbol: baseUnitKg.symbol,
|
|
89
|
+
targetUnitSymbol: inactiveUnit.symbol,
|
|
90
|
+
},
|
|
91
|
+
ctx,
|
|
92
|
+
),
|
|
93
|
+
).rejects.toBeInstanceOf(InactiveUnitError);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("throws when units belong to different categories", async () => {
|
|
97
|
+
const { db, spies } = createMockDb<DB>();
|
|
98
|
+
spies.select
|
|
99
|
+
.mockReturnValueOnce(baseUnitKg) // Weight category
|
|
100
|
+
.mockReturnValueOnce(baseUnitLiter); // Volume category
|
|
101
|
+
|
|
102
|
+
await expect(
|
|
103
|
+
convertQuantity(
|
|
104
|
+
db,
|
|
105
|
+
{
|
|
106
|
+
quantity: 10,
|
|
107
|
+
sourceUnitSymbol: baseUnitKg.symbol,
|
|
108
|
+
targetUnitSymbol: baseUnitLiter.symbol,
|
|
109
|
+
},
|
|
110
|
+
ctx,
|
|
111
|
+
),
|
|
112
|
+
).rejects.toBeInstanceOf(IncompatibleUnitsError);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Success cases
|
|
116
|
+
it("returns same quantity when source and target are the same unit", async () => {
|
|
117
|
+
const { db, spies } = createMockDb<DB>();
|
|
118
|
+
spies.select.mockReturnValueOnce(baseUnitKg).mockReturnValueOnce(baseUnitKg);
|
|
119
|
+
|
|
120
|
+
const result = await convertQuantity(
|
|
121
|
+
db,
|
|
122
|
+
{
|
|
123
|
+
quantity: 10,
|
|
124
|
+
sourceUnitSymbol: baseUnitKg.symbol,
|
|
125
|
+
targetUnitSymbol: baseUnitKg.symbol,
|
|
126
|
+
},
|
|
127
|
+
ctx,
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
expect(result.convertedQuantity).toBe(10);
|
|
131
|
+
expect(result.sourceUnit).toEqual(baseUnitKg);
|
|
132
|
+
expect(result.targetUnit).toEqual(baseUnitKg);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("converts kg to grams correctly", async () => {
|
|
136
|
+
const { db, spies } = createMockDb<DB>();
|
|
137
|
+
spies.select
|
|
138
|
+
.mockReturnValueOnce(baseUnitKg) // 1 kg = 1.0 (reference)
|
|
139
|
+
.mockReturnValueOnce(baseUnitGram); // 1 g = 0.001 kg
|
|
140
|
+
|
|
141
|
+
// 5 kg * 1.0 / 0.001 = 5000 g
|
|
142
|
+
const result = await convertQuantity(
|
|
143
|
+
db,
|
|
144
|
+
{
|
|
145
|
+
quantity: 5,
|
|
146
|
+
sourceUnitSymbol: baseUnitKg.symbol,
|
|
147
|
+
targetUnitSymbol: baseUnitGram.symbol,
|
|
148
|
+
},
|
|
149
|
+
ctx,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
expect(result.convertedQuantity).toBe(5000); // rounded to 0 decimal places (gram precision)
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("converts grams to kg correctly", async () => {
|
|
156
|
+
const { db, spies } = createMockDb<DB>();
|
|
157
|
+
spies.select
|
|
158
|
+
.mockReturnValueOnce(baseUnitGram) // 1 g = 0.001 kg
|
|
159
|
+
.mockReturnValueOnce(baseUnitKg); // 1 kg = 1.0 (reference)
|
|
160
|
+
|
|
161
|
+
// 2500 g * 0.001 / 1.0 = 2.5 kg
|
|
162
|
+
const result = await convertQuantity(
|
|
163
|
+
db,
|
|
164
|
+
{
|
|
165
|
+
quantity: 2500,
|
|
166
|
+
sourceUnitSymbol: baseUnitGram.symbol,
|
|
167
|
+
targetUnitSymbol: baseUnitKg.symbol,
|
|
168
|
+
},
|
|
169
|
+
ctx,
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
expect(result.convertedQuantity).toBe(2.5); // rounded to 2 decimal places (kg precision)
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("converts pounds to kg correctly", async () => {
|
|
176
|
+
const { db, spies } = createMockDb<DB>();
|
|
177
|
+
spies.select
|
|
178
|
+
.mockReturnValueOnce(baseUnitPound) // 1 lb = 0.453592 kg
|
|
179
|
+
.mockReturnValueOnce(baseUnitKg); // 1 kg = 1.0 (reference)
|
|
180
|
+
|
|
181
|
+
// 10 lb * 0.453592 / 1.0 = 4.53592 kg, rounded to 4.54
|
|
182
|
+
const result = await convertQuantity(
|
|
183
|
+
db,
|
|
184
|
+
{
|
|
185
|
+
quantity: 10,
|
|
186
|
+
sourceUnitSymbol: baseUnitPound.symbol,
|
|
187
|
+
targetUnitSymbol: baseUnitKg.symbol,
|
|
188
|
+
},
|
|
189
|
+
ctx,
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
expect(result.convertedQuantity).toBe(4.54);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("handles zero quantity correctly", async () => {
|
|
196
|
+
const { db, spies } = createMockDb<DB>();
|
|
197
|
+
spies.select.mockReturnValueOnce(baseUnitKg).mockReturnValueOnce(baseUnitGram);
|
|
198
|
+
|
|
199
|
+
const result = await convertQuantity(
|
|
200
|
+
db,
|
|
201
|
+
{
|
|
202
|
+
quantity: 0,
|
|
203
|
+
sourceUnitSymbol: baseUnitKg.symbol,
|
|
204
|
+
targetUnitSymbol: baseUnitGram.symbol,
|
|
205
|
+
},
|
|
206
|
+
ctx,
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
expect(result.convertedQuantity).toBe(0);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("throws when permission is missing", async () => {
|
|
213
|
+
const { db } = createMockDb<DB>();
|
|
214
|
+
const denied: CommandContext = { actorId: "test-actor", permissions: [] };
|
|
215
|
+
await expect(
|
|
216
|
+
convertQuantity(db, { quantity: 1, sourceUnitSymbol: "kg", targetUnitSymbol: "g" }, denied),
|
|
217
|
+
).rejects.toBeInstanceOf(InsufficientPermissionError);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { defineCommand } from "../../shared/internal";
|
|
2
|
+
import { DB } from "../generated/kysely-tailordb";
|
|
3
|
+
import { InactiveUnitError, IncompatibleUnitsError, UnitNotFoundError } from "../lib/errors";
|
|
4
|
+
import { permissions } from "../permissions";
|
|
5
|
+
|
|
6
|
+
export interface ConvertQuantityInput {
|
|
7
|
+
quantity: number;
|
|
8
|
+
sourceUnitSymbol: string;
|
|
9
|
+
targetUnitSymbol: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Function: convertQuantity
|
|
14
|
+
*
|
|
15
|
+
* Converts a quantity from one unit of measure to another within the same category.
|
|
16
|
+
* Units are identified by their symbol (e.g., "kg", "lb", "g").
|
|
17
|
+
* The conversion uses each unit's conversion factor relative to the category's reference unit.
|
|
18
|
+
* Result is rounded to the target unit's precision setting.
|
|
19
|
+
*/
|
|
20
|
+
export const convertQuantity = defineCommand(
|
|
21
|
+
permissions.convertQuantity,
|
|
22
|
+
async (db: DB, input: ConvertQuantityInput) => {
|
|
23
|
+
// 1. Validate source unit exists
|
|
24
|
+
const sourceUnit = await db
|
|
25
|
+
.selectFrom("Unit")
|
|
26
|
+
.selectAll()
|
|
27
|
+
.where("symbol", "=", input.sourceUnitSymbol)
|
|
28
|
+
.executeTakeFirst();
|
|
29
|
+
|
|
30
|
+
if (!sourceUnit) {
|
|
31
|
+
throw new UnitNotFoundError(input.sourceUnitSymbol);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 2. Validate target unit exists
|
|
35
|
+
const targetUnit = await db
|
|
36
|
+
.selectFrom("Unit")
|
|
37
|
+
.selectAll()
|
|
38
|
+
.where("symbol", "=", input.targetUnitSymbol)
|
|
39
|
+
.executeTakeFirst();
|
|
40
|
+
|
|
41
|
+
if (!targetUnit) {
|
|
42
|
+
throw new UnitNotFoundError(input.targetUnitSymbol);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 3. Validate both units are active
|
|
46
|
+
if (!sourceUnit.isActive) {
|
|
47
|
+
throw new InactiveUnitError(input.sourceUnitSymbol);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!targetUnit.isActive) {
|
|
51
|
+
throw new InactiveUnitError(input.targetUnitSymbol);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 4. Validate units belong to the same category
|
|
55
|
+
if (sourceUnit.categoryId !== targetUnit.categoryId) {
|
|
56
|
+
throw new IncompatibleUnitsError(input.sourceUnitSymbol, input.targetUnitSymbol);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 5. Perform conversion
|
|
60
|
+
// Formula: result = quantity * sourceConversionFactor / targetConversionFactor
|
|
61
|
+
const rawResult = (input.quantity * sourceUnit.conversionFactor) / targetUnit.conversionFactor;
|
|
62
|
+
|
|
63
|
+
// 6. Apply rounding to target unit's precision
|
|
64
|
+
const roundingFactor = Math.pow(10, targetUnit.roundingPrecision);
|
|
65
|
+
const convertedQuantity = Math.round(rawResult * roundingFactor) / roundingFactor;
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
convertedQuantity,
|
|
69
|
+
sourceUnit,
|
|
70
|
+
targetUnit,
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
);
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createMockDb } from "../../testing/index";
|
|
3
|
+
import { InsufficientPermissionError, type CommandContext } from "../../shared/internal";
|
|
4
|
+
import { DB } from "../generated/kysely-tailordb";
|
|
5
|
+
import { DuplicateCategoryNameError } from "../lib/errors";
|
|
6
|
+
import { baseUoMCategory, baseUnitKg } from "../testing/fixtures";
|
|
7
|
+
import { makeCreateCategory } from "./createCategory";
|
|
8
|
+
|
|
9
|
+
const createCategory = makeCreateCategory();
|
|
10
|
+
|
|
11
|
+
describe("createCategory", () => {
|
|
12
|
+
const ctx: CommandContext = { actorId: "test-actor", permissions: ["primitives:createCategory"] };
|
|
13
|
+
|
|
14
|
+
// Error cases
|
|
15
|
+
it("throws when category name already exists", async () => {
|
|
16
|
+
const { db, spies } = createMockDb<DB>();
|
|
17
|
+
spies.select.mockReturnValue(baseUoMCategory);
|
|
18
|
+
|
|
19
|
+
await expect(
|
|
20
|
+
createCategory(
|
|
21
|
+
db,
|
|
22
|
+
{
|
|
23
|
+
name: baseUoMCategory.name,
|
|
24
|
+
referenceUnit: {
|
|
25
|
+
name: "Kilogram",
|
|
26
|
+
symbol: "kg",
|
|
27
|
+
roundingPrecision: 2,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
ctx,
|
|
31
|
+
),
|
|
32
|
+
).rejects.toBeInstanceOf(DuplicateCategoryNameError);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Success cases
|
|
36
|
+
it("creates category with reference unit", async () => {
|
|
37
|
+
const { db, spies } = createMockDb<DB>();
|
|
38
|
+
const createdCategory = {
|
|
39
|
+
...baseUoMCategory,
|
|
40
|
+
id: "new-category-id",
|
|
41
|
+
referenceUnitId: "new-unit-id",
|
|
42
|
+
};
|
|
43
|
+
const createdUnit = {
|
|
44
|
+
...baseUnitKg,
|
|
45
|
+
id: "new-unit-id",
|
|
46
|
+
categoryId: "new-category-id",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
spies.select.mockReturnValue(undefined); // No existing category
|
|
50
|
+
spies.insert.mockReturnValueOnce(createdCategory).mockReturnValueOnce(createdUnit);
|
|
51
|
+
spies.update.mockReturnValue({ ...createdCategory, referenceUnitId: "new-unit-id" });
|
|
52
|
+
|
|
53
|
+
const result = await createCategory(
|
|
54
|
+
db,
|
|
55
|
+
{
|
|
56
|
+
name: "Weight",
|
|
57
|
+
description: "Weight units",
|
|
58
|
+
referenceUnit: {
|
|
59
|
+
name: "Kilogram",
|
|
60
|
+
symbol: "kg",
|
|
61
|
+
roundingPrecision: 2,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
ctx,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(result.category.name).toBe("Weight");
|
|
68
|
+
expect(result.category.isActive).toBe(true);
|
|
69
|
+
expect(result.referenceUnit.conversionFactor).toBe(1.0);
|
|
70
|
+
expect(spies.insert).toHaveBeenCalled();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("throws when permission is missing", async () => {
|
|
74
|
+
const { db } = createMockDb<DB>();
|
|
75
|
+
const denied: CommandContext = { actorId: "test-actor", permissions: [] };
|
|
76
|
+
await expect(
|
|
77
|
+
createCategory(
|
|
78
|
+
db,
|
|
79
|
+
{ name: "Test", referenceUnit: { name: "Unit", symbol: "u", roundingPrecision: 2 } },
|
|
80
|
+
denied,
|
|
81
|
+
),
|
|
82
|
+
).rejects.toBeInstanceOf(InsufficientPermissionError);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("passes custom fields through to insert", async () => {
|
|
86
|
+
const createCategoryWithFields = makeCreateCategory<
|
|
87
|
+
{ priority: number },
|
|
88
|
+
{ tolerance: number }
|
|
89
|
+
>();
|
|
90
|
+
const { db, spies } = createMockDb<DB>();
|
|
91
|
+
const createdCategory = {
|
|
92
|
+
...baseUoMCategory,
|
|
93
|
+
id: "new-category-id",
|
|
94
|
+
referenceUnitId: "new-unit-id",
|
|
95
|
+
};
|
|
96
|
+
const createdUnit = {
|
|
97
|
+
...baseUnitKg,
|
|
98
|
+
id: "new-unit-id",
|
|
99
|
+
categoryId: "new-category-id",
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
spies.select.mockReturnValue(undefined);
|
|
103
|
+
spies.insert.mockReturnValueOnce(createdCategory).mockReturnValueOnce(createdUnit);
|
|
104
|
+
spies.update.mockReturnValue({ ...createdCategory, referenceUnitId: "new-unit-id" });
|
|
105
|
+
|
|
106
|
+
await createCategoryWithFields(
|
|
107
|
+
db,
|
|
108
|
+
{
|
|
109
|
+
name: "Weight",
|
|
110
|
+
priority: 1,
|
|
111
|
+
referenceUnit: {
|
|
112
|
+
name: "Kilogram",
|
|
113
|
+
symbol: "kg",
|
|
114
|
+
roundingPrecision: 2,
|
|
115
|
+
tolerance: 0.01,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
ctx,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Category insert includes category custom field
|
|
122
|
+
expect(spies.values).toHaveBeenNthCalledWith(1, expect.objectContaining({ priority: 1 }));
|
|
123
|
+
// Unit insert includes unit custom field
|
|
124
|
+
expect(spies.values).toHaveBeenNthCalledWith(2, expect.objectContaining({ tolerance: 0.01 }));
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { defineCommand } from "../../shared/internal";
|
|
2
|
+
import { DB } from "../generated/kysely-tailordb";
|
|
3
|
+
import { DuplicateCategoryNameError } from "../lib/errors";
|
|
4
|
+
import { permissions } from "../permissions";
|
|
5
|
+
|
|
6
|
+
interface CreateCategoryInput<UnitCF extends Record<string, unknown>> {
|
|
7
|
+
name: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
referenceUnit: {
|
|
10
|
+
name: string;
|
|
11
|
+
symbol: string;
|
|
12
|
+
roundingPrecision: number;
|
|
13
|
+
} & UnitCF;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Function: createCategory
|
|
18
|
+
*
|
|
19
|
+
* Establishes a new unit of measure category that groups related units.
|
|
20
|
+
* Creates the category with a reference unit that has conversion factor 1.0.
|
|
21
|
+
*/
|
|
22
|
+
export function makeCreateCategory<
|
|
23
|
+
CatCF extends Record<string, unknown>,
|
|
24
|
+
UnitCF extends Record<string, unknown>,
|
|
25
|
+
>() {
|
|
26
|
+
return defineCommand(
|
|
27
|
+
permissions.createCategory,
|
|
28
|
+
async (db: DB, input: CreateCategoryInput<UnitCF> & CatCF) => {
|
|
29
|
+
const { name, description, referenceUnit, ...categoryCustomFields } = input;
|
|
30
|
+
const { name: unitName, symbol, roundingPrecision, ...unitCustomFields } = referenceUnit;
|
|
31
|
+
|
|
32
|
+
// 1. Check if category name already exists
|
|
33
|
+
const existingCategory = await db
|
|
34
|
+
.selectFrom("UoMCategory")
|
|
35
|
+
.selectAll()
|
|
36
|
+
.where("name", "=", name)
|
|
37
|
+
.executeTakeFirst();
|
|
38
|
+
|
|
39
|
+
if (existingCategory) {
|
|
40
|
+
throw new DuplicateCategoryNameError(name);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 2. Create category without reference unit first
|
|
44
|
+
const category = await db
|
|
45
|
+
.insertInto("UoMCategory")
|
|
46
|
+
.values({
|
|
47
|
+
...(categoryCustomFields as Record<string, unknown>),
|
|
48
|
+
name,
|
|
49
|
+
description: description ?? null,
|
|
50
|
+
referenceUnitId: null,
|
|
51
|
+
isActive: true,
|
|
52
|
+
createdAt: new Date(),
|
|
53
|
+
updatedAt: null,
|
|
54
|
+
})
|
|
55
|
+
.returningAll()
|
|
56
|
+
.executeTakeFirst();
|
|
57
|
+
|
|
58
|
+
// 3. Create reference unit with conversion factor 1.0
|
|
59
|
+
const createdReferenceUnit = await db
|
|
60
|
+
.insertInto("Unit")
|
|
61
|
+
.values({
|
|
62
|
+
...(unitCustomFields as Record<string, unknown>),
|
|
63
|
+
name: unitName,
|
|
64
|
+
symbol,
|
|
65
|
+
categoryId: category!.id,
|
|
66
|
+
conversionFactor: 1.0,
|
|
67
|
+
roundingPrecision,
|
|
68
|
+
isActive: true,
|
|
69
|
+
createdAt: new Date(),
|
|
70
|
+
updatedAt: null,
|
|
71
|
+
})
|
|
72
|
+
.returningAll()
|
|
73
|
+
.executeTakeFirst();
|
|
74
|
+
|
|
75
|
+
// 4. Update category with reference unit ID
|
|
76
|
+
const updatedCategory = await db
|
|
77
|
+
.updateTable("UoMCategory")
|
|
78
|
+
.set({
|
|
79
|
+
referenceUnitId: createdReferenceUnit!.id,
|
|
80
|
+
updatedAt: new Date(),
|
|
81
|
+
})
|
|
82
|
+
.where("id", "=", category!.id)
|
|
83
|
+
.returningAll()
|
|
84
|
+
.executeTakeFirst();
|
|
85
|
+
|
|
86
|
+
return { category: updatedCategory!, referenceUnit: createdReferenceUnit! };
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
}
|