@unchainedshop/core-countries 4.3.3 → 4.4.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 +84 -2
- package/lib/countries-index.d.ts +1 -2
- package/lib/countries-index.js +1 -2
- package/lib/db/CountriesCollection.d.ts +1 -2
- package/lib/db/CountriesCollection.js +1 -2
- package/lib/module/configureCountriesModule.d.ts +2 -3
- package/lib/module/configureCountriesModule.js +2 -3
- package/package.json +10 -9
- package/lib/countries-index.d.ts.map +0 -1
- package/lib/countries-index.js.map +0 -1
- package/lib/db/CountriesCollection.d.ts.map +0 -1
- package/lib/db/CountriesCollection.js.map +0 -1
- package/lib/db/CountriesSchema.d.ts +0 -3
- package/lib/db/CountriesSchema.d.ts.map +0 -1
- package/lib/db/CountriesSchema.js +0 -9
- package/lib/db/CountriesSchema.js.map +0 -1
- package/lib/migrations/addMigrations.d.ts +0 -3
- package/lib/migrations/addMigrations.d.ts.map +0 -1
- package/lib/migrations/addMigrations.js +0 -28
- package/lib/migrations/addMigrations.js.map +0 -1
- package/lib/module/configureCountriesModule.d.ts.map +0 -1
- package/lib/module/configureCountriesModule.js.map +0 -1
- package/src/countries-index.ts +0 -1
- package/src/db/CountriesCollection.ts +0 -32
- package/src/module/buildFindSelector.test.ts +0 -18
- package/src/module/configureCountriesModule.ts +0 -149
- package/tsconfig.json +0 -10
package/README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://npmjs.com/package/@unchainedshop/core-countries)
|
|
2
|
+
[](https://opensource.org/licenses/EUPL-1.2)
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
# @unchainedshop/core-countries
|
|
5
|
+
|
|
6
|
+
Country management module for the Unchained Engine. Manages supported countries with ISO codes, currencies, and activation status.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @unchainedshop/core-countries
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { configureCountriesModule } from '@unchainedshop/core-countries';
|
|
18
|
+
|
|
19
|
+
const countriesModule = await configureCountriesModule({ db });
|
|
20
|
+
|
|
21
|
+
// Create a country
|
|
22
|
+
const countryId = await countriesModule.create({
|
|
23
|
+
isoCode: 'CH',
|
|
24
|
+
defaultCurrencyCode: 'CHF',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Find countries
|
|
28
|
+
const countries = await countriesModule.findCountries({ includeInactive: false });
|
|
29
|
+
|
|
30
|
+
// Get localized country name
|
|
31
|
+
const name = countriesModule.name(country, new Intl.Locale('en'));
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API Overview
|
|
35
|
+
|
|
36
|
+
### Module Configuration
|
|
37
|
+
|
|
38
|
+
| Export | Description |
|
|
39
|
+
|--------|-------------|
|
|
40
|
+
| `configureCountriesModule` | Configure and return the countries module |
|
|
41
|
+
|
|
42
|
+
### Queries
|
|
43
|
+
|
|
44
|
+
| Method | Description |
|
|
45
|
+
|--------|-------------|
|
|
46
|
+
| `findCountry` | Find country by ID or ISO code |
|
|
47
|
+
| `findCountries` | Find countries with filtering, sorting, and pagination |
|
|
48
|
+
| `count` | Count countries matching query |
|
|
49
|
+
| `countryExists` | Check if a country exists |
|
|
50
|
+
|
|
51
|
+
### Mutations
|
|
52
|
+
|
|
53
|
+
| Method | Description |
|
|
54
|
+
|--------|-------------|
|
|
55
|
+
| `create` | Create a new country |
|
|
56
|
+
| `update` | Update an existing country |
|
|
57
|
+
| `delete` | Soft delete a country |
|
|
58
|
+
|
|
59
|
+
### Helper Methods
|
|
60
|
+
|
|
61
|
+
| Method | Description |
|
|
62
|
+
|--------|-------------|
|
|
63
|
+
| `name` | Get localized country name using Intl.DisplayNames |
|
|
64
|
+
| `flagEmoji` | Get flag emoji for a country |
|
|
65
|
+
| `isBase` | Check if country is the base/system country |
|
|
66
|
+
|
|
67
|
+
### Types
|
|
68
|
+
|
|
69
|
+
| Export | Description |
|
|
70
|
+
|--------|-------------|
|
|
71
|
+
| `Country` | Country document type |
|
|
72
|
+
| `CountryQuery` | Query parameters type |
|
|
73
|
+
| `CountriesModule` | Module interface type |
|
|
74
|
+
|
|
75
|
+
## Events
|
|
76
|
+
|
|
77
|
+
| Event | Description |
|
|
78
|
+
|-------|-------------|
|
|
79
|
+
| `COUNTRY_CREATE` | Emitted when a country is created |
|
|
80
|
+
| `COUNTRY_UPDATE` | Emitted when a country is updated |
|
|
81
|
+
| `COUNTRY_REMOVE` | Emitted when a country is removed |
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
EUPL-1.2
|
package/lib/countries-index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from './module/configureCountriesModule.
|
|
2
|
-
//# sourceMappingURL=countries-index.d.ts.map
|
|
1
|
+
export * from './module/configureCountriesModule.ts';
|
package/lib/countries-index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
//# sourceMappingURL=countries-index.js.map
|
|
1
|
+
export * from "./module/configureCountriesModule.js";
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { mongodb } from '@unchainedshop/mongodb';
|
|
2
|
-
import { Country } from '../countries-index.
|
|
2
|
+
import type { Country } from '../countries-index.ts';
|
|
3
3
|
export declare const CountriesCollection: (db: mongodb.Db) => Promise<mongodb.Collection<Country>>;
|
|
4
|
-
//# sourceMappingURL=CountriesCollection.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildDbIndexes, isDocumentDBCompatModeEnabled } from '@unchainedshop/mongodb';
|
|
1
|
+
import { mongodb, buildDbIndexes, isDocumentDBCompatModeEnabled } from '@unchainedshop/mongodb';
|
|
2
2
|
export const CountriesCollection = async (db) => {
|
|
3
3
|
const Countries = db.collection('countries');
|
|
4
4
|
if (!isDocumentDBCompatModeEnabled()) {
|
|
@@ -25,4 +25,3 @@ export const CountriesCollection = async (db) => {
|
|
|
25
25
|
]);
|
|
26
26
|
return Countries;
|
|
27
27
|
};
|
|
28
|
-
//# sourceMappingURL=CountriesCollection.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { mongodb, TimestampFields, ModuleInput } from '@unchainedshop/mongodb';
|
|
2
|
-
import { SortOption } from '@unchainedshop/utils';
|
|
1
|
+
import { mongodb, type TimestampFields, type ModuleInput } from '@unchainedshop/mongodb';
|
|
2
|
+
import { type SortOption } from '@unchainedshop/utils';
|
|
3
3
|
export type Country = {
|
|
4
4
|
_id: string;
|
|
5
5
|
isoCode: string;
|
|
@@ -34,4 +34,3 @@ export declare const configureCountriesModule: ({ db }: ModuleInput<Record<strin
|
|
|
34
34
|
delete: (countryId: string) => Promise<number>;
|
|
35
35
|
}>;
|
|
36
36
|
export type CountriesModule = Awaited<ReturnType<typeof configureCountriesModule>>;
|
|
37
|
-
//# sourceMappingURL=configureCountriesModule.d.ts.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { assertDocumentDBCompatMode, } from '@unchainedshop/mongodb';
|
|
1
|
+
import { mongodb, assertDocumentDBCompatMode, } from '@unchainedshop/mongodb';
|
|
2
2
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
3
3
|
import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
4
4
|
import { SortDirection } from '@unchainedshop/utils';
|
|
5
5
|
import { systemLocale } from '@unchainedshop/utils';
|
|
6
|
-
import { CountriesCollection } from
|
|
6
|
+
import { CountriesCollection } from "../db/CountriesCollection.js";
|
|
7
7
|
const COUNTRY_EVENTS = ['COUNTRY_CREATE', 'COUNTRY_UPDATE', 'COUNTRY_REMOVE'];
|
|
8
8
|
export const buildFindSelector = ({ includeInactive = false, queryString = '', ...rest }) => {
|
|
9
9
|
const selector = { ...rest, deleted: null };
|
|
@@ -92,4 +92,3 @@ export const configureCountriesModule = async ({ db }) => {
|
|
|
92
92
|
},
|
|
93
93
|
};
|
|
94
94
|
};
|
|
95
|
-
//# sourceMappingURL=configureCountriesModule.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-countries",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"main": "lib/countries-index.js",
|
|
5
5
|
"types": "lib/countries-index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"build": "tsc -b",
|
|
10
10
|
"prepublishOnly": "npm run clean && npm run build",
|
|
11
11
|
"watch": "tsc -w",
|
|
12
|
-
"test": "
|
|
13
|
-
"test:watch": "
|
|
12
|
+
"test": "node --test",
|
|
13
|
+
"test:watch": "node --test --watch"
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
@@ -22,8 +22,10 @@
|
|
|
22
22
|
"core"
|
|
23
23
|
],
|
|
24
24
|
"authors": [
|
|
25
|
-
"
|
|
26
|
-
"
|
|
25
|
+
"Pascal Kaufmann",
|
|
26
|
+
"Mikael Araya",
|
|
27
|
+
"Vedran Rudelj",
|
|
28
|
+
"Joël Meiller"
|
|
27
29
|
],
|
|
28
30
|
"license": "EUPL-1.2",
|
|
29
31
|
"bugs": {
|
|
@@ -31,12 +33,11 @@
|
|
|
31
33
|
},
|
|
32
34
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
35
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/events": "^4.
|
|
35
|
-
"@unchainedshop/utils": "^4.
|
|
36
|
+
"@unchainedshop/events": "^4.4.0",
|
|
37
|
+
"@unchainedshop/utils": "^4.4.0"
|
|
36
38
|
},
|
|
37
39
|
"devDependencies": {
|
|
38
|
-
"@types/node": "^
|
|
39
|
-
"tsx": "^4.20.3",
|
|
40
|
+
"@types/node": "^25.0.0",
|
|
40
41
|
"typescript": "^5.8.3"
|
|
41
42
|
}
|
|
42
43
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"countries-index.d.ts","sourceRoot":"","sources":["../src/countries-index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"countries-index.js","sourceRoot":"","sources":["../src/countries-index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CountriesCollection.d.ts","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAiD,MAAM,wBAAwB,CAAC;AAChG,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAEhD,eAAO,MAAM,mBAAmB,GAAU,IAAI,OAAO,CAAC,EAAE,yCA4BvD,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CountriesCollection.js","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,cAAc,EAAE,6BAA6B,EAAE,MAAM,wBAAwB,CAAC;AAGhG,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EAAE,EAAc,EAAE,EAAE;IAC1D,MAAM,SAAS,GAAG,EAAE,CAAC,UAAU,CAAU,WAAW,CAAC,CAAC;IAEtD,IAAI,CAAC,6BAA6B,EAAE,EAAE,CAAC;QACrC,MAAM,cAAc,CAAU,SAAS,EAAE;YACvC;gBACE,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE;gBACvC,OAAO,EAAE;oBACP,OAAO,EAAE;wBACP,GAAG,EAAE,CAAC;wBACN,OAAO,EAAE,CAAC;qBACX;oBACD,IAAI,EAAE,2BAA2B;iBAClC;aACF;SACF,CAAC,CAAC;IACL,CAAC;IAED,MAAM,cAAc,CAAU,SAAS,EAAE;QACvC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;QACpD;YACE,KAAK,EAAE;gBACL,OAAO,EAAE,CAAC;aACX;SACF;KACF,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CountriesSchema.d.ts","sourceRoot":"","sources":["../../src/db/CountriesSchema.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,eAAO,MAAM,eAAe,cAQ3B,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Schemas } from '@unchainedshop/utils';
|
|
2
|
-
import SimpleSchema from 'simpl-schema';
|
|
3
|
-
export const CountriesSchema = new SimpleSchema({
|
|
4
|
-
isoCode: { type: String, required: true },
|
|
5
|
-
isActive: Boolean,
|
|
6
|
-
defaultCurrencyCode: String,
|
|
7
|
-
...Schemas.timestampFields,
|
|
8
|
-
}, { requiredByDefault: false });
|
|
9
|
-
//# sourceMappingURL=CountriesSchema.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CountriesSchema.js","sourceRoot":"","sources":["../../src/db/CountriesSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,YAAY,CAC7C;IACE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACzC,QAAQ,EAAE,OAAO;IACjB,mBAAmB,EAAE,MAAM;IAC3B,GAAG,OAAO,CAAC,eAAe;CAC3B,EACD,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAC7B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"addMigrations.d.ts","sourceRoot":"","sources":["../../src/migrations/addMigrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAI9E,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,UAAU,EAAE,mBAAmB,CAAC,SAAS,CAAC,QAmC/E"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
2
|
-
export default function addMigrations(repository) {
|
|
3
|
-
repository?.register({
|
|
4
|
-
id: 20240712123500,
|
|
5
|
-
name: 'Convert all tags to lower case to make it easy for search',
|
|
6
|
-
up: async () => {
|
|
7
|
-
const Currencies = await repository.db.collection('currencies');
|
|
8
|
-
const Countries = await CountriesCollection(repository.db);
|
|
9
|
-
const allCurrencies = await Currencies.find({}).toArray();
|
|
10
|
-
const allCountries = await Countries.find({}).toArray();
|
|
11
|
-
const currencyMap = allCurrencies.reduce((acc, currency) => {
|
|
12
|
-
acc[currency._id] = currency.isoCode;
|
|
13
|
-
return acc;
|
|
14
|
-
}, {});
|
|
15
|
-
for (const country of allCountries) {
|
|
16
|
-
await Countries.updateOne({ _id: country._id, defaultCurrencyId: { $exists: true } }, {
|
|
17
|
-
$set: {
|
|
18
|
-
defaultCurrencyCode: currencyMap[country.defaultCurrencyId],
|
|
19
|
-
},
|
|
20
|
-
$unset: {
|
|
21
|
-
defaultCurrencyId: 1,
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=addMigrations.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"addMigrations.js","sourceRoot":"","sources":["../../src/migrations/addMigrations.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;A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,OAAO,CAAC,WAAW,EAAE;gBAClC,QAAQ,EAAE,IAAI;aACf,CAAC,CAAC;YACH,MAAM,IAAI,CAAC,gBAAgB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;YAC5C,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,EAAE,KAAK,EAAE,SAAiB,EAAE,GAAY,EAAE,EAAE;YAChD,MAAM,SAAS,CAAC,SAAS,CAAC,oBAAoB,CAAC,SAAS,CAAC,EAAE;gBACzD,IAAI,EAAE;oBACJ,OAAO,EAAE,IAAI,IAAI,EAAE;oBACnB,GAAG,GAAG;iBACP;aACF,CAAC,CAAC;YACH,MAAM,IAAI,CAAC,gBAAgB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;YAC5C,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,EAAE,KAAK,EAAE,SAAiB,EAAE,EAAE;YAClC,MAAM,EAAE,aAAa,EAAE,YAAY,EAAE,GAAG,MAAM,SAAS,CAAC,SAAS,CAC/D,oBAAoB,CAAC,SAAS,CAAC,EAC/B;gBACE,IAAI,EAAE;oBACJ,OAAO,EAAE,IAAI,IAAI,EAAE;iBACpB;aACF,CACF,CAAC;YACF,MAAM,IAAI,CAAC,gBAAgB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;YAC5C,OAAO,YAAY,CAAC;QACtB,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/src/countries-index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './module/configureCountriesModule.js';
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { mongodb, buildDbIndexes, isDocumentDBCompatModeEnabled } from '@unchainedshop/mongodb';
|
|
2
|
-
import { Country } from '../countries-index.js';
|
|
3
|
-
|
|
4
|
-
export const CountriesCollection = async (db: mongodb.Db) => {
|
|
5
|
-
const Countries = db.collection<Country>('countries');
|
|
6
|
-
|
|
7
|
-
if (!isDocumentDBCompatModeEnabled()) {
|
|
8
|
-
await buildDbIndexes<Country>(Countries, [
|
|
9
|
-
{
|
|
10
|
-
index: { isoCode: 'text', _id: 'text' },
|
|
11
|
-
options: {
|
|
12
|
-
weights: {
|
|
13
|
-
_id: 8,
|
|
14
|
-
isoCode: 6,
|
|
15
|
-
},
|
|
16
|
-
name: 'countries_fulltext_search',
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
]);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
await buildDbIndexes<Country>(Countries, [
|
|
23
|
-
{ index: { isoCode: 1 }, options: { unique: true } },
|
|
24
|
-
{
|
|
25
|
-
index: {
|
|
26
|
-
deleted: 1,
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
]);
|
|
30
|
-
|
|
31
|
-
return Countries;
|
|
32
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { describe, it } from 'node:test';
|
|
2
|
-
import { buildFindSelector } from './configureCountriesModule.js';
|
|
3
|
-
import assert from 'node:assert';
|
|
4
|
-
|
|
5
|
-
describe('buildFindSelector', () => {
|
|
6
|
-
it('should return correct filter object', () => {
|
|
7
|
-
assert.deepStrictEqual(buildFindSelector({ includeInactive: true, queryString: 'hello world' }), {
|
|
8
|
-
deleted: null,
|
|
9
|
-
$text: { $search: 'hello world' },
|
|
10
|
-
});
|
|
11
|
-
assert.deepStrictEqual(buildFindSelector({ includeInactive: true }), { deleted: null });
|
|
12
|
-
assert.deepStrictEqual(buildFindSelector({ queryString: 'hello world' }), {
|
|
13
|
-
deleted: null,
|
|
14
|
-
$text: { $search: 'hello world' },
|
|
15
|
-
isActive: true,
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
});
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
mongodb,
|
|
3
|
-
TimestampFields,
|
|
4
|
-
ModuleInput,
|
|
5
|
-
assertDocumentDBCompatMode,
|
|
6
|
-
} from '@unchainedshop/mongodb';
|
|
7
|
-
import { emit, registerEvents } from '@unchainedshop/events';
|
|
8
|
-
import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
9
|
-
import { SortDirection, SortOption } from '@unchainedshop/utils';
|
|
10
|
-
import { systemLocale } from '@unchainedshop/utils';
|
|
11
|
-
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
12
|
-
|
|
13
|
-
export type Country = {
|
|
14
|
-
_id: string;
|
|
15
|
-
isoCode: string;
|
|
16
|
-
isActive?: boolean;
|
|
17
|
-
defaultCurrencyCode?: string;
|
|
18
|
-
} & TimestampFields;
|
|
19
|
-
|
|
20
|
-
export type CountryQuery = mongodb.Filter<Country> & {
|
|
21
|
-
includeInactive?: boolean;
|
|
22
|
-
queryString?: string;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const COUNTRY_EVENTS: string[] = ['COUNTRY_CREATE', 'COUNTRY_UPDATE', 'COUNTRY_REMOVE'];
|
|
26
|
-
|
|
27
|
-
export const buildFindSelector = ({
|
|
28
|
-
includeInactive = false,
|
|
29
|
-
queryString = '',
|
|
30
|
-
...rest
|
|
31
|
-
}: CountryQuery) => {
|
|
32
|
-
const selector: mongodb.Filter<Country> = { ...rest, deleted: null };
|
|
33
|
-
if (!includeInactive) selector.isActive = true;
|
|
34
|
-
if (queryString) {
|
|
35
|
-
assertDocumentDBCompatMode();
|
|
36
|
-
selector.$text = { $search: queryString };
|
|
37
|
-
}
|
|
38
|
-
return selector;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export const configureCountriesModule = async ({ db }: ModuleInput<Record<string, never>>) => {
|
|
42
|
-
registerEvents(COUNTRY_EVENTS);
|
|
43
|
-
|
|
44
|
-
const Countries = await CountriesCollection(db);
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
count: async (query: CountryQuery): Promise<number> => {
|
|
48
|
-
const countryCount = await Countries.countDocuments(buildFindSelector(query));
|
|
49
|
-
return countryCount;
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
findCountry: async (params: { countryId: string } | { isoCode: string }) => {
|
|
53
|
-
if ('countryId' in params) {
|
|
54
|
-
return Countries.findOne(generateDbFilterById(params.countryId));
|
|
55
|
-
} else {
|
|
56
|
-
return Countries.findOne({ isoCode: params.isoCode });
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
findCountries: async (
|
|
61
|
-
{
|
|
62
|
-
limit,
|
|
63
|
-
offset,
|
|
64
|
-
sort,
|
|
65
|
-
...query
|
|
66
|
-
}: CountryQuery & {
|
|
67
|
-
limit?: number;
|
|
68
|
-
offset?: number;
|
|
69
|
-
sort?: SortOption[];
|
|
70
|
-
},
|
|
71
|
-
options?: mongodb.FindOptions,
|
|
72
|
-
): Promise<Country[]> => {
|
|
73
|
-
const defaultSort = [{ key: 'created', value: SortDirection.ASC }] as SortOption[];
|
|
74
|
-
const countries = Countries.find(buildFindSelector(query), {
|
|
75
|
-
skip: offset,
|
|
76
|
-
limit,
|
|
77
|
-
sort: buildSortOptions(sort || defaultSort),
|
|
78
|
-
...options,
|
|
79
|
-
});
|
|
80
|
-
return countries.toArray();
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
countryExists: async ({ countryId }: { countryId: string }): Promise<boolean> => {
|
|
84
|
-
const countryCount = await Countries.countDocuments(
|
|
85
|
-
generateDbFilterById(countryId, { deleted: null }),
|
|
86
|
-
{
|
|
87
|
-
limit: 1,
|
|
88
|
-
},
|
|
89
|
-
);
|
|
90
|
-
return !!countryCount;
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
name(country: Country, locale: Intl.Locale) {
|
|
94
|
-
return new Intl.DisplayNames([locale], { type: 'region', fallback: 'code' }).of(country.isoCode);
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
flagEmoji(country: Country) {
|
|
98
|
-
const letterToLetterEmoji = (letter: string): string => {
|
|
99
|
-
return String.fromCodePoint(letter.toLowerCase().charCodeAt(0) + 127365);
|
|
100
|
-
};
|
|
101
|
-
return Array.from(country.isoCode.toUpperCase()).map(letterToLetterEmoji).join('');
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
isBase(country: Country) {
|
|
105
|
-
return country.isoCode === systemLocale.region;
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
create: async (
|
|
109
|
-
doc: Omit<Country, '_id' | 'created'> & Pick<Partial<Country>, '_id' | 'created'>,
|
|
110
|
-
) => {
|
|
111
|
-
await Countries.deleteOne({ isoCode: doc.isoCode.toUpperCase(), deleted: { $ne: null } });
|
|
112
|
-
const { insertedId: countryId } = await Countries.insertOne({
|
|
113
|
-
_id: generateDbObjectId(),
|
|
114
|
-
created: new Date(),
|
|
115
|
-
...doc,
|
|
116
|
-
isoCode: doc.isoCode.toUpperCase(),
|
|
117
|
-
isActive: true,
|
|
118
|
-
});
|
|
119
|
-
await emit('COUNTRY_CREATE', { countryId });
|
|
120
|
-
return countryId;
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
update: async (countryId: string, doc: Country) => {
|
|
124
|
-
await Countries.updateOne(generateDbFilterById(countryId), {
|
|
125
|
-
$set: {
|
|
126
|
-
updated: new Date(),
|
|
127
|
-
...doc,
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
await emit('COUNTRY_UPDATE', { countryId });
|
|
131
|
-
return countryId;
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
delete: async (countryId: string) => {
|
|
135
|
-
const { modifiedCount: deletedCount } = await Countries.updateOne(
|
|
136
|
-
generateDbFilterById(countryId),
|
|
137
|
-
{
|
|
138
|
-
$set: {
|
|
139
|
-
deleted: new Date(),
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
);
|
|
143
|
-
await emit('COUNTRY_REMOVE', { countryId });
|
|
144
|
-
return deletedCount;
|
|
145
|
-
},
|
|
146
|
-
};
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
export type CountriesModule = Awaited<ReturnType<typeof configureCountriesModule>>;
|