@unchainedshop/core-countries 3.0.0-alpha2 → 3.0.0-alpha4
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/lib/countries-index.d.ts +1 -1
- package/lib/countries-index.d.ts.map +1 -1
- package/lib/countries-index.js +1 -1
- package/lib/countries-index.js.map +1 -1
- package/lib/db/CountriesCollection.d.ts +1 -1
- package/lib/db/CountriesCollection.d.ts.map +1 -1
- package/lib/migrations/addMigrations.d.ts +2 -2
- package/lib/migrations/addMigrations.d.ts.map +1 -1
- package/lib/migrations/addMigrations.js.map +1 -1
- package/lib/module/configureCountriesModule.d.ts +33 -2
- package/lib/module/configureCountriesModule.d.ts.map +1 -1
- package/lib/module/configureCountriesModule.js +18 -10
- package/lib/module/configureCountriesModule.js.map +1 -1
- package/package.json +6 -8
- package/src/countries-index.ts +1 -1
- package/src/db/CountriesCollection.ts +1 -1
- package/src/migrations/addMigrations.ts +3 -3
- package/src/module/buildFindSelector.test.ts +12 -6
- package/src/module/configureCountriesModule.ts +60 -15
- 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/src/db/CountriesSchema.ts +0 -12
package/lib/countries-index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './module/configureCountriesModule.js';
|
|
2
2
|
//# sourceMappingURL=countries-index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"countries-index.d.ts","sourceRoot":"","sources":["../src/countries-index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"countries-index.d.ts","sourceRoot":"","sources":["../src/countries-index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC"}
|
package/lib/countries-index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './module/configureCountriesModule.js';
|
|
2
2
|
//# sourceMappingURL=countries-index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"countries-index.js","sourceRoot":"","sources":["../src/countries-index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"countries-index.js","sourceRoot":"","sources":["../src/countries-index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { mongodb } from '@unchainedshop/mongodb';
|
|
2
|
-
import { Country } from '
|
|
2
|
+
import { Country } from '../countries-index.js';
|
|
3
3
|
export declare const CountriesCollection: (db: mongodb.Db) => Promise<mongodb.Collection<Country>>;
|
|
4
4
|
//# sourceMappingURL=CountriesCollection.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CountriesCollection.d.ts","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAkB,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"CountriesCollection.d.ts","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAkB,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAEhD,eAAO,MAAM,mBAAmB,OAAc,OAAO,CAAC,EAAE,yCAuBvD,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export default function addMigrations(repository: MigrationRepository
|
|
1
|
+
import { MigrationRepository } from '@unchainedshop/mongodb';
|
|
2
|
+
export default function addMigrations(repository: MigrationRepository): void;
|
|
3
3
|
//# sourceMappingURL=addMigrations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addMigrations.d.ts","sourceRoot":"","sources":["../../src/migrations/addMigrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"addMigrations.d.ts","sourceRoot":"","sources":["../../src/migrations/addMigrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAI7D,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,UAAU,EAAE,mBAAmB,QAmCpE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addMigrations.js","sourceRoot":"","sources":["../../src/migrations/addMigrations.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAGnE,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,
|
|
1
|
+
{"version":3,"file":"addMigrations.js","sourceRoot":"","sources":["../../src/migrations/addMigrations.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAGnE,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,UAA+B;IACnE,UAAU,EAAE,QAAQ,CAAC;QACnB,EAAE,EAAE,cAAc;QAClB,IAAI,EAAE,2DAA2D;QACjE,EAAE,EAAE,KAAK,IAAI,EAAE;YACb,MAAM,UAAU,GAAG,MAAM,UAAU,CAAC,EAAE,CAAC,UAAU,CAAmC,YAAY,CAAC,CAAC;YAClG,MAAM,SAAS,GAAG,MAAM,mBAAmB,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;YAE3D,MAAM,aAAa,GAAG,MAAM,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;YAC1D,MAAM,YAAY,GAAG,MAAM,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;YAExD,MAAM,WAAW,GAAG,aAAa,CAAC,MAAM,CACtC,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE;gBAChB,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC;gBACrC,OAAO,GAAG,CAAC;YACb,CAAC,EACD,EAAyB,CAC1B,CAAC;YAEF,KAAK,MAAM,OAAO,IAAI,YAAY,EAAE,CAAC;gBACnC,MAAM,SAAS,CAAC,SAAS,CACvB,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,iBAAiB,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAC1D;oBACE,IAAI,EAAE;wBACJ,mBAAmB,EACjB,WAAW,CAAE,OAAmD,CAAC,iBAAiB,CAAC;qBACtF;oBACD,MAAM,EAAE;wBACN,iBAAiB,EAAE,CAAC;qBACrB;iBACF,CACF,CAAC;YACJ,CAAC;QACH,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -1,5 +1,36 @@
|
|
|
1
|
-
import { ModuleInput } from '@unchainedshop/
|
|
2
|
-
import {
|
|
1
|
+
import type { mongodb, TimestampFields, ModuleInput } from '@unchainedshop/mongodb';
|
|
2
|
+
import { SortOption } from '@unchainedshop/utils';
|
|
3
|
+
export type Country = {
|
|
4
|
+
_id?: string;
|
|
5
|
+
isoCode: string;
|
|
6
|
+
isActive?: boolean;
|
|
7
|
+
defaultCurrencyCode?: string;
|
|
8
|
+
} & TimestampFields;
|
|
9
|
+
export type CountryQuery = {
|
|
10
|
+
includeInactive?: boolean;
|
|
11
|
+
queryString?: string;
|
|
12
|
+
};
|
|
13
|
+
export type CountriesModule = {
|
|
14
|
+
findCountry: (params: {
|
|
15
|
+
countryId?: string;
|
|
16
|
+
isoCode?: string;
|
|
17
|
+
}) => Promise<Country>;
|
|
18
|
+
findCountries: (params: CountryQuery & {
|
|
19
|
+
limit?: number;
|
|
20
|
+
offset?: number;
|
|
21
|
+
sort?: Array<SortOption>;
|
|
22
|
+
}, options?: mongodb.FindOptions) => Promise<Array<Country>>;
|
|
23
|
+
count: (query: CountryQuery) => Promise<number>;
|
|
24
|
+
countryExists: (params: {
|
|
25
|
+
countryId: string;
|
|
26
|
+
}) => Promise<boolean>;
|
|
27
|
+
flagEmoji: (country: Country) => string;
|
|
28
|
+
isBase: (country: Country) => boolean;
|
|
29
|
+
name: (country: Country, language: string) => string;
|
|
30
|
+
update: (_id: string, doc: Country) => Promise<string>;
|
|
31
|
+
delete: (_id: string) => Promise<number>;
|
|
32
|
+
create: (doc: Country) => Promise<string | null>;
|
|
33
|
+
};
|
|
3
34
|
export declare const buildFindSelector: ({ includeInactive, queryString }: CountryQuery) => {
|
|
4
35
|
isActive?: true;
|
|
5
36
|
$text?: any;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureCountriesModule.d.ts","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"configureCountriesModule.d.ts","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGpF,OAAO,EAAiB,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAKjE,MAAM,MAAM,OAAO,GAAG;IACpB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,GAAG,eAAe,CAAC;AAEpB,MAAM,MAAM,YAAY,GAAG;IACzB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AACF,MAAM,MAAM,eAAe,GAAG;IAC5B,WAAW,EAAE,CAAC,MAAM,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,KAAK,OAAO,CAAC,OAAO,CAA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ents } from '@unchainedshop/events';
|
|
2
|
-
import { generateDbFilterById,
|
|
3
|
-
import { SortDirection } from '@unchainedshop/
|
|
2
|
+
import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
3
|
+
import { SortDirection } from '@unchainedshop/utils';
|
|
4
4
|
import { systemLocale } from '@unchainedshop/utils';
|
|
5
5
|
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
6
|
-
import { CountriesSchema } from '../db/CountriesSchema.js';
|
|
7
6
|
import addMigrations from '../migrations/addMigrations.js';
|
|
8
7
|
const COUNTRY_EVENTS = ['COUNTRY_CREATE', 'COUNTRY_UPDATE', 'COUNTRY_REMOVE'];
|
|
9
8
|
export const buildFindSelector = ({ includeInactive = false, queryString = '' }) => {
|
|
@@ -19,7 +18,6 @@ export const configureCountriesModule = async ({ db, migrationRepository, }) =>
|
|
|
19
18
|
// Migration
|
|
20
19
|
addMigrations(migrationRepository);
|
|
21
20
|
const Countries = await CountriesCollection(db);
|
|
22
|
-
const mutations = generateDbMutations(Countries, CountriesSchema);
|
|
23
21
|
return {
|
|
24
22
|
count: async (query) => {
|
|
25
23
|
const countryCount = await Countries.countDocuments(buildFindSelector(query));
|
|
@@ -54,11 +52,13 @@ export const configureCountriesModule = async ({ db, migrationRepository, }) =>
|
|
|
54
52
|
return Array.from(country.isoCode.toUpperCase()).map(letterToLetterEmoji).join('');
|
|
55
53
|
},
|
|
56
54
|
isBase(country) {
|
|
57
|
-
return country.isoCode === systemLocale.
|
|
55
|
+
return country.isoCode === systemLocale.region;
|
|
58
56
|
},
|
|
59
57
|
create: async (doc) => {
|
|
60
58
|
await Countries.deleteOne({ isoCode: doc.isoCode.toUpperCase(), deleted: { $ne: null } });
|
|
61
|
-
const countryId = await
|
|
59
|
+
const { insertedId: countryId } = await Countries.insertOne({
|
|
60
|
+
_id: generateDbObjectId(),
|
|
61
|
+
created: new Date(),
|
|
62
62
|
...doc,
|
|
63
63
|
isoCode: doc.isoCode.toUpperCase(),
|
|
64
64
|
isActive: true,
|
|
@@ -66,17 +66,25 @@ export const configureCountriesModule = async ({ db, migrationRepository, }) =>
|
|
|
66
66
|
await emit('COUNTRY_CREATE', { countryId });
|
|
67
67
|
return countryId;
|
|
68
68
|
},
|
|
69
|
-
update: async (
|
|
70
|
-
|
|
69
|
+
update: async (countryId, doc) => {
|
|
70
|
+
await Countries.updateOne(generateDbFilterById(countryId), {
|
|
71
|
+
$set: {
|
|
72
|
+
updated: new Date(),
|
|
73
|
+
...doc,
|
|
74
|
+
},
|
|
75
|
+
});
|
|
71
76
|
await emit('COUNTRY_UPDATE', { countryId });
|
|
72
77
|
return countryId;
|
|
73
78
|
},
|
|
74
79
|
delete: async (countryId) => {
|
|
75
|
-
const deletedCount = await
|
|
80
|
+
const { modifiedCount: deletedCount } = await Countries.updateOne(generateDbFilterById(countryId), {
|
|
81
|
+
$set: {
|
|
82
|
+
deleted: new Date(),
|
|
83
|
+
},
|
|
84
|
+
});
|
|
76
85
|
await emit('COUNTRY_REMOVE', { countryId });
|
|
77
86
|
return deletedCount;
|
|
78
87
|
},
|
|
79
|
-
deletePermanently: mutations.deletePermanently,
|
|
80
88
|
};
|
|
81
89
|
};
|
|
82
90
|
//# sourceMappingURL=configureCountriesModule.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureCountriesModule.js","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"configureCountriesModule.js","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AACpG,OAAO,EAAE,aAAa,EAAc,MAAM,sBAAsB,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC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,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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-countries",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-alpha4",
|
|
4
4
|
"main": "lib/countries-index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/countries-index.js",
|
|
@@ -31,16 +31,14 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/events": "^3.0.0-
|
|
35
|
-
"@unchainedshop/utils": "^3.0.0-
|
|
36
|
-
"simpl-schema": "^3.4.6"
|
|
34
|
+
"@unchainedshop/events": "^3.0.0-alpha4",
|
|
35
|
+
"@unchainedshop/utils": "^3.0.0-alpha4"
|
|
37
36
|
},
|
|
38
37
|
"devDependencies": {
|
|
39
|
-
"@types/node": "^
|
|
40
|
-
"@unchainedshop/types": "^3.0.0-alpha2",
|
|
38
|
+
"@types/node": "^22.7.8",
|
|
41
39
|
"cross-env": "^7.0.3",
|
|
42
40
|
"jest": "^29.7.0",
|
|
43
|
-
"ts-jest": "^29.2.
|
|
44
|
-
"typescript": "^5.
|
|
41
|
+
"ts-jest": "^29.2.5",
|
|
42
|
+
"typescript": "^5.6.3"
|
|
45
43
|
}
|
|
46
44
|
}
|
package/src/countries-index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './module/configureCountriesModule.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { mongodb, buildDbIndexes } from '@unchainedshop/mongodb';
|
|
2
|
-
import { Country } from '
|
|
2
|
+
import { Country } from '../countries-index.js';
|
|
3
3
|
|
|
4
4
|
export const CountriesCollection = async (db: mongodb.Db) => {
|
|
5
5
|
const Countries = db.collection<Country>('countries');
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MigrationRepository } from '@unchainedshop/mongodb';
|
|
2
2
|
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
3
|
-
import { Country } from '
|
|
3
|
+
import { Country } from '../countries-index.js';
|
|
4
4
|
|
|
5
|
-
export default function addMigrations(repository: MigrationRepository
|
|
5
|
+
export default function addMigrations(repository: MigrationRepository) {
|
|
6
6
|
repository?.register({
|
|
7
7
|
id: 20240712123500,
|
|
8
8
|
name: 'Convert all tags to lower case to make it easy for search',
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { buildFindSelector } from
|
|
1
|
+
import { buildFindSelector } from './configureCountriesModule.js';
|
|
2
2
|
|
|
3
3
|
describe('buildFindSelector', () => {
|
|
4
|
-
it('should return correct filter object',
|
|
5
|
-
expect(buildFindSelector({includeInactive: true, queryString: 'hello world'})).toEqual({
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
it('should return correct filter object', () => {
|
|
5
|
+
expect(buildFindSelector({ includeInactive: true, queryString: 'hello world' })).toEqual({
|
|
6
|
+
deleted: null,
|
|
7
|
+
$text: { $search: 'hello world' },
|
|
8
|
+
});
|
|
9
|
+
expect(buildFindSelector({ includeInactive: true })).toEqual({ deleted: null });
|
|
10
|
+
expect(buildFindSelector({ queryString: 'hello world' })).toEqual({
|
|
11
|
+
deleted: null,
|
|
12
|
+
$text: { $search: 'hello world' },
|
|
13
|
+
isActive: true,
|
|
14
|
+
});
|
|
8
15
|
});
|
|
9
|
-
|
|
10
16
|
});
|
|
@@ -1,13 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CountriesModule, Country, CountryQuery } from '@unchainedshop/types/countries.js';
|
|
1
|
+
import type { mongodb, TimestampFields, ModuleInput } from '@unchainedshop/mongodb';
|
|
3
2
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
4
|
-
import { generateDbFilterById,
|
|
5
|
-
import { SortDirection, SortOption } from '@unchainedshop/
|
|
3
|
+
import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
4
|
+
import { SortDirection, SortOption } from '@unchainedshop/utils';
|
|
6
5
|
import { systemLocale } from '@unchainedshop/utils';
|
|
7
6
|
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
8
|
-
import { CountriesSchema } from '../db/CountriesSchema.js';
|
|
9
7
|
import addMigrations from '../migrations/addMigrations.js';
|
|
10
8
|
|
|
9
|
+
export type Country = {
|
|
10
|
+
_id?: string;
|
|
11
|
+
isoCode: string;
|
|
12
|
+
isActive?: boolean;
|
|
13
|
+
defaultCurrencyCode?: string;
|
|
14
|
+
} & TimestampFields;
|
|
15
|
+
|
|
16
|
+
export type CountryQuery = {
|
|
17
|
+
includeInactive?: boolean;
|
|
18
|
+
queryString?: string;
|
|
19
|
+
};
|
|
20
|
+
export type CountriesModule = {
|
|
21
|
+
findCountry: (params: { countryId?: string; isoCode?: string }) => Promise<Country>;
|
|
22
|
+
findCountries: (
|
|
23
|
+
params: CountryQuery & {
|
|
24
|
+
limit?: number;
|
|
25
|
+
offset?: number;
|
|
26
|
+
sort?: Array<SortOption>;
|
|
27
|
+
},
|
|
28
|
+
options?: mongodb.FindOptions,
|
|
29
|
+
) => Promise<Array<Country>>;
|
|
30
|
+
count: (query: CountryQuery) => Promise<number>;
|
|
31
|
+
countryExists: (params: { countryId: string }) => Promise<boolean>;
|
|
32
|
+
|
|
33
|
+
flagEmoji: (country: Country) => string;
|
|
34
|
+
isBase: (country: Country) => boolean;
|
|
35
|
+
name: (country: Country, language: string) => string;
|
|
36
|
+
|
|
37
|
+
update: (_id: string, doc: Country) => Promise<string>;
|
|
38
|
+
delete: (_id: string) => Promise<number>;
|
|
39
|
+
create: (doc: Country) => Promise<string | null>;
|
|
40
|
+
};
|
|
41
|
+
|
|
11
42
|
const COUNTRY_EVENTS: string[] = ['COUNTRY_CREATE', 'COUNTRY_UPDATE', 'COUNTRY_REMOVE'];
|
|
12
43
|
|
|
13
44
|
export const buildFindSelector = ({ includeInactive = false, queryString = '' }: CountryQuery) => {
|
|
@@ -28,8 +59,6 @@ export const configureCountriesModule = async ({
|
|
|
28
59
|
|
|
29
60
|
const Countries = await CountriesCollection(db);
|
|
30
61
|
|
|
31
|
-
const mutations = generateDbMutations<Country>(Countries, CountriesSchema) as ModuleMutations<Country>;
|
|
32
|
-
|
|
33
62
|
return {
|
|
34
63
|
count: async (query) => {
|
|
35
64
|
const countryCount = await Countries.countDocuments(buildFindSelector(query));
|
|
@@ -64,6 +93,7 @@ export const configureCountriesModule = async ({
|
|
|
64
93
|
name(country, language) {
|
|
65
94
|
return new Intl.DisplayNames([language], { type: 'region', fallback: 'code' }).of(country.isoCode);
|
|
66
95
|
},
|
|
96
|
+
|
|
67
97
|
flagEmoji(country) {
|
|
68
98
|
const letterToLetterEmoji = (letter: string): string => {
|
|
69
99
|
return String.fromCodePoint(letter.toLowerCase().charCodeAt(0) + 127365);
|
|
@@ -72,11 +102,14 @@ export const configureCountriesModule = async ({
|
|
|
72
102
|
},
|
|
73
103
|
|
|
74
104
|
isBase(country) {
|
|
75
|
-
return country.isoCode === systemLocale.
|
|
105
|
+
return country.isoCode === systemLocale.region;
|
|
76
106
|
},
|
|
77
|
-
|
|
107
|
+
|
|
108
|
+
create: async (doc) => {
|
|
78
109
|
await Countries.deleteOne({ isoCode: doc.isoCode.toUpperCase(), deleted: { $ne: null } });
|
|
79
|
-
const countryId = await
|
|
110
|
+
const { insertedId: countryId } = await Countries.insertOne({
|
|
111
|
+
_id: generateDbObjectId(),
|
|
112
|
+
created: new Date(),
|
|
80
113
|
...doc,
|
|
81
114
|
isoCode: doc.isoCode.toUpperCase(),
|
|
82
115
|
isActive: true,
|
|
@@ -84,17 +117,29 @@ export const configureCountriesModule = async ({
|
|
|
84
117
|
await emit('COUNTRY_CREATE', { countryId });
|
|
85
118
|
return countryId;
|
|
86
119
|
},
|
|
87
|
-
|
|
88
|
-
|
|
120
|
+
|
|
121
|
+
update: async (countryId, doc) => {
|
|
122
|
+
await Countries.updateOne(generateDbFilterById(countryId), {
|
|
123
|
+
$set: {
|
|
124
|
+
updated: new Date(),
|
|
125
|
+
...doc,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
89
128
|
await emit('COUNTRY_UPDATE', { countryId });
|
|
90
129
|
return countryId;
|
|
91
130
|
},
|
|
131
|
+
|
|
92
132
|
delete: async (countryId) => {
|
|
93
|
-
const deletedCount = await
|
|
133
|
+
const { modifiedCount: deletedCount } = await Countries.updateOne(
|
|
134
|
+
generateDbFilterById(countryId),
|
|
135
|
+
{
|
|
136
|
+
$set: {
|
|
137
|
+
deleted: new Date(),
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
);
|
|
94
141
|
await emit('COUNTRY_REMOVE', { countryId });
|
|
95
142
|
return deletedCount;
|
|
96
143
|
},
|
|
97
|
-
|
|
98
|
-
deletePermanently: mutations.deletePermanently,
|
|
99
144
|
};
|
|
100
145
|
};
|
|
@@ -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,12 +0,0 @@
|
|
|
1
|
-
import { Schemas } from '@unchainedshop/utils';
|
|
2
|
-
import SimpleSchema from 'simpl-schema';
|
|
3
|
-
|
|
4
|
-
export const CountriesSchema = new SimpleSchema(
|
|
5
|
-
{
|
|
6
|
-
isoCode: { type: String, required: true },
|
|
7
|
-
isActive: Boolean,
|
|
8
|
-
defaultCurrencyCode: String,
|
|
9
|
-
...Schemas.timestampFields,
|
|
10
|
-
},
|
|
11
|
-
{ requiredByDefault: false },
|
|
12
|
-
);
|