@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,44 @@
1
+ import { Type } from 'class-transformer';
2
+ import {
3
+ IsNotEmpty,
4
+ IsNumberString,
5
+ IsOptional,
6
+ IsPositive,
7
+ IsString,
8
+ Length,
9
+ Max,
10
+ Min,
11
+ ValidateNested,
12
+ } from 'class-validator';
13
+
14
+ export class PaymentCardValidator {
15
+ @IsString()
16
+ @IsNotEmpty()
17
+ public number = String();
18
+
19
+ @IsString()
20
+ @IsNotEmpty()
21
+ public name = String();
22
+
23
+ @IsPositive()
24
+ @Min(1)
25
+ @Max(12)
26
+ public expMonth: number;
27
+
28
+ @IsPositive()
29
+ @Min(new Date().getFullYear())
30
+ @IsNotEmpty()
31
+ public expYear: number;
32
+
33
+ @IsNumberString()
34
+ @Length(3, 4)
35
+ @IsNotEmpty()
36
+ public cvv = String();
37
+ }
38
+
39
+ export class PaymentValidator {
40
+ @ValidateNested()
41
+ @Type(() => PaymentCardValidator)
42
+ @IsOptional()
43
+ public card = new PaymentCardValidator();
44
+ }
@@ -0,0 +1,14 @@
1
+ import { ShippingType } from '../../payments/enums/shipping.type';
2
+ import { AddressValidator } from './address.validator';
3
+
4
+ export class ShippingValidator {
5
+ public address = new AddressValidator();
6
+
7
+ public type: ShippingType;
8
+
9
+ public name = String();
10
+
11
+ public phone = String();
12
+
13
+ public email = String();
14
+ }
@@ -0,0 +1,17 @@
1
+ import { Prop } from '@nestjs/mongoose';
2
+ import { Schema } from 'mongoose';
3
+ import type { MaybeEntity } from '../../../../abstracts/abstract.entity';
4
+ import { UserEntity } from '../../../auth/users/user.entity';
5
+ import { UserSchema } from '../../../auth/users/user.schema';
6
+ import { PaymentStatus } from '../enums/payment.status';
7
+
8
+ export class StatusEntity {
9
+ @Prop({ enum: PaymentStatus, required: true, type: Schema.Types.String })
10
+ public name: PaymentStatus;
11
+
12
+ @Prop({ ref: UserSchema.name, required: true, type: Schema.Types.ObjectId })
13
+ public user: MaybeEntity<UserEntity>;
14
+
15
+ @Prop({ default: () => new Date(), type: Schema.Types.Date })
16
+ public date: Date;
17
+ }
@@ -0,0 +1,6 @@
1
+ export enum BillingType {
2
+ PIX = 'PIX',
3
+ BOLETO = 'BOLETO',
4
+ CARD = 'CARD',
5
+ TRANSFER = 'TRANSFER',
6
+ }
@@ -0,0 +1,7 @@
1
+ export enum PaymentStatus {
2
+ UNPAID = 'UNPAID',
3
+ WAITING = 'WAITING',
4
+ PAID = 'PAID',
5
+ REFUNDED = 'REFUNDED',
6
+ CANCELLED = 'CANCELLED',
7
+ }
@@ -0,0 +1,4 @@
1
+ export enum ShippingType {
2
+ RETIRE = 'RETIRE',
3
+ DELIVERY = 'DELIVERY',
4
+ }
@@ -0,0 +1,45 @@
1
+ import { Body, Controller, Delete, Get, Inject, Param, Patch, 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 { GetUser } from '../../../decorators/user.decorator';
6
+ import { UserRepository } from '../../auth/users/user.repository';
7
+ import { PaymentEntity } from './payment.entity';
8
+ import { PaymentRepository } from './payment.repository';
9
+ import { UpdatePaymentStatusValidator, UpdatePaymentValidator } from './payment.validator';
10
+
11
+ @Controller('/')
12
+ @ApiTags('sales')
13
+ export class PaymentController extends AbstractController {
14
+ @Inject()
15
+ protected readonly $repository: PaymentRepository;
16
+
17
+ @Inject()
18
+ protected readonly $userRepository: UserRepository;
19
+
20
+ @Get('/')
21
+ public find(@Query() validator: FindValidator<PaymentEntity>) {
22
+ return this.$repository.find(validator);
23
+ }
24
+
25
+ @Get('/:id')
26
+ public read(@Param() { id }: IdMongoValidator, @Query() validator?: ReadValidator) {
27
+ return this.$repository.read(id, validator);
28
+ }
29
+
30
+ @Delete('/:id')
31
+ public delete(@Param() { id }: IdMongoValidator) {
32
+ return this.$repository.delete(id);
33
+ }
34
+
35
+ @Patch('/:id/status/:type')
36
+ public async updateStatus(
37
+ @Param() { id, type }: UpdatePaymentStatusValidator,
38
+ @Body() validator: UpdatePaymentValidator,
39
+ @GetUser('sub') sub: string,
40
+ ) {
41
+ const user = await this.$userRepository.readByAuth0(sub);
42
+
43
+ return this.$repository.updateStatus(id, type, user);
44
+ }
45
+ }
@@ -0,0 +1,25 @@
1
+ import { Prop, Schema as Collection } from '@nestjs/mongoose';
2
+ import { Schema } from 'mongoose';
3
+ import { $collection, AbstractEntity } from '../../../abstracts/abstract.entity';
4
+ import { OrderEntity } from '../orders/order.entity';
5
+ import { StatusEntity } from './entities/status.entity';
6
+ import { BillingType } from './enums/billing.type';
7
+
8
+ @Collection($collection)
9
+ export class PaymentEntity extends AbstractEntity {
10
+ @Prop({ default: () => [], type: Schema.Types.Array })
11
+ public status: StatusEntity[] = [];
12
+
13
+ @Prop({ required: true, type: Schema.Types.Number })
14
+ public total: number;
15
+
16
+ @Prop({ default: () => 0, type: Schema.Types.Number })
17
+ public fee = 0;
18
+
19
+ @Prop({ enum: BillingType, required: true, type: Schema.Types.String })
20
+ public type: BillingType;
21
+
22
+ public readonly order: OrderEntity;
23
+
24
+ public readonly lastStatus: StatusEntity;
25
+ }
@@ -0,0 +1,20 @@
1
+ import { Module, ModuleMetadata } from '@nestjs/common';
2
+ import { UserRepository } from '../../auth/users/user.repository';
3
+ import { PaymentController } from './payment.controller';
4
+ import { PaymentRepository } from './payment.repository';
5
+
6
+ export const metadata: ModuleMetadata = {
7
+ controllers: [PaymentController],
8
+ exports: [PaymentRepository],
9
+ imports: [],
10
+ providers: [PaymentRepository, UserRepository],
11
+ };
12
+
13
+ @Module(metadata)
14
+ export class PaymentModule {
15
+ public static readonly path = '/payments';
16
+
17
+ public static readonly module = PaymentModule;
18
+
19
+ public static readonly children = metadata.imports;
20
+ }
@@ -0,0 +1,57 @@
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 { UserEntity } from '../../auth/users/user.entity';
6
+ import { OrderEntity } from '../orders/order.entity';
7
+ import { StatusEntity } from './entities/status.entity';
8
+ import { PaymentStatus } from './enums/payment.status';
9
+ import { PaymentEntity } from './payment.entity';
10
+ import { PaymentSchema } from './payment.schema';
11
+ import { CreatePaymentValidator, UpdatePaymentValidator } from './payment.validator';
12
+
13
+ @Injectable()
14
+ export class PaymentRepository extends AbstractRepository<PaymentEntity> {
15
+ @InjectModel(PaymentSchema.name)
16
+ protected readonly $model: Model<PaymentEntity>;
17
+
18
+ public async create(validator: CreatePaymentValidator, order: OrderEntity) {
19
+ const payment = new this.$model();
20
+
21
+ const status = new StatusEntity();
22
+ status.date = new Date();
23
+ status.name = PaymentStatus.UNPAID;
24
+ status.user = validator.user as any;
25
+ payment.status.push(status);
26
+
27
+ payment.type = validator.type;
28
+ payment.total = order.total;
29
+
30
+ await payment.save();
31
+
32
+ return payment;
33
+ }
34
+
35
+ public async update(id: string, validator: UpdatePaymentValidator) {
36
+ const payment = await this.read(id);
37
+
38
+ await payment.save();
39
+
40
+ return payment;
41
+ }
42
+
43
+ public async updateStatus(id: string, type: PaymentStatus, user: UserEntity) {
44
+ const payment = await this.read(id);
45
+
46
+ const status = new StatusEntity();
47
+ status.date = new Date();
48
+ status.name = type;
49
+ status.user = user;
50
+
51
+ payment.status.push(status);
52
+
53
+ await payment.save();
54
+
55
+ return payment;
56
+ }
57
+ }
@@ -0,0 +1,14 @@
1
+ import { SchemaFactory } from '@nestjs/mongoose';
2
+ import { PaymentEntity } from './payment.entity';
3
+
4
+ export namespace PaymentSchema {
5
+ export const name = 'sales_payments';
6
+
7
+ export const schema = SchemaFactory.createForClass(PaymentEntity);
8
+
9
+ schema.virtual('lastStatus').get(function () {
10
+ const [first] = this.status.reverse();
11
+
12
+ return first;
13
+ });
14
+ }
@@ -0,0 +1,32 @@
1
+ import { Type } from 'class-transformer';
2
+ import { IsEnum, IsMongoId, IsNotEmptyObject, IsPositive, ValidateNested } from 'class-validator';
3
+ import { IdMongoValidator } from '../../../abstracts/abstract.validator';
4
+ import { PaymentValidator } from '../orders/validators/payment.validator';
5
+ import { BillingType } from './enums/billing.type';
6
+ import { PaymentStatus } from './enums/payment.status';
7
+
8
+ export class CreatePaymentValidator {
9
+ @IsEnum(BillingType)
10
+ public type: BillingType;
11
+
12
+ @IsPositive()
13
+ public total: number;
14
+
15
+ @IsMongoId()
16
+ public order: string;
17
+
18
+ @IsMongoId()
19
+ public user: string;
20
+
21
+ @ValidateNested()
22
+ @Type(() => PaymentValidator)
23
+ @IsNotEmptyObject()
24
+ public payload: PaymentValidator;
25
+ }
26
+
27
+ export class UpdatePaymentValidator {}
28
+
29
+ export class UpdatePaymentStatusValidator extends IdMongoValidator {
30
+ @IsEnum(PaymentStatus)
31
+ public type: PaymentStatus;
32
+ }
@@ -0,0 +1,20 @@
1
+ import { Module, type ModuleMetadata } from '@nestjs/common';
2
+ import { CustomerModule } from './customers/customer.module';
3
+ import { OrderModule } from './orders/order.module';
4
+ import { PaymentModule } from './payments/payment.module';
5
+
6
+ export const metadata: ModuleMetadata = {
7
+ controllers: [],
8
+ exports: [OrderModule, PaymentModule, CustomerModule],
9
+ imports: [OrderModule, PaymentModule, CustomerModule],
10
+ providers: [],
11
+ };
12
+
13
+ @Module(metadata)
14
+ export class SaleModule {
15
+ public static readonly path = '/sale';
16
+
17
+ public static readonly module = SaleModule;
18
+
19
+ public static readonly children = metadata.imports;
20
+ }
@@ -0,0 +1,49 @@
1
+ import { Injectable, OnModuleInit } from '@nestjs/common';
2
+ import type { GetOrganizations200ResponseOneOfInner, ManagementClientOptionsWithClientCredentials } from 'auth0';
3
+ import { ManagementClient } from 'auth0';
4
+ import { AppConfig } from '../app.config';
5
+
6
+ @Injectable()
7
+ export class Auth0Service implements OnModuleInit {
8
+ protected $auth0: ManagementClient;
9
+
10
+ public get organizations() {
11
+ const parseOrg = (org: GetOrganizations200ResponseOneOfInner) => {
12
+ if (!org.metadata) {
13
+ org.metadata = {};
14
+ }
15
+
16
+ for (const key in org.metadata) {
17
+ try {
18
+ org.metadata[key] = JSON.parse(org.metadata[key]);
19
+ } catch (e: unknown) {}
20
+ }
21
+
22
+ return org;
23
+ };
24
+
25
+ return {
26
+ find: async () => {
27
+ const { data } = await this.$auth0.organizations.getAll();
28
+
29
+ return data.map(parseOrg);
30
+ },
31
+ read: async (id: string) => {
32
+ const { data } = await this.$auth0.organizations.get({ id });
33
+
34
+ return parseOrg(data);
35
+ },
36
+ };
37
+ }
38
+
39
+ public async onModuleInit() {
40
+ const options: ManagementClientOptionsWithClientCredentials = {
41
+ audience: AppConfig.AUTH0.audience,
42
+ clientId: AppConfig.AUTH0.client,
43
+ clientSecret: AppConfig.AUTH0.secret,
44
+ domain: AppConfig.AUTH0.domain,
45
+ };
46
+
47
+ this.$auth0 = new ManagementClient(options);
48
+ }
49
+ }
@@ -0,0 +1,52 @@
1
+ import { Inject, Injectable } from '@nestjs/common';
2
+ import { REQUEST } from '@nestjs/core';
3
+ import { MongooseModuleOptions, MongooseOptionsFactory } from '@nestjs/mongoose';
4
+ import { Request } from 'express';
5
+ import { AppConfig } from '../app.config';
6
+ import { MissingIdentityException } from '../exceptions/missing-identity.exception';
7
+ import { Auth0Service } from './auth0.service';
8
+
9
+ @Injectable()
10
+ export class MongoService implements MongooseOptionsFactory {
11
+ @Inject(REQUEST)
12
+ protected readonly $request: Request;
13
+
14
+ @Inject()
15
+ protected readonly $auth0: Auth0Service;
16
+
17
+ public static extractIdentityFromRequest(request: Request) {
18
+ const identity = request.header?.('identity') || request.query['identity'];
19
+
20
+ if (!identity && request.path !== '/auth/resolve') {
21
+ throw new MissingIdentityException();
22
+ }
23
+
24
+ return identity;
25
+ }
26
+
27
+ public async createMongooseOptions(): Promise<MongooseModuleOptions> {
28
+ const { host, pass, user, retryWrites, w } = AppConfig.MONGO;
29
+ const uri = `mongodb+srv://${user}:${pass}@${host}`;
30
+ const [, entrypoint] = process.argv;
31
+
32
+ let DSN: URL;
33
+
34
+ // if is command running
35
+ if (entrypoint.endsWith('console.ts')) {
36
+ const { ORG_ID = 'not_defined_organization' } = process.env;
37
+
38
+ DSN = new URL(`${uri}/${ORG_ID}`);
39
+
40
+ return { uri: DSN.toString() };
41
+ }
42
+
43
+ const identity = MongoService.extractIdentityFromRequest(this.$request);
44
+
45
+ DSN = new URL(`${uri}/${identity}`);
46
+
47
+ DSN.searchParams.set('retryWrites', String(retryWrites));
48
+ DSN.searchParams.set('w', String(w));
49
+
50
+ return { uri: DSN.toString() };
51
+ }
52
+ }
@@ -0,0 +1,20 @@
1
+ import { INestApplication } from '@nestjs/common';
2
+ import { setupApp } from '../main';
3
+
4
+ describe('When payment is requested', () => {
5
+ let app: INestApplication;
6
+
7
+ beforeAll(async () => {
8
+ app = await setupApp();
9
+
10
+ await app.init();
11
+ });
12
+
13
+ afterAll(async () => {
14
+ await app.close();
15
+ });
16
+
17
+ it('If is a lost card', () => {
18
+ console.log('WORK');
19
+ });
20
+ });
package/test/main.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Test } from '@nestjs/testing';
2
+ import { type INestApplication } from '@nestjs/common';
3
+ import { type ModuleMetadata } from '@nestjs/common/interfaces/modules/module-metadata.interface';
4
+ import { AppModule } from '../src/app.module';
5
+ import { createNestApp } from '../app';
6
+
7
+ export async function setupApp(): Promise<INestApplication> {
8
+ const metadata: ModuleMetadata = { imports: [AppModule] };
9
+ const module = await Test.createTestingModule(metadata).compile();
10
+
11
+ return createNestApp(module.createNestApplication());
12
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "declaration": true,
5
+ "removeComments": true,
6
+ "emitDecoratorMetadata": true,
7
+ "experimentalDecorators": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "target": "es2017",
10
+ "sourceMap": true,
11
+ "outDir": "./dist",
12
+ "baseUrl": "./",
13
+ "incremental": true,
14
+ "skipLibCheck": true,
15
+ "strictNullChecks": false,
16
+ "noImplicitAny": false,
17
+ "strictBindCallApply": false,
18
+ "forceConsistentCasingInFileNames": false,
19
+ "noFallthroughCasesInSwitch": false
20
+ }
21
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "declarationMap": false,
6
+ "sourceMap": false,
7
+ "emitDeclarationOnly": false,
8
+ "declarationDir": null,
9
+ "noEmitHelpers": false,
10
+ "outDir": "./.output",
11
+ "emitDecoratorMetadata": true
12
+ },
13
+ "include": [
14
+ "./src/types.ts",
15
+ "./src/**/*.entity.ts",
16
+ "./src/**/*.document.ts",
17
+ "./src/**/*.fixture.ts",
18
+ "./src/**/*.exception.ts",
19
+ "./src/**/*.types.ts",
20
+ "./src/**/*.validator.ts"
21
+ ]
22
+ }
@@ -1,9 +0,0 @@
1
-
2
- export declare abstract class AbstractEntity {
3
- static $index: string;
4
- readonly _id: string;
5
- id: string;
6
- readonly createdAt: Date;
7
- readonly updatedAt?: Date;
8
- readonly deletedAt?: Date;
9
- }
@@ -1,11 +0,0 @@
1
- import { ArgumentsHost, ExceptionFilter, HttpException } from '@nestjs/common';
2
- import { Request, Response } from 'express';
3
- import { LoggerService } from '../services/logger.service';
4
- export declare abstract class AbstractException implements ExceptionFilter {
5
- protected readonly $logger: LoggerService;
6
- protected request: Request;
7
- protected response: Response;
8
- protected host: ArgumentsHost;
9
- catch(exception: HttpException, host: ArgumentsHost): Response<any, Record<string, any>>;
10
- protected abstract handleError(exception: HttpException): HttpException;
11
- }
@@ -1,53 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.AbstractException = void 0;
13
- var common_1 = require("@nestjs/common");
14
- var logger_service_1 = require("../services/logger.service");
15
- var AbstractException = (function () {
16
- function AbstractException() {
17
- }
18
- AbstractException.prototype.catch = function (exception, host) {
19
- this.host = host;
20
- var ctx = this.host.switchToHttp();
21
- this.response = ctx.getResponse();
22
- this.request = ctx.getRequest();
23
- this.$logger.error(exception);
24
- var handledError = this.handleError(exception);
25
- if (handledError instanceof common_1.HttpException) {
26
- var trace = exception.getResponse().message;
27
- this.response.statusMessage = handledError.name
28
- .slice(0, -9)
29
- .replace(/([A-Z])/g, ' $1')
30
- .trim();
31
- return this.response
32
- .status(handledError.getStatus())
33
- .json({
34
- statusCode: handledError.getStatus(),
35
- message: handledError.message,
36
- error: this.response.statusMessage,
37
- trace: trace,
38
- });
39
- }
40
- return this.response
41
- .status(500)
42
- .json(new common_1.InternalServerErrorException(handledError));
43
- };
44
- __decorate([
45
- (0, common_1.Inject)(),
46
- __metadata("design:type", logger_service_1.LoggerService)
47
- ], AbstractException.prototype, "$logger", void 0);
48
- AbstractException = __decorate([
49
- (0, common_1.Injectable)()
50
- ], AbstractException);
51
- return AbstractException;
52
- }());
53
- exports.AbstractException = AbstractException;
@@ -1,4 +0,0 @@
1
- export declare abstract class AbstractFixture {
2
- protected $faker: import("@faker-js/faker").Faker;
3
- abstract normal(): any;
4
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractFixture = void 0;
4
- var faker_1 = require("@faker-js/faker");
5
- var AbstractFixture = (function () {
6
- function AbstractFixture() {
7
- this.$faker = faker_1.faker;
8
- }
9
- return AbstractFixture;
10
- }());
11
- exports.AbstractFixture = AbstractFixture;
@@ -1,20 +0,0 @@
1
- import { FilterQuery, PopulateOptions, SortOrder } from 'mongoose';
2
- import { AbstractEntity } from './abstract.entity';
3
- import 'reflect-metadata';
4
- export interface SearchResults<I> {
5
- page: number;
6
- total: number;
7
- items: I[];
8
- }
9
- export declare abstract class AbstractValidator {
10
- }
11
- export declare type KeyOfType<T, V> = keyof {
12
- [P in keyof T as T[P] extends V ? P : string]: any;
13
- };
14
- export declare class FindValidator<E extends AbstractEntity> extends AbstractValidator {
15
- limit?: number;
16
- page?: number;
17
- sort?: Partial<Record<keyof E, SortOrder>>;
18
- filters?: FilterQuery<E>;
19
- join?: (PopulateOptions | KeyOfType<E, AbstractEntity | AbstractEntity[]>)[];
20
- }