@merkaly/api 0.2.5-4 → 0.2.5-6

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.
Files changed (241) hide show
  1. package/.bin/deploy.sh +9 -0
  2. package/.bin/package.sh +12 -0
  3. package/.docker/Dockerfile +21 -0
  4. package/.dockerignore +12 -0
  5. package/.github/dependabot.yml +17 -0
  6. package/.github/semantic.yml +5 -0
  7. package/.github/workflows/pull_request.yml +35 -0
  8. package/.prettierrc +5 -0
  9. package/app.ts +11 -0
  10. package/authCertificate.pem +19 -0
  11. package/nest-cli.json +8 -0
  12. package/package.json +66 -78
  13. package/src/abstracts/abstarct.repository.ts +62 -0
  14. package/src/abstracts/abstract.controller.ts +18 -0
  15. package/src/abstracts/abstract.entity.ts +22 -0
  16. package/src/abstracts/abstract.router.ts +7 -0
  17. package/src/abstracts/abstract.validator.ts +61 -0
  18. package/src/app.config.ts +59 -0
  19. package/src/app.console.ts +24 -0
  20. package/src/app.guard.ts +39 -0
  21. package/src/app.module.ts +54 -0
  22. package/src/app.strategy.ts +21 -0
  23. package/src/commands/seed.command.ts +263 -0
  24. package/src/decorators/public.decorator.ts +5 -0
  25. package/src/decorators/user.decorator.ts +30 -0
  26. package/src/exceptions/missing-identity.exception.ts +11 -0
  27. package/src/interceptors/mongoose.interceptor.ts +63 -0
  28. package/src/main.ts +27 -0
  29. package/src/middlewares/router.middleware.ts +14 -0
  30. package/src/modules/asset/asset.module.ts +19 -0
  31. package/src/modules/asset/files/file.controller.ts +22 -0
  32. package/src/modules/asset/files/file.entity.ts +24 -0
  33. package/src/modules/asset/files/file.module.ts +21 -0
  34. package/src/modules/asset/files/file.repository.ts +44 -0
  35. package/src/modules/asset/files/file.schema.ts +8 -0
  36. package/src/modules/asset/files/file.service.ts +4 -0
  37. package/src/modules/auth/auth.controller.ts +57 -0
  38. package/src/modules/auth/auth.module.ts +20 -0
  39. package/src/modules/auth/organizations/organization.controller.ts +20 -0
  40. package/src/modules/auth/organizations/organization.entity.ts +22 -0
  41. package/src/modules/auth/organizations/organization.module.ts +21 -0
  42. package/src/modules/auth/organizations/organization.repository.ts +16 -0
  43. package/src/modules/auth/organizations/organization.schema.ts +8 -0
  44. package/src/modules/auth/users/user.controller.ts +23 -0
  45. package/src/modules/auth/users/user.entity.ts +25 -0
  46. package/src/modules/auth/users/user.module.ts +20 -0
  47. package/src/modules/auth/users/user.repository.ts +79 -0
  48. package/src/modules/auth/users/user.schema.ts +8 -0
  49. package/src/modules/auth/users/user.validator.ts +32 -0
  50. package/src/modules/command.module.ts +15 -0
  51. package/src/modules/global.module.ts +46 -0
  52. package/src/modules/inventory/brands/brand.controller.ts +44 -0
  53. package/src/modules/inventory/brands/brand.entity.ts +20 -0
  54. package/src/modules/inventory/brands/brand.module.ts +20 -0
  55. package/src/modules/inventory/brands/brand.repository.ts +40 -0
  56. package/src/modules/inventory/brands/brand.schema.ts +8 -0
  57. package/src/modules/inventory/brands/brand.validator.ts +31 -0
  58. package/src/modules/inventory/categories/category.controller.ts +42 -0
  59. package/src/modules/inventory/categories/category.entity.ts +15 -0
  60. package/src/modules/inventory/categories/category.module.ts +19 -0
  61. package/src/modules/inventory/categories/category.repository.ts +34 -0
  62. package/src/modules/inventory/categories/category.schema.ts +8 -0
  63. package/src/modules/inventory/categories/category.validator.ts +20 -0
  64. package/src/modules/inventory/complements/complement.controller.ts +41 -0
  65. package/src/modules/inventory/complements/complement.entity.ts +15 -0
  66. package/src/modules/inventory/complements/complement.module.ts +19 -0
  67. package/src/modules/inventory/complements/complement.repository.ts +37 -0
  68. package/src/modules/inventory/complements/complement.schema.ts +8 -0
  69. package/src/modules/inventory/complements/complement.validator.ts +28 -0
  70. package/src/modules/inventory/inventory.module.ts +23 -0
  71. package/src/modules/inventory/products/documents/attribute.document.ts +14 -0
  72. package/src/modules/inventory/products/documents/dimension.document.ts +16 -0
  73. package/src/modules/inventory/products/product.controller.ts +62 -0
  74. package/src/modules/inventory/products/product.entity.ts +46 -0
  75. package/src/modules/inventory/products/product.module.ts +19 -0
  76. package/src/modules/inventory/products/product.repository.ts +64 -0
  77. package/src/modules/inventory/products/product.schema.ts +19 -0
  78. package/src/modules/inventory/products/product.validator.ts +132 -0
  79. package/src/modules/inventory/products/validators/attributes.validator.ts +15 -0
  80. package/src/modules/inventory/products/validators/dimension.validator.ts +19 -0
  81. package/src/modules/sale/customers/customer.controller.ts +41 -0
  82. package/src/modules/sale/customers/customer.entity.ts +19 -0
  83. package/src/modules/sale/customers/customer.module.ts +18 -0
  84. package/src/modules/sale/customers/customer.repository.ts +30 -0
  85. package/src/modules/sale/customers/customer.schema.ts +8 -0
  86. package/src/modules/sale/customers/customer.validator.ts +43 -0
  87. package/src/modules/sale/orders/documents/address.document.ts +22 -0
  88. package/src/modules/sale/orders/documents/status.document.ts +17 -0
  89. package/src/modules/sale/orders/entities/item.entity.ts +24 -0
  90. package/src/modules/sale/orders/enums/order.status.ts +6 -0
  91. package/src/modules/sale/orders/order.controller.ts +65 -0
  92. package/src/modules/sale/orders/order.entity.ts +46 -0
  93. package/src/modules/sale/orders/order.module.ts +22 -0
  94. package/src/modules/sale/orders/order.repository.ts +122 -0
  95. package/src/modules/sale/orders/order.schema.ts +36 -0
  96. package/src/modules/sale/orders/order.validator.ts +44 -0
  97. package/src/modules/sale/orders/validators/address.validator.ts +50 -0
  98. package/src/modules/sale/orders/validators/billing.validator.ts +11 -0
  99. package/src/modules/sale/orders/validators/item.validator.ts +14 -0
  100. package/src/modules/sale/orders/validators/payment.validator.ts +44 -0
  101. package/src/modules/sale/orders/validators/shipping.validator.ts +14 -0
  102. package/src/modules/sale/payments/entities/status.entity.ts +17 -0
  103. package/src/modules/sale/payments/enums/billing.type.ts +6 -0
  104. package/src/modules/sale/payments/enums/payment.status.ts +7 -0
  105. package/src/modules/sale/payments/enums/shipping.type.ts +4 -0
  106. package/src/modules/sale/payments/payment.controller.ts +45 -0
  107. package/src/modules/sale/payments/payment.entity.ts +25 -0
  108. package/src/modules/sale/payments/payment.module.ts +20 -0
  109. package/src/modules/sale/payments/payment.repository.ts +57 -0
  110. package/src/modules/sale/payments/payment.schema.ts +14 -0
  111. package/src/modules/sale/payments/payment.validator.ts +32 -0
  112. package/src/modules/sale/sale.module.ts +20 -0
  113. package/src/services/auth0.service.ts +49 -0
  114. package/src/services/mongo.service.ts +52 -0
  115. package/test/auth/auth.spec.ts +20 -0
  116. package/test/main.ts +12 -0
  117. package/tsconfig.build.json +4 -0
  118. package/tsconfig.json +21 -0
  119. package/tsconfig.package.json +22 -0
  120. package/.output/abstract/abstract.entity.d.ts +0 -9
  121. package/.output/abstract/abstract.exception.d.ts +0 -11
  122. package/.output/abstract/abstract.exception.js +0 -53
  123. package/.output/abstract/abstract.fixture.d.ts +0 -4
  124. package/.output/abstract/abstract.fixture.js +0 -11
  125. package/.output/abstract/abstract.validator.d.ts +0 -20
  126. package/.output/abstract/abstract.validator.js +0 -105
  127. package/.output/exceptions/missing-identity.exception.d.ts +0 -5
  128. package/.output/exceptions/missing-identity.exception.js +0 -30
  129. package/.output/exceptions/store-not-implemented.exception.d.ts +0 -5
  130. package/.output/exceptions/store-not-implemented.exception.js +0 -31
  131. package/.output/exceptions/store-not-recognized.exception.d.ts +0 -5
  132. package/.output/exceptions/store-not-recognized.exception.js +0 -30
  133. package/.output/modules/asset/files/file.entity.d.ts +0 -10
  134. package/.output/modules/auth/auth.types.d.ts +0 -3
  135. package/.output/modules/auth/auth.types.js +0 -7
  136. package/.output/modules/auth/users/user.validator.d.ts +0 -10
  137. package/.output/modules/auth/users/user.validator.js +0 -50
  138. package/.output/modules/config/organization/organization.entity.d.ts +0 -21
  139. package/.output/modules/config/organization/organization.types.d.ts +0 -23
  140. package/.output/modules/config/organization/organization.types.js +0 -107
  141. package/.output/modules/config/organization/organization.validator.d.ts +0 -18
  142. package/.output/modules/config/organization/organization.validator.js +0 -88
  143. package/.output/modules/insight/validators/order.validator.d.ts +0 -4
  144. package/.output/modules/insight/validators/order.validator.js +0 -27
  145. package/.output/modules/inventory/brands/brand.entity.d.ts +0 -9
  146. package/.output/modules/inventory/brands/brand.exception.d.ts +0 -5
  147. package/.output/modules/inventory/brands/brand.exception.js +0 -40
  148. package/.output/modules/inventory/brands/brand.validator.d.ts +0 -11
  149. package/.output/modules/inventory/brands/brand.validator.js +0 -75
  150. package/.output/modules/inventory/categories/category.entity.d.ts +0 -12
  151. package/.output/modules/inventory/categories/category.exception.d.ts +0 -5
  152. package/.output/modules/inventory/categories/category.exception.js +0 -40
  153. package/.output/modules/inventory/categories/category.fixture.d.ts +0 -5
  154. package/.output/modules/inventory/categories/category.fixture.js +0 -38
  155. package/.output/modules/inventory/categories/category.validator.d.ts +0 -13
  156. package/.output/modules/inventory/categories/category.validator.js +0 -88
  157. package/.output/modules/inventory/products/entities/code.entity.d.ts +0 -5
  158. package/.output/modules/inventory/products/entities/dimension.entity.d.ts +0 -6
  159. package/.output/modules/inventory/products/entities/seo.entity.d.ts +0 -5
  160. package/.output/modules/inventory/products/entities/variant.entity.d.ts +0 -10
  161. package/.output/modules/inventory/products/product.entity.d.ts +0 -26
  162. package/.output/modules/inventory/products/product.exception.d.ts +0 -5
  163. package/.output/modules/inventory/products/product.exception.js +0 -40
  164. package/.output/modules/inventory/products/product.fixture.d.ts +0 -5
  165. package/.output/modules/inventory/products/product.fixture.js +0 -39
  166. package/.output/modules/inventory/products/product.validator.d.ts +0 -38
  167. package/.output/modules/inventory/products/product.validator.js +0 -235
  168. package/.output/modules/inventory/products/schemas/variant.schema.d.ts +0 -27
  169. package/.output/modules/inventory/products/schemas/variant.schema.js +0 -6
  170. package/.output/modules/inventory/products/validators/code.validator.d.ts +0 -5
  171. package/.output/modules/inventory/products/validators/code.validator.js +0 -37
  172. package/.output/modules/inventory/products/validators/dimension.validator.d.ts +0 -6
  173. package/.output/modules/inventory/products/validators/dimension.validator.js +0 -43
  174. package/.output/modules/inventory/products/validators/seo.validator.d.ts +0 -5
  175. package/.output/modules/inventory/products/validators/seo.validator.js +0 -34
  176. package/.output/modules/inventory/products/validators/variant.validator.d.ts +0 -9
  177. package/.output/modules/inventory/products/validators/variant.validator.js +0 -39
  178. package/.output/modules/inventory/properties/property.entity.d.ts +0 -8
  179. package/.output/modules/inventory/properties/property.exception.d.ts +0 -5
  180. package/.output/modules/inventory/properties/property.exception.js +0 -40
  181. package/.output/modules/inventory/properties/property.types.d.ts +0 -6
  182. package/.output/modules/inventory/properties/property.types.js +0 -10
  183. package/.output/modules/inventory/properties/property.validator.d.ts +0 -12
  184. package/.output/modules/inventory/properties/property.validator.js +0 -79
  185. package/.output/modules/layout/pages/page.entity.d.ts +0 -8
  186. package/.output/modules/layout/pages/page.fixture.d.ts +0 -5
  187. package/.output/modules/layout/pages/page.fixture.js +0 -36
  188. package/.output/modules/layout/pages/page.types.d.ts +0 -20
  189. package/.output/modules/layout/pages/page.types.js +0 -2
  190. package/.output/modules/layout/pages/page.validator.d.ts +0 -8
  191. package/.output/modules/layout/pages/page.validator.js +0 -61
  192. package/.output/modules/sale/customers/customer.entity.d.ts +0 -12
  193. package/.output/modules/sale/customers/customer.exception.d.ts +0 -5
  194. package/.output/modules/sale/customers/customer.exception.js +0 -40
  195. package/.output/modules/sale/customers/customer.validator.d.ts +0 -18
  196. package/.output/modules/sale/customers/customer.validator.js +0 -107
  197. package/.output/modules/sale/orders/billing/billing.entity.d.ts +0 -10
  198. package/.output/modules/sale/orders/billing/billing.types.d.ts +0 -9
  199. package/.output/modules/sale/orders/billing/billing.types.js +0 -14
  200. package/.output/modules/sale/orders/billing/billing.validator.d.ts +0 -9
  201. package/.output/modules/sale/orders/billing/billing.validator.js +0 -42
  202. package/.output/modules/sale/orders/billing/entities/address.entity.d.ts +0 -10
  203. package/.output/modules/sale/orders/billing/entities/customer.entity.d.ts +0 -5
  204. package/.output/modules/sale/orders/billing/entities/status.entity.d.ts +0 -6
  205. package/.output/modules/sale/orders/billing/validators/address.validator.d.ts +0 -10
  206. package/.output/modules/sale/orders/billing/validators/address.validator.js +0 -56
  207. package/.output/modules/sale/orders/billing/validators/customer.validator.d.ts +0 -5
  208. package/.output/modules/sale/orders/billing/validators/customer.validator.js +0 -36
  209. package/.output/modules/sale/orders/customer/customer.entity.d.ts +0 -5
  210. package/.output/modules/sale/orders/customer/customer.validator.d.ts +0 -5
  211. package/.output/modules/sale/orders/customer/customer.validator.js +0 -36
  212. package/.output/modules/sale/orders/item/item.entity.d.ts +0 -9
  213. package/.output/modules/sale/orders/item/item.schema.d.ts +0 -27
  214. package/.output/modules/sale/orders/item/item.schema.js +0 -9
  215. package/.output/modules/sale/orders/item/item.validator.d.ts +0 -4
  216. package/.output/modules/sale/orders/item/item.validator.js +0 -29
  217. package/.output/modules/sale/orders/order.entity.d.ts +0 -21
  218. package/.output/modules/sale/orders/order.exception.d.ts +0 -13
  219. package/.output/modules/sale/orders/order.exception.js +0 -46
  220. package/.output/modules/sale/orders/order.validator.d.ts +0 -28
  221. package/.output/modules/sale/orders/order.validator.js +0 -140
  222. package/.output/modules/sale/orders/shipping/entities/address.entity.d.ts +0 -10
  223. package/.output/modules/sale/orders/shipping/entities/customer.entity.d.ts +0 -5
  224. package/.output/modules/sale/orders/shipping/entities/status.entity.d.ts +0 -6
  225. package/.output/modules/sale/orders/shipping/shipping.entity.d.ts +0 -11
  226. package/.output/modules/sale/orders/shipping/shipping.types.d.ts +0 -11
  227. package/.output/modules/sale/orders/shipping/shipping.types.js +0 -16
  228. package/.output/modules/sale/orders/shipping/shipping.validator.d.ts +0 -10
  229. package/.output/modules/sale/orders/shipping/shipping.validator.js +0 -48
  230. package/.output/modules/sale/orders/shipping/validators/address.validator.d.ts +0 -10
  231. package/.output/modules/sale/orders/shipping/validators/address.validator.js +0 -56
  232. package/.output/modules/sale/orders/shipping/validators/customer.validator.d.ts +0 -5
  233. package/.output/modules/sale/orders/shipping/validators/customer.validator.js +0 -36
  234. package/.output/modules/sale/orders/status/status.entity.d.ts +0 -6
  235. package/.output/modules/sale/orders/status/status.validator.d.ts +0 -8
  236. package/.output/modules/sale/orders/status/status.validator.js +0 -12
  237. package/.output/services/logger.service.d.ts +0 -11
  238. package/.output/services/logger.service.js +0 -50
  239. package/.output/types.d.ts +0 -18
  240. package/.output/types.js +0 -2
  241. package/LICENSE +0 -674
