|
1
|
import { model } from "@medusajs/framework/utils" |
|
2
|
import TaxProvider from "./tax-provider" |
|
3
|
import TaxRate from "./tax-rate" |
|
4
|
|
|
5
|
export const taxRegionProviderTopLevelCheckName = |
|
6
|
"CK_tax_region_provider_top_level" |
|
7
|
export const taxRegionCountryTopLevelCheckName = |
|
8
|
"CK_tax_region_country_top_level" |
|
9
|
|
|
10
|
const TaxRegion = model |
|
11
|
.define("TaxRegion", { |
|
12
|
id: model.id({ prefix: "txreg" }).primaryKey(), |
|
13
|
country_code: model.text().searchable(), |
|
14
|
province_code: model.text().searchable().nullable(), |
|
15
|
metadata: model.json().nullable(), |
|
16
|
created_by: model.text().nullable(), |
|
17
|
provider: model |
|
18
|
.belongsTo(() => TaxProvider, { |
|
19
|
mappedBy: "regions", |
|
20
|
}) |
|
21
|
.nullable(), |
|
22
|
parent: model |
|
23
|
.belongsTo(() => TaxRegion, { |
|
24
|
mappedBy: "children", |
|
25
|
}) |
|
26
|
.nullable(), |
|
27
|
children: model.hasMany(() => TaxRegion, { |
|
28
|
mappedBy: "parent", |
|
29
|
}), |
|
30
|
tax_rates: model.hasMany(() => TaxRate, { |
|
31
|
mappedBy: "tax_region", |
|
32
|
}), |
|
33
|
}) |
|
34
|
.checks([ |
|
35
|
{ |
|
36
|
name: taxRegionProviderTopLevelCheckName, |
|
37
|
expression: `parent_id IS NULL OR provider_id IS NULL`, |
|
38
|
}, |
|
39
|
{ |
|
40
|
name: taxRegionCountryTopLevelCheckName, |
|
41
|
expression: `parent_id IS NULL OR province_code IS NOT NULL`, |
|
42
|
}, |
|
43
|
]) |
|
44
|
.indexes([ |
|
45
|
{ |
|
46
|
name: "IDX_tax_region_unique_country_province", |
|
47
|
on: ["country_code", "province_code"], |
|
48
|
unique: true, |
|
49
|
where: "deleted_at IS NULL", |
|
50
|
}, |
|
51
|
{ |
|
52
|
name: "IDX_tax_region_unique_country_nullable_province", |
|
53
|
on: ["country_code"], |
|
54
|
unique: true, |
|
55
|
where: "province_code IS NULL AND deleted_at IS NULL", |
|
56
|
}, |
|
57
|
]) |
|
58
|
.cascades({ |
|
59
|
delete: ["children", "tax_rates"], |
|
60
|
}) |
|
61
|
export default TaxRegion |