@unchainedshop/core-countries 3.0.0-alpha2 → 3.0.0-alpha3

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.
@@ -1,2 +1,2 @@
1
- export { configureCountriesModule } from './module/configureCountriesModule.js';
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,OAAO,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC"}
1
+ {"version":3,"file":"countries-index.d.ts","sourceRoot":"","sources":["../src/countries-index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC"}
@@ -1,2 +1,2 @@
1
- export { configureCountriesModule } from './module/configureCountriesModule.js';
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,OAAO,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC"}
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 '@unchainedshop/types/countries.js';
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,mCAAmC,CAAC;AAE5D,eAAO,MAAM,mBAAmB,OAAc,OAAO,CAAC,EAAE,yCAuBvD,CAAC"}
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 { Migration, MigrationRepository } from '@unchainedshop/types/core.js';
1
+ import { Migration, MigrationRepository } from '@unchainedshop/core';
2
2
  export default function addMigrations(repository: MigrationRepository<Migration>): 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,SAAS,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAI9E,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,UAAU,EAAE,mBAAmB,CAAC,SAAS,CAAC,QAmC/E"}
1
+ {"version":3,"file":"addMigrations.d.ts","sourceRoot":"","sources":["../../src/migrations/addMigrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAIrE,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,UAAU,EAAE,mBAAmB,CAAC,SAAS,CAAC,QAmC/E"}
@@ -1,5 +1,37 @@
1
- import { ModuleInput } from '@unchainedshop/types/core.js';
2
- import { CountriesModule, CountryQuery } from '@unchainedshop/types/countries.js';
1
+ import type { mongodb, TimestampFields } from '@unchainedshop/mongodb';
2
+ import { ModuleInput } from '@unchainedshop/core';
3
+ import { SortOption } from '@unchainedshop/utils';
4
+ export type Country = {
5
+ _id?: string;
6
+ isoCode: string;
7
+ isActive?: boolean;
8
+ defaultCurrencyCode?: string;
9
+ } & TimestampFields;
10
+ export type CountryQuery = {
11
+ includeInactive?: boolean;
12
+ queryString?: string;
13
+ };
14
+ export type CountriesModule = {
15
+ findCountry: (params: {
16
+ countryId?: string;
17
+ isoCode?: string;
18
+ }) => Promise<Country>;
19
+ findCountries: (params: CountryQuery & {
20
+ limit?: number;
21
+ offset?: number;
22
+ sort?: Array<SortOption>;
23
+ }, options?: mongodb.FindOptions) => Promise<Array<Country>>;
24
+ count: (query: CountryQuery) => Promise<number>;
25
+ countryExists: (params: {
26
+ countryId: string;
27
+ }) => Promise<boolean>;
28
+ flagEmoji: (country: Country) => string;
29
+ isBase: (country: Country) => boolean;
30
+ name: (country: Country, language: string) => string;
31
+ update: (_id: string, doc: Country) => Promise<string>;
32
+ delete: (_id: string) => Promise<number>;
33
+ create: (doc: Country) => Promise<string | null>;
34
+ };
3
35
  export declare const buildFindSelector: ({ includeInactive, queryString }: CountryQuery) => {
4
36
  isActive?: true;
5
37
  $text?: any;
@@ -1 +1 @@
1
- {"version":3,"file":"configureCountriesModule.d.ts","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAmB,MAAM,8BAA8B,CAAC;AAC5E,OAAO,EAAE,eAAe,EAAW,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAW3F,eAAO,MAAM,iBAAiB,qCAAmD,YAAY;eAC9D,IAAI;YAAU,GAAG;cAAY,IAAI;CAI/D,CAAC;AAEF,eAAO,MAAM,wBAAwB,iCAGlC,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,KAAG,OAAO,CAAC,eAAe,CA6E9D,CAAC"}
1
+ {"version":3,"file":"configureCountriesModule.d.ts","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;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AAQ,EAAE,MAAM,KAAK,MAAM,CAAC;IAErD,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACvD,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACzC,MAAM,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CAClD,CAAC;AAIF,eAAO,MAAM,iBAAiB,qCAAmD,YAAY;eAC9D,IAAI;YAAU,GAAG;cAAY,IAAI;CAI/D,CAAC;AAEF,eAAO,MAAM,wBAAwB,iCAGlC,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,KAAG,OAAO,CAAC,eAAe,CA2F9D,CAAC"}
@@ -1,9 +1,8 @@
1
1
  import { emit, registerEvents } from '@unchainedshop/events';
2
- import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/mongodb';
3
- import { SortDirection } from '@unchainedshop/types/api.js';
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.country;
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 mutations.create({
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 (_id, doc) => {
70
- const countryId = await mutations.update(_id, { $set: doc });
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 mutations.delete(countryId);
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":"AAEA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,mBAA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AAO,EAAE,GAAG,CAAC,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,SAAS,EAAE,GAAG,EAAE,EAAE;YAC/B,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,SAAS,EAAE,EAAE;YAC1B,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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/core-countries",
3
- "version": "3.0.0-alpha2",
3
+ "version": "3.0.0-alpha3",
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-alpha2",
35
- "@unchainedshop/utils": "^3.0.0-alpha2",
36
- "simpl-schema": "^3.4.6"
34
+ "@unchainedshop/events": "^3.0.0-alpha3",
35
+ "@unchainedshop/utils": "^3.0.0-alpha3"
37
36
  },
38
37
  "devDependencies": {
39
- "@types/node": "^20.14.12",
40
- "@unchainedshop/types": "^3.0.0-alpha2",
38
+ "@types/node": "^22.5.5",
41
39
  "cross-env": "^7.0.3",
42
40
  "jest": "^29.7.0",
43
- "ts-jest": "^29.2.3",
44
- "typescript": "^5.5.4"
41
+ "ts-jest": "^29.2.5",
42
+ "typescript": "^5.6.2"
45
43
  }
46
44
  }
@@ -1 +1 @@
1
- export { configureCountriesModule } from './module/configureCountriesModule.js';
1
+ export * from './module/configureCountriesModule.js';
@@ -1,5 +1,5 @@
1
1
  import { mongodb, buildDbIndexes } from '@unchainedshop/mongodb';
2
- import { Country } from '@unchainedshop/types/countries.js';
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,6 +1,6 @@
1
- import { Migration, MigrationRepository } from '@unchainedshop/types/core.js';
1
+ import { Migration, MigrationRepository } from '@unchainedshop/core';
2
2
  import { CountriesCollection } from '../db/CountriesCollection.js';
3
- import { Country } from '@unchainedshop/types/countries.js';
3
+ import { Country } from '../countries-index.js';
4
4
 
5
5
  export default function addMigrations(repository: MigrationRepository<Migration>) {
6
6
  repository?.register({
@@ -1,13 +1,45 @@
1
- import { ModuleInput, ModuleMutations } from '@unchainedshop/types/core.js';
2
- import { CountriesModule, Country, CountryQuery } from '@unchainedshop/types/countries.js';
1
+ import type { mongodb, TimestampFields } from '@unchainedshop/mongodb';
2
+ import { ModuleInput } from '@unchainedshop/core';
3
3
  import { emit, registerEvents } from '@unchainedshop/events';
4
- import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/mongodb';
5
- import { SortDirection, SortOption } from '@unchainedshop/types/api.js';
4
+ import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
5
+ import { SortDirection, SortOption } from '@unchainedshop/utils';
6
6
  import { systemLocale } from '@unchainedshop/utils';
7
7
  import { CountriesCollection } from '../db/CountriesCollection.js';
8
- import { CountriesSchema } from '../db/CountriesSchema.js';
9
8
  import addMigrations from '../migrations/addMigrations.js';
10
9
 
10
+ export type Country = {
11
+ _id?: string;
12
+ isoCode: string;
13
+ isActive?: boolean;
14
+ defaultCurrencyCode?: string;
15
+ } & TimestampFields;
16
+
17
+ export type CountryQuery = {
18
+ includeInactive?: boolean;
19
+ queryString?: string;
20
+ };
21
+ export type CountriesModule = {
22
+ findCountry: (params: { countryId?: string; isoCode?: string }) => Promise<Country>;
23
+ findCountries: (
24
+ params: CountryQuery & {
25
+ limit?: number;
26
+ offset?: number;
27
+ sort?: Array<SortOption>;
28
+ },
29
+ options?: mongodb.FindOptions,
30
+ ) => Promise<Array<Country>>;
31
+ count: (query: CountryQuery) => Promise<number>;
32
+ countryExists: (params: { countryId: string }) => Promise<boolean>;
33
+
34
+ flagEmoji: (country: Country) => string;
35
+ isBase: (country: Country) => boolean;
36
+ name: (country: Country, language: string) => string;
37
+
38
+ update: (_id: string, doc: Country) => Promise<string>;
39
+ delete: (_id: string) => Promise<number>;
40
+ create: (doc: Country) => Promise<string | null>;
41
+ };
42
+
11
43
  const COUNTRY_EVENTS: string[] = ['COUNTRY_CREATE', 'COUNTRY_UPDATE', 'COUNTRY_REMOVE'];
12
44
 
13
45
  export const buildFindSelector = ({ includeInactive = false, queryString = '' }: CountryQuery) => {
@@ -28,8 +60,6 @@ export const configureCountriesModule = async ({
28
60
 
29
61
  const Countries = await CountriesCollection(db);
30
62
 
31
- const mutations = generateDbMutations<Country>(Countries, CountriesSchema) as ModuleMutations<Country>;
32
-
33
63
  return {
34
64
  count: async (query) => {
35
65
  const countryCount = await Countries.countDocuments(buildFindSelector(query));
@@ -64,6 +94,7 @@ export const configureCountriesModule = async ({
64
94
  name(country, language) {
65
95
  return new Intl.DisplayNames([language], { type: 'region', fallback: 'code' }).of(country.isoCode);
66
96
  },
97
+
67
98
  flagEmoji(country) {
68
99
  const letterToLetterEmoji = (letter: string): string => {
69
100
  return String.fromCodePoint(letter.toLowerCase().charCodeAt(0) + 127365);
@@ -72,11 +103,14 @@ export const configureCountriesModule = async ({
72
103
  },
73
104
 
74
105
  isBase(country) {
75
- return country.isoCode === systemLocale.country;
106
+ return country.isoCode === systemLocale.region;
76
107
  },
77
- create: async (doc: Country) => {
108
+
109
+ create: async (doc) => {
78
110
  await Countries.deleteOne({ isoCode: doc.isoCode.toUpperCase(), deleted: { $ne: null } });
79
- const countryId = await mutations.create({
111
+ const { insertedId: countryId } = await Countries.insertOne({
112
+ _id: generateDbObjectId(),
113
+ created: new Date(),
80
114
  ...doc,
81
115
  isoCode: doc.isoCode.toUpperCase(),
82
116
  isActive: true,
@@ -84,17 +118,29 @@ export const configureCountriesModule = async ({
84
118
  await emit('COUNTRY_CREATE', { countryId });
85
119
  return countryId;
86
120
  },
87
- update: async (_id: string, doc: Partial<Country>) => {
88
- const countryId = await mutations.update(_id, { $set: doc });
121
+
122
+ update: async (countryId, doc) => {
123
+ await Countries.updateOne(generateDbFilterById(countryId), {
124
+ $set: {
125
+ updated: new Date(),
126
+ ...doc,
127
+ },
128
+ });
89
129
  await emit('COUNTRY_UPDATE', { countryId });
90
130
  return countryId;
91
131
  },
132
+
92
133
  delete: async (countryId) => {
93
- const deletedCount = await mutations.delete(countryId);
134
+ const { modifiedCount: deletedCount } = await Countries.updateOne(
135
+ generateDbFilterById(countryId),
136
+ {
137
+ $set: {
138
+ deleted: new Date(),
139
+ },
140
+ },
141
+ );
94
142
  await emit('COUNTRY_REMOVE', { countryId });
95
143
  return deletedCount;
96
144
  },
97
-
98
- deletePermanently: mutations.deletePermanently,
99
145
  };
100
146
  };
@@ -1,3 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export declare const CountriesSchema: SimpleSchema;
3
- //# sourceMappingURL=CountriesSchema.d.ts.map
@@ -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
- );