@@ -0,0 +1,57 @@
1
+ import { Controller, Get, Inject, NotAcceptableException, NotFoundException, Put, Req } from '@nestjs/common';
2
+ import { ApiTags } from '@nestjs/swagger';
3
+ import type { Request } from 'express';
4
+ import { Public } from '../../decorators/public.decorator';
5
+ import { DecodedUser, GetUser } from '../../decorators/user.decorator';
6
+ import { OrganizationRepository } from './organizations/organization.repository';
7
+ import { UserRepository } from './users/user.repository';
8
+
9
+ @Controller('/')
10
+ @ApiTags('auth')
11
+ export class AuthController {
12
+ @Inject()
13
+ protected readonly $user: UserRepository;
14
+
15
+ @Inject()
16
+ protected readonly $organization: OrganizationRepository;
17
+
18
+ @Put('/token')
19
+ public token(@GetUser() user: DecodedUser) {
20
+ return this.$user.upsert(user);
21
+ }
22
+
23
+ @Get('/resolve')
24
+ @Public()
25
+ public async resolve(@Req() req: Request) {
26
+ const list = await this.$organization.find();
27
+ const { origin, identity } = req.headers;
28
+
29
+ if (identity) {
30
+ return this.$organization.read(String(identity));
31
+ }
32
+
33
+ if (!origin) {
34
+ throw new NotAcceptableException();
35
+ }
36
+
37
+ const { hostname } = new URL(origin);
38
+
39
+ const result = list.find((item) => {
40
+ const domain = item.metadata?.domain;
41
+
42
+ if (!domain) {
43
+ return false;
44
+ }
45
+
46
+ const { name } = domain;
47
+
48
+ return String(name).includes(hostname);
49
+ });
50
+
51
+ if (!result) {
52
+ throw new NotFoundException('Store Not Found');
53
+ }
54
+
55
+ return result;
56
+ }
57
+ }
@@ -0,0 +1,20 @@
1
+ import { Module, type ModuleMetadata } from '@nestjs/common';
2
+ import { AuthController } from './auth.controller';
3
+ import { OrganizationModule } from './organizations/organization.module';
4
+ import { UserModule } from './users/user.module';
5
+
6
+ const metadata: ModuleMetadata = {
7
+ controllers: [AuthController],
8
+ exports: [UserModule],
9
+ imports: [UserModule, OrganizationModule],
10
+ providers: [],
11
+ };
12
+
13
+ @Module(metadata)
14
+ export class AuthModule {
15
+ public static readonly path = '/auth';
16
+
17
+ public static readonly module = AuthModule;
18
+
19
+ public static readonly children = metadata.imports;
20
+ }
@@ -0,0 +1,20 @@
1
+ import { Controller, Get, Inject, Param } from '@nestjs/common';
2
+ import { ApiTags } from '@nestjs/swagger';
3
+ import { OrganizationRepository } from './organization.repository';
4
+
5
+ @Controller('/')
6
+ @ApiTags('auth')
7
+ export class OrganizationController {
8
+ @Inject()
9
+ protected readonly $repository: OrganizationRepository;
10
+
11
+ @Get('/')
12
+ public find() {
13
+ return this.$repository.find();
14
+ }
15
+
16
+ @Get('/:id')
17
+ public read(@Param('id') id: string) {
18
+ return this.$repository.read(id);
19
+ }
20
+ }
@@ -0,0 +1,22 @@
1
+ import { Prop } from '@nestjs/mongoose';
2
+ import { Schema as Collection } from '@nestjs/mongoose/dist/decorators/schema.decorator';
3
+ import { Schema } from 'mongoose';
4
+ import { $collection, AbstractEntity } from '../../../abstracts/abstract.entity';
5
+
6
+ @Collection($collection)
7
+ export class OrganizationEntity extends AbstractEntity {
8
+ @Prop({ required: true, type: Schema.Types.String, unique: true })
9
+ public auth0: string;
10
+
11
+ @Prop({ required: true, type: Schema.Types.String, unique: true })
12
+ public name: string;
13
+
14
+ @Prop({ required: true, type: Schema.Types.String })
15
+ public displayName: string;
16
+
17
+ @Prop({ required: true, type: Schema.Types.Mixed })
18
+ public metadata: Record<string, any>;
19
+
20
+ @Prop({ required: true, type: Schema.Types.String })
21
+ public logoUrl: string;
22
+ }
@@ -0,0 +1,21 @@
1
+ import { Module, ModuleMetadata } from '@nestjs/common';
2
+ import { AbstractRouter } from '../../../abstracts/abstract.router';
3
+ import { Auth0Service } from '../../../services/auth0.service';
4
+ import { OrganizationController } from './organization.controller';
5
+ import { OrganizationRepository } from './organization.repository';
6
+
7
+ export const metadata: ModuleMetadata = {
8
+ controllers: [OrganizationController],
9
+ exports: [OrganizationRepository],
10
+ imports: [],
11
+ providers: [OrganizationRepository, Auth0Service],
12
+ };
13
+
14
+ @Module(metadata)
15
+ export class OrganizationModule extends AbstractRouter {
16
+ public static readonly module = OrganizationModule;
17
+
18
+ public static readonly path = '/organization';
19
+
20
+ public static readonly children = metadata.imports;
21
+ }
@@ -0,0 +1,16 @@
1
+ import { Inject, Injectable } from '@nestjs/common';
2
+ import { Auth0Service } from '../../../services/auth0.service';
3
+
4
+ @Injectable()
5
+ export class OrganizationRepository {
6
+ @Inject()
7
+ public readonly $auth0: Auth0Service;
8
+
9
+ public find() {
10
+ return this.$auth0.organizations.find();
11
+ }
12
+
13
+ public read(id: string) {
14
+ return this.$auth0.organizations.read(id);
15
+ }
16
+ }
@@ -0,0 +1,8 @@
1
+ import { SchemaFactory } from '@nestjs/mongoose';
2
+ import { OrganizationEntity } from './organization.entity';
3
+
4
+ export namespace OrganizationSchema {
5
+ export const name = 'auth_organization';
6
+
7
+ export const schema = SchemaFactory.createForClass(OrganizationEntity);
8
+ }
@@ -0,0 +1,23 @@
1
+ import { Controller, Get, Inject, Param, Query } from '@nestjs/common';
2
+ import { ApiTags } from '@nestjs/swagger';
3
+ import { AbstractController } from '../../../abstracts/abstract.controller';
4
+ import { FindValidator, IdMongoValidator, ReadValidator } from '../../../abstracts/abstract.validator';
5
+ import { UserEntity } from './user.entity';
6
+ import { UserRepository } from './user.repository';
7
+
8
+ @Controller('/')
9
+ @ApiTags('auth')
10
+ export class UserController extends AbstractController {
11
+ @Inject()
12
+ protected readonly $repository: UserRepository;
13
+
14
+ @Get('/')
15
+ public find(@Query() validator: FindValidator<UserEntity>) {
16
+ return this.$repository.find(validator);
17
+ }
18
+
19
+ @Get('/:id')
20
+ public read(@Param() { id }: IdMongoValidator, @Query() validator?: ReadValidator) {
21
+ return this.$repository.read(id, validator);
22
+ }
23
+ }
@@ -0,0 +1,25 @@
1
+ import { Prop } from '@nestjs/mongoose';
2
+ import { Schema as Collection } from '@nestjs/mongoose/dist/decorators/schema.decorator';
3
+ import { Schema } from 'mongoose';
4
+ import { $collection, AbstractEntity } from '../../../abstracts/abstract.entity';
5
+
6
+ @Collection($collection)
7
+ export class UserEntity extends AbstractEntity {
8
+ @Prop({ required: true, type: Schema.Types.String, unique: true })
9
+ public auth0: string;
10
+
11
+ @Prop({ required: true, type: Schema.Types.String, unique: true })
12
+ public email: string;
13
+
14
+ @Prop({ required: true, type: Schema.Types.String })
15
+ public name: string;
16
+
17
+ @Prop({ required: true, type: Schema.Types.String })
18
+ public picture: string;
19
+
20
+ @Prop({ required: true, type: Schema.Types.String })
21
+ public locale: string;
22
+
23
+ @Prop({ default: () => new Date(), type: Schema.Types.Date })
24
+ public lastSeen: Date;
25
+ }
@@ -0,0 +1,20 @@
1
+ import { Module, type ModuleMetadata } from '@nestjs/common';
2
+ import { Auth0Service } from '../../../services/auth0.service';
3
+ import { UserController } from './user.controller';
4
+ import { UserRepository } from './user.repository';
5
+
6
+ const metadata: ModuleMetadata = {
7
+ controllers: [UserController],
8
+ exports: [UserRepository],
9
+ imports: [],
10
+ providers: [UserRepository, Auth0Service],
11
+ };
12
+
13
+ @Module(metadata)
14
+ export class UserModule {
15
+ public static readonly path = '/users';
16
+
17
+ public static readonly module = UserModule;
18
+
19
+ public static readonly children = metadata.imports;
20
+ }
@@ -0,0 +1,79 @@
1
+ import { Injectable, NotFoundException } from '@nestjs/common';
2
+ import { InjectModel } from '@nestjs/mongoose';
3
+ import { Model } from 'mongoose';
4
+ import { AbstractRepository } from '../../../abstracts/abstarct.repository';
5
+ import { DecodedUser } from '../../../decorators/user.decorator';
6
+ import { UserEntity } from './user.entity';
7
+ import { UserSchema } from './user.schema';
8
+ import { CreateUserValidator, UpdatedUserValidator } from './user.validator';
9
+
10
+ @Injectable()
11
+ export class UserRepository extends AbstractRepository<UserEntity> {
12
+ @InjectModel(UserSchema.name)
13
+ protected readonly $model: Model<UserEntity>;
14
+
15
+ public async create(validator: CreateUserValidator) {
16
+ const user = new this.$model();
17
+
18
+ user.auth0 = validator.auth0;
19
+ user.name = validator.name;
20
+ user.email = validator.email.toLowerCase();
21
+ user.picture = validator.picture;
22
+ user.locale = validator.locale;
23
+
24
+ await user.save();
25
+
26
+ return user;
27
+ }
28
+
29
+ public async update(id: string, validator: UpdatedUserValidator) {
30
+ const user = await this.read(id);
31
+
32
+ user.name = validator.name || user.name;
33
+ user.picture = validator.picture || user.picture;
34
+ user.locale = validator.locale || user.locale;
35
+ user.lastSeen = new Date();
36
+
37
+ await user.save();
38
+
39
+ return user;
40
+ }
41
+
42
+ public async upsert(decodedUser: DecodedUser) {
43
+ let foundedUser: UserEntity;
44
+
45
+ try {
46
+ foundedUser = await this.readByAuth0(decodedUser.sub);
47
+ } catch (e: unknown) {}
48
+
49
+ if (!foundedUser) {
50
+ const validator: CreateUserValidator = {
51
+ auth0: decodedUser.sub,
52
+ email: decodedUser.email,
53
+ locale: decodedUser.locale,
54
+ name: decodedUser.name,
55
+ picture: decodedUser.picture,
56
+ };
57
+
58
+ return this.create(validator);
59
+ }
60
+
61
+ const validator: UpdatedUserValidator = {
62
+ locale: decodedUser.locale,
63
+ name: decodedUser.name,
64
+ picture: decodedUser.picture,
65
+ };
66
+
67
+ return this.update(foundedUser._id, validator);
68
+ }
69
+
70
+ public async readByAuth0(auth0: string): Promise<UserEntity> {
71
+ const user = await this.$model.findOne({ auth0 });
72
+
73
+ if (!user) {
74
+ throw new NotFoundException();
75
+ }
76
+
77
+ return user;
78
+ }
79
+ }
@@ -0,0 +1,8 @@
1
+ import { SchemaFactory } from '@nestjs/mongoose';
2
+ import { UserEntity } from './user.entity';
3
+
4
+ export namespace UserSchema {
5
+ export const name = 'auth_users';
6
+
7
+ export const schema = SchemaFactory.createForClass(UserEntity);
8
+ }
@@ -0,0 +1,32 @@
1
+ import { IsEmail, IsLocale, IsOptional, IsString } from 'class-validator';
2
+
3
+ export class CreateUserValidator {
4
+ @IsString()
5
+ public auth0: string;
6
+
7
+ @IsString()
8
+ public name: string;
9
+
10
+ @IsEmail()
11
+ public email: string;
12
+
13
+ @IsString()
14
+ public picture: string;
15
+
16
+ @IsLocale()
17
+ public locale: string;
18
+ }
19
+
20
+ export class UpdatedUserValidator {
21
+ @IsString()
22
+ @IsOptional()
23
+ public name?: string;
24
+
25
+ @IsString()
26
+ @IsOptional()
27
+ public picture?: string;
28
+
29
+ @IsLocale()
30
+ @IsOptional()
31
+ public locale?: string;
32
+ }
@@ -0,0 +1,15 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { ConsoleModule } from 'nestjs-console';
3
+ import { router } from '../app.module';
4
+ import { SeedCommand } from '../commands/seed.command';
5
+
6
+ export class CommandsModule {
7
+ public static register(): DynamicModule {
8
+ return {
9
+ global: true,
10
+ imports: [ConsoleModule, ...router],
11
+ module: CommandsModule,
12
+ providers: [SeedCommand],
13
+ };
14
+ }
15
+ }
@@ -0,0 +1,46 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { MongooseModule } from '@nestjs/mongoose';
3
+ import { Auth0Service } from '../services/auth0.service';
4
+ import { MongoService } from '../services/mongo.service';
5
+ import { FileSchema } from './asset/files/file.schema';
6
+ import { OrganizationSchema } from './auth/organizations/organization.schema';
7
+ import { UserSchema } from './auth/users/user.schema';
8
+ import { BrandSchema } from './inventory/brands/brand.schema';
9
+ import { CategorySchema } from './inventory/categories/category.schema';
10
+ import { ComplementSchema } from './inventory/complements/complement.schema';
11
+ import { ProductSchema } from './inventory/products/product.schema';
12
+ import { CustomerSchema } from './sale/customers/customer.schema';
13
+ import { OrderSchema } from './sale/orders/order.schema';
14
+ import { PaymentSchema } from './sale/payments/payment.schema';
15
+
16
+ const mongooseEntities = MongooseModule.forFeature([
17
+ // Assets
18
+ FileSchema,
19
+
20
+ // Auth
21
+ UserSchema,
22
+ OrganizationSchema,
23
+
24
+ // Sale
25
+ OrderSchema,
26
+ PaymentSchema,
27
+ CustomerSchema,
28
+
29
+ // Inventory
30
+ BrandSchema,
31
+ ProductSchema,
32
+ CategorySchema,
33
+ ComplementSchema,
34
+ ]);
35
+
36
+ export class GlobalModule {
37
+ public static register(): DynamicModule {
38
+ return {
39
+ exports: [mongooseEntities, Auth0Service],
40
+ global: true,
41
+ imports: [MongooseModule.forRootAsync({ useClass: MongoService }), mongooseEntities],
42
+ module: GlobalModule,
43
+ providers: [Auth0Service],
44
+ };
45
+ }
46
+ }
@@ -0,0 +1,44 @@
1
+ import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Inject, Param, Patch, Post, Query } from '@nestjs/common';
2
+ import { ApiTags } from '@nestjs/swagger';
3
+ import { AbstractController } from '../../../abstracts/abstract.controller';
4
+ import { FindValidator, IdMongoValidator, ReadValidator } from '../../../abstracts/abstract.validator';
5
+ import { Public } from '../../../decorators/public.decorator';
6
+ import { BrandEntity } from './brand.entity';
7
+ import { BrandRepository } from './brand.repository';
8
+ import { CreateBrandValidator, UpdateBrandValidator } from './brand.validator';
9
+
10
+ @Controller()
11
+ @ApiTags('inventory')
12
+ export class BrandController extends AbstractController {
13
+ @Inject()
14
+ protected readonly $repository: BrandRepository;
15
+
16
+ @Get('/')
17
+ @Public()
18
+ public async find(@Query() validator: FindValidator<BrandEntity>) {
19
+ return super.find(validator);
20
+ }
21
+
22
+ @Get('/:id')
23
+ @Public()
24
+ public async read(@Param() { id }: IdMongoValidator, @Query() validator?: ReadValidator) {
25
+ return this.$repository.read(id, validator);
26
+ }
27
+
28
+ @Post('/')
29
+ @HttpCode(HttpStatus.CREATED)
30
+ public async create(@Body() validator: CreateBrandValidator) {
31
+ return this.$repository.create(validator);
32
+ }
33
+
34
+ @Patch('/:id')
35
+ public async update(@Param('id') id: string, @Body() validator: UpdateBrandValidator) {
36
+ return this.$repository.update(id, validator);
37
+ }
38
+
39
+ @Delete('/:id')
40
+ @HttpCode(HttpStatus.NO_CONTENT)
41
+ public delete(@Param() { id }: IdMongoValidator) {
42
+ return this.$repository.delete(id);
43
+ }
44
+ }
@@ -0,0 +1,20 @@
1
+ import { Prop } from '@nestjs/mongoose';
2
+ import { Schema as Collection } from '@nestjs/mongoose/dist/decorators/schema.decorator';
3
+ import { Schema as MongoSchema } from 'mongoose';
4
+ import { $collection, AbstractEntity } from '../../../abstracts/abstract.entity';
5
+
6
+ @Collection($collection)
7
+ export class BrandEntity extends AbstractEntity {
8
+ @Prop({
9
+ type: MongoSchema.Types.String,
10
+ required: true,
11
+ unique: true,
12
+ length: 32,
13
+ })
14
+ public name: string;
15
+
16
+ @Prop({ type: MongoSchema.Types.String })
17
+ public description: string;
18
+
19
+ public readonly picture: string;
20
+ }
@@ -0,0 +1,20 @@
1
+ import { Module, ModuleMetadata } from '@nestjs/common';
2
+ import { AbstractRouter } from '../../../abstracts/abstract.router';
3
+ import { BrandController } from './brand.controller';
4
+ import { BrandRepository } from './brand.repository';
5
+
6
+ export const metadata: ModuleMetadata = {
7
+ controllers: [BrandController],
8
+ exports: [BrandRepository],
9
+ imports: [],
10
+ providers: [BrandRepository],
11
+ };
12
+
13
+ @Module(metadata)
14
+ export class BrandModule extends AbstractRouter {
15
+ public static readonly path = '/brands';
16
+
17
+ public static readonly module = BrandModule;
18
+
19
+ public static readonly children = metadata.imports;
20
+ }
@@ -0,0 +1,40 @@
1
+ import { Injectable } from '@nestjs/common';
2
+ import { InjectModel } from '@nestjs/mongoose';
3
+ import { Model } from 'mongoose';
4
+ import { AbstractRepository } from '../../../abstracts/abstarct.repository';
5
+ import { BrandEntity } from './brand.entity';
6
+ import { BrandSchema } from './brand.schema';
7
+ import { CreateBrandValidator, UpdateBrandValidator } from './brand.validator';
8
+
9
+ @Injectable()
10
+ export class BrandRepository extends AbstractRepository<BrandEntity> {
11
+ @InjectModel(BrandSchema.name)
12
+ protected readonly $model: Model<BrandEntity>;
13
+
14
+ public async create(validator: CreateBrandValidator) {
15
+ const brand = new this.$model();
16
+
17
+ brand.name = validator.name;
18
+ brand.description = validator.description;
19
+
20
+ await brand.save();
21
+
22
+ return brand;
23
+ }
24
+
25
+ public async update(id: string, validator: UpdateBrandValidator) {
26
+ const brand = await this.read(id);
27
+
28
+ if (validator.name) {
29
+ brand.name = validator.name;
30
+ }
31
+
32
+ if (validator.description) {
33
+ brand.description = validator.description;
34
+ }
35
+
36
+ await brand.save();
37
+
38
+ return brand;
39
+ }
40
+ }
@@ -0,0 +1,8 @@
1
+ import { SchemaFactory } from '@nestjs/mongoose';
2
+ import { BrandEntity } from './brand.entity';
3
+
4
+ export namespace BrandSchema {
5
+ export const name = 'inventory_brands';
6
+
7
+ export const schema = SchemaFactory.createForClass(BrandEntity);
8
+ }
@@ -0,0 +1,31 @@
1
+ import { IsMongoId, IsOptional, IsString, Length } from 'class-validator';
2
+ import { AbstractValidator } from '../../../abstracts/abstract.validator';
3
+
4
+ export class CreateBrandValidator extends AbstractValidator {
5
+ @IsString()
6
+ @Length(2, 32)
7
+ public name: string;
8
+
9
+ @IsString()
10
+ @IsOptional()
11
+ public description?: string;
12
+
13
+ @IsMongoId()
14
+ @IsOptional()
15
+ public image: string;
16
+ }
17
+
18
+ export class UpdateBrandValidator extends AbstractValidator {
19
+ @IsString()
20
+ @Length(2, 32)
21
+ @IsOptional()
22
+ public name?: string;
23
+
24
+ @IsString()
25
+ @IsOptional()
26
+ public description?: string;
27
+
28
+ @IsMongoId()
29
+ @IsOptional()
30
+ public image: string;
31
+ }
@@ -0,0 +1,42 @@
1
+ import { Body, Controller, Delete, Get, Inject, Param, Patch, Post, Query } from '@nestjs/common';
2
+ import { ApiTags } from '@nestjs/swagger';
3
+ import { AbstractController } from '../../../abstracts/abstract.controller';
4
+ import { FindValidator, IdMongoValidator, ReadValidator } from '../../../abstracts/abstract.validator';
5
+ import { Public } from '../../../decorators/public.decorator';
6
+ import { CategoryEntity } from './category.entity';
7
+ import { CategoryRepository } from './category.repository';
8
+ import { CreateCategoryValidator, UpdateCategoryValidator } from './category.validator';
9
+
10
+ @Controller('')
11
+ @ApiTags('inventory')
12
+ export class CategoryController extends AbstractController {
13
+ @Inject()
14
+ protected readonly $repository: CategoryRepository;
15
+
16
+ @Public()
17
+ @Get('/')
18
+ public find(@Query() validator: FindValidator<CategoryEntity>) {
19
+ return this.$repository.find(validator);
20
+ }
21
+
22
+ @Public()
23
+ @Get('/:id')
24
+ public read(@Param() { id }: IdMongoValidator, @Query() validator?: ReadValidator) {
25
+ return this.$repository.read(id, validator);
26
+ }
27
+
28
+ @Post('/')
29
+ public create(@Body() validator: CreateCategoryValidator) {
30
+ return this.$repository.create(validator);
31
+ }
32
+
33
+ @Patch('/:id')
34
+ public update(@Param('id') id: string, @Body() validator: UpdateCategoryValidator) {
35
+ return this.$repository.update(id, validator);
36
+ }
37
+
38
+ @Delete('/:id')
39
+ public remove(@Param('id') id: string) {
40
+ return this.$repository.delete(id);
41
+ }
42
+ }
@@ -0,0 +1,15 @@
1
+ import { Prop, Schema as Collection } from '@nestjs/mongoose';
2
+ import { Schema } from 'mongoose';
3
+ import { $collection, AbstractEntity } from '../../../abstracts/abstract.entity';
4
+
5
+ @Collection($collection)
6
+ export class CategoryEntity extends AbstractEntity {
7
+ @Prop({ required: true, type: Schema.Types.String, unique: true })
8
+ public name: string;
9
+
10
+ @Prop({ type: Schema.Types.String })
11
+ public description: string;
12
+
13
+ @Prop({ type: Schema.Types.String })
14
+ public picture: string;
15
+ }