kybernus 3.0.1 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli/commands/ecommerce.d.ts +3 -0
- package/dist/cli/commands/ecommerce.d.ts.map +1 -0
- package/dist/cli/commands/ecommerce.js +164 -0
- package/dist/cli/commands/ecommerce.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/templates/ecommerce/.env.example +10 -0
- package/templates/ecommerce/.github/workflows/ci.yml +102 -0
- package/templates/ecommerce/.github/workflows/deploy.yml +31 -0
- package/templates/ecommerce/.prettierrc +9 -0
- package/templates/ecommerce/Dockerfile +54 -0
- package/templates/ecommerce/README.md +295 -0
- package/templates/ecommerce/apps/api/.env.example +59 -0
- package/templates/ecommerce/apps/api/jest.config.ts +50 -0
- package/templates/ecommerce/apps/api/jest.integration.config.ts +45 -0
- package/templates/ecommerce/apps/api/package.json +59 -0
- package/templates/ecommerce/apps/api/prisma/migrations/20260306000137_init/migration.sql +184 -0
- package/templates/ecommerce/apps/api/prisma/migrations/migration_lock.toml +3 -0
- package/templates/ecommerce/apps/api/prisma/schema.prisma +181 -0
- package/templates/ecommerce/apps/api/prisma/seed.ts +159 -0
- package/templates/ecommerce/apps/api/src/__tests__/app.test.ts +39 -0
- package/templates/ecommerce/apps/api/src/__tests__/globalSetup.ts +34 -0
- package/templates/ecommerce/apps/api/src/__tests__/globalTeardown.ts +16 -0
- package/templates/ecommerce/apps/api/src/__tests__/setup.db.ts +18 -0
- package/templates/ecommerce/apps/api/src/__tests__/setup.env.ts +14 -0
- package/templates/ecommerce/apps/api/src/app.ts +133 -0
- package/templates/ecommerce/apps/api/src/application/admin/admin-user.service.ts +24 -0
- package/templates/ecommerce/apps/api/src/application/admin/dashboard.service.ts +102 -0
- package/templates/ecommerce/apps/api/src/application/auth/auth.service.ts +185 -0
- package/templates/ecommerce/apps/api/src/application/cart/cart.service.ts +151 -0
- package/templates/ecommerce/apps/api/src/application/cart/coupon.service.ts +51 -0
- package/templates/ecommerce/apps/api/src/application/catalog/catalog.service.ts +168 -0
- package/templates/ecommerce/apps/api/src/application/checkout/checkout.service.ts +114 -0
- package/templates/ecommerce/apps/api/src/application/orders/order.service.ts +93 -0
- package/templates/ecommerce/apps/api/src/application/ports/email.port.ts +3 -0
- package/templates/ecommerce/apps/api/src/application/ports/payment.port.ts +24 -0
- package/templates/ecommerce/apps/api/src/application/ports/shipping.port.ts +9 -0
- package/templates/ecommerce/apps/api/src/application/ports/storage.port.ts +3 -0
- package/templates/ecommerce/apps/api/src/application/ports/token-blacklist.port.ts +4 -0
- package/templates/ecommerce/apps/api/src/application/ports/token.port.ts +18 -0
- package/templates/ecommerce/apps/api/src/application/profile/profile.service.ts +76 -0
- package/templates/ecommerce/apps/api/src/domain/auth/user.entity.ts +109 -0
- package/templates/ecommerce/apps/api/src/domain/auth/user.repository.ts +11 -0
- package/templates/ecommerce/apps/api/src/domain/cart/cart.entity.ts +136 -0
- package/templates/ecommerce/apps/api/src/domain/cart/cart.repository.ts +8 -0
- package/templates/ecommerce/apps/api/src/domain/cart/coupon.entity.ts +58 -0
- package/templates/ecommerce/apps/api/src/domain/cart/coupon.repository.ts +10 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/category.entity.ts +51 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/category.repository.ts +10 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/product.entity.ts +130 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/product.repository.ts +28 -0
- package/templates/ecommerce/apps/api/src/domain/checkout/order.entity.ts +121 -0
- package/templates/ecommerce/apps/api/src/domain/checkout/order.repository.ts +11 -0
- package/templates/ecommerce/apps/api/src/domain/shared/AppError.ts +12 -0
- package/templates/ecommerce/apps/api/src/infrastructure/cache/redis.ts +16 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/admin.registry.ts +13 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/auth.registry.ts +34 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/cart.registry.ts +49 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/catalog.registry.ts +24 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/checkout.registry.ts +47 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/orders.registry.ts +6 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/profile.registry.ts +4 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/cart.memory.repository.ts +33 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/category.memory.repository.ts +41 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/coupon.memory.repository.ts +55 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/order.memory.repository.ts +75 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/product.memory.repository.ts +100 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/user.memory.repository.ts +54 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/auth/user.prisma.repository.ts +83 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/catalog/category.prisma.repository.ts +69 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/catalog/product.prisma.repository.ts +185 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/checkout/order.prisma.repository.ts +149 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma-client.ts +17 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/email.registry.ts +18 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/ethereal.email.service.ts +38 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/noop.email.service.ts +12 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/smtp.email.service.ts +36 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/payment/stripe-webhook.handler.ts +83 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/payment/stripe.adapter.ts +39 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/shipping/mock.shipping.service.ts +17 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/in-memory.storage.service.ts +11 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/local-disk.storage.service.ts +27 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/s3.storage.service.ts +52 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/storage.registry.ts +19 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/token/redis.token.blacklist.ts +23 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/token/token.blacklist.ts +30 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/token/token.service.ts +136 -0
- package/templates/ecommerce/apps/api/src/modules/admin/__tests__/admin.routes.integration.test.ts +250 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.controller.ts +116 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.routes.ts +21 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.user.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/auth.logout.redis.test.ts +104 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/auth.routes.integration.test.ts +211 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/auth.service.unit.test.ts +260 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/email.service.unit.test.ts +94 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/token.blacklist.redis.test.ts +65 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/user.entity.unit.test.ts +79 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/user.prisma.repository.test.ts +138 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.controller.ts +148 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/redis.token.blacklist.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/token.blacklist.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/auth/token.service.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/auth/user.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/user.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/user.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/cart.entity.unit.test.ts +144 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/cart.routes.integration.test.ts +242 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/cart.service.unit.test.ts +151 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/coupon.admin.integration.test.ts +136 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.controller.ts +94 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/coupon.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/coupon.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/cart/coupon.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/shipping.service.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/catalog.routes.integration.test.ts +275 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/catalog.service.unit.test.ts +223 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/product.image.integration.test.ts +130 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/product.prisma.repository.test.ts +174 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.controller.ts +176 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.routes.ts +38 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/category.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/category.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/category.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/product.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/product.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/product.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/__tests__/checkout.routes.integration.test.ts +163 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/__tests__/checkout.service.unit.test.ts +191 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/__tests__/order.prisma.repository.test.ts +150 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.controller.ts +59 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.routes.ts +18 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/order.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/order.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/order.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/tax.service.ts +9 -0
- package/templates/ecommerce/apps/api/src/modules/orders/__tests__/order.entity.unit.test.ts +68 -0
- package/templates/ecommerce/apps/api/src/modules/orders/__tests__/order.routes.integration.test.ts +254 -0
- package/templates/ecommerce/apps/api/src/modules/orders/__tests__/order.service.email.unit.test.ts +142 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.controller.ts +96 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/payment/__tests__/stripe-webhook.unit.test.ts +330 -0
- package/templates/ecommerce/apps/api/src/modules/payment/__tests__/stripe.adapter.unit.test.ts +84 -0
- package/templates/ecommerce/apps/api/src/modules/payment/adapters/stripe.adapter.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/payment/payment.port.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/payment/stripe-webhook.handler.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/profile/__tests__/profile.routes.integration.test.ts +180 -0
- package/templates/ecommerce/apps/api/src/modules/profile/__tests__/profile.service.unit.test.ts +187 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.controller.ts +92 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.routes.ts +14 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/presentation/middlewares/authenticate.ts +37 -0
- package/templates/ecommerce/apps/api/src/presentation/middlewares/authorize.ts +23 -0
- package/templates/ecommerce/apps/api/src/presentation/middlewares/errorHandler.ts +48 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/admin/admin.controller.ts +116 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/admin/admin.routes.ts +21 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/auth/auth.controller.ts +147 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/auth/auth.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/cart/cart.controller.ts +94 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/cart/cart.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/catalog/catalog.controller.ts +176 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/catalog/catalog.routes.ts +38 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/checkout/checkout.controller.ts +59 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/checkout/checkout.routes.ts +18 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/orders/order.controller.ts +96 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/orders/order.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/profile/profile.controller.ts +92 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/profile/profile.routes.ts +14 -0
- package/templates/ecommerce/apps/api/src/presentation/validators/uuidParam.ts +20 -0
- package/templates/ecommerce/apps/api/src/server.ts +47 -0
- package/templates/ecommerce/apps/api/src/shared/__tests__/uuid.validation.test.ts +111 -0
- package/templates/ecommerce/apps/api/src/shared/errors/AppError.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/EtherealEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/IEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/NoopEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/SmtpEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/__tests__/ethereal.email.integration.test.ts +32 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/email.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/prisma.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/redis.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/IStorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/InMemoryStorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/LocalDiskStorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/S3StorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/__tests__/s3.storage.unit.test.ts +73 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/storage.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/middlewares/authenticate.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/middlewares/authorize.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/middlewares/errorHandler.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/validators/uuidParam.ts +1 -0
- package/templates/ecommerce/apps/api/tsconfig.json +15 -0
- package/templates/ecommerce/apps/web/.env.example +8 -0
- package/templates/ecommerce/apps/web/index.html +19 -0
- package/templates/ecommerce/apps/web/jest.config.ts +45 -0
- package/templates/ecommerce/apps/web/package.json +38 -0
- package/templates/ecommerce/apps/web/src/App.tsx +133 -0
- package/templates/ecommerce/apps/web/src/__mocks__/fileMock.ts +1 -0
- package/templates/ecommerce/apps/web/src/__mocks__/styleMock.ts +1 -0
- package/templates/ecommerce/apps/web/src/index.css +159 -0
- package/templates/ecommerce/apps/web/src/main.tsx +13 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/CouponsAdminPage.test.tsx +134 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/DashboardPage.test.tsx +65 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/OrdersAdminPage.test.tsx +79 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/ProductsAdminPage.test.tsx +84 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/UsersAdminPage.test.tsx +85 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/CouponsAdminPage.tsx +179 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/DashboardPage.tsx +58 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/OrdersAdminPage.tsx +178 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/ProductsAdminPage.tsx +444 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/UsersAdminPage.tsx +87 -0
- package/templates/ecommerce/apps/web/src/modules/auth/LoginForm.tsx +91 -0
- package/templates/ecommerce/apps/web/src/modules/auth/RegisterForm.tsx +109 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/ForgotPasswordPage.test.tsx +42 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/LoginForm.test.tsx +76 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/RegisterForm.test.tsx +62 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/ResetPasswordPage.test.tsx +66 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/ForgotPasswordPage.tsx +100 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/LoginPage.tsx +39 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/RegisterPage.tsx +39 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/ResetPasswordPage.tsx +110 -0
- package/templates/ecommerce/apps/web/src/modules/auth/useAuthStore.ts +141 -0
- package/templates/ecommerce/apps/web/src/modules/cart/__tests__/CartPage.test.tsx +111 -0
- package/templates/ecommerce/apps/web/src/modules/cart/pages/CartPage.tsx +313 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/__tests__/ProductCard.test.tsx +59 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/__tests__/ProductFilters.test.tsx +56 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/components/ProductCard.tsx +78 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/components/ProductFilters.tsx +104 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/pages/ProductDetailPage.tsx +179 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/pages/ProductListPage.tsx +100 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/__tests__/CheckoutPage.test.tsx +159 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/__tests__/StripePaymentForm.test.tsx +79 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/components/StripePaymentForm.tsx +55 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/hooks/useCheckout.ts +56 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/pages/CheckoutPage.tsx +344 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/pages/CheckoutSuccessPage.tsx +12 -0
- package/templates/ecommerce/apps/web/src/modules/legal/pages/PrivacyPolicyPage.tsx +207 -0
- package/templates/ecommerce/apps/web/src/modules/legal/pages/TermsOfServicePage.tsx +175 -0
- package/templates/ecommerce/apps/web/src/modules/orders/__tests__/OrderDetailPage.test.tsx +75 -0
- package/templates/ecommerce/apps/web/src/modules/orders/__tests__/OrderHistoryPage.test.tsx +87 -0
- package/templates/ecommerce/apps/web/src/modules/orders/pages/OrderDetailPage.tsx +73 -0
- package/templates/ecommerce/apps/web/src/modules/orders/pages/OrderHistoryPage.tsx +97 -0
- package/templates/ecommerce/apps/web/src/modules/profile/__tests__/ProfilePage.test.tsx +150 -0
- package/templates/ecommerce/apps/web/src/modules/profile/pages/ProfilePage.tsx +275 -0
- package/templates/ecommerce/apps/web/src/setupTests.ts +10 -0
- package/templates/ecommerce/apps/web/src/shared/components/CookieConsent.tsx +108 -0
- package/templates/ecommerce/apps/web/src/shared/components/ErrorBoundary.tsx +112 -0
- package/templates/ecommerce/apps/web/src/shared/components/Layout.tsx +143 -0
- package/templates/ecommerce/apps/web/src/shared/components/ProtectedRoute.tsx +21 -0
- package/templates/ecommerce/apps/web/src/shared/config/siteConfig.ts +57 -0
- package/templates/ecommerce/apps/web/src/shared/hooks/usePageTitle.ts +16 -0
- package/templates/ecommerce/apps/web/src/shared/lib/apiFetch.ts +16 -0
- package/templates/ecommerce/apps/web/src/shared/pages/NotFoundPage.tsx +42 -0
- package/templates/ecommerce/apps/web/src/shared/theme/ThemeProvider.tsx +45 -0
- package/templates/ecommerce/apps/web/src/shared/theme/__tests__/ThemeProvider.test.tsx +78 -0
- package/templates/ecommerce/apps/web/src/shared/theme/createTheme.ts +58 -0
- package/templates/ecommerce/apps/web/src/shared/theme/tokens.ts +81 -0
- package/templates/ecommerce/apps/web/src/vite-env.d.ts +1 -0
- package/templates/ecommerce/apps/web/tsconfig.jest.json +12 -0
- package/templates/ecommerce/apps/web/tsconfig.json +25 -0
- package/templates/ecommerce/apps/web/tsconfig.node.json +11 -0
- package/templates/ecommerce/apps/web/vite.config.ts +30 -0
- package/templates/ecommerce/docker-compose.yml +85 -0
- package/templates/ecommerce/package-lock.json +11255 -0
- package/templates/ecommerce/package.json +27 -0
- package/templates/ecommerce/packages/shared-types/package.json +13 -0
- package/templates/ecommerce/packages/shared-types/src/index.ts +3 -0
- package/templates/ecommerce/packages/shared-types/src/theme.ts +44 -0
- package/templates/ecommerce/packages/shared-types/tsconfig.json +11 -0
- package/templates/ecommerce/scripts/customize.sh +201 -0
- package/templates/ecommerce/tsconfig.json +14 -0
- package/templates/java-spring/clean/.gitignore.hbs +72 -0
- package/templates/java-spring/clean/docker-compose.yml.hbs +6 -3
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/application/usecase/PaymentUseCase.java.hbs +21 -17
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/persistence/entity/UserEntity.java.hbs +52 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/persistence/repository/JpaUserRepository.java.hbs +12 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/security/JwtAuthenticationFilter.java.hbs +64 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/security/SecurityConfig.java.hbs +36 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/stripe/StripeGateway.java.hbs +63 -0
- package/templates/java-spring/clean/src/main/resources/application.properties.hbs +6 -7
- package/templates/java-spring/hexagonal/.gitignore.hbs +72 -0
- package/templates/java-spring/hexagonal/docker-compose.yml.hbs +6 -3
- package/templates/java-spring/hexagonal/src/main/java/{{packagePath}}/adapters/outbound/security/JwtFilter.java.hbs +71 -0
- package/templates/java-spring/hexagonal/src/main/java/{{packagePath}}/adapters/outbound/security/SecurityConfig.java.hbs +35 -0
- package/templates/java-spring/hexagonal/src/main/java/{{packagePath}}/core/service/PaymentService.java.hbs +3 -3
- package/templates/java-spring/hexagonal/src/main/resources/application.properties.hbs +4 -4
- package/templates/java-spring/mvc/.gitignore.hbs +72 -0
- package/templates/java-spring/mvc/docker-compose.yml.hbs +6 -3
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/config/SecurityConfig.java.hbs +13 -12
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/AuthController.java.hbs +9 -8
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/PaymentsController.java.hbs +5 -6
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/service/StripeService.java.hbs +3 -3
- package/templates/java-spring/mvc/src/main/resources/application.yml.hbs +29 -26
- package/templates/nestjs/clean/.gitignore.hbs +42 -0
- package/templates/nestjs/clean/Dockerfile.hbs +6 -3
- package/templates/nestjs/clean/docker-compose.yml.hbs +1 -11
- package/templates/nestjs/clean/src/app.module.ts.hbs +2 -1
- package/templates/nestjs/clean/src/application/payment.service.ts.hbs +72 -72
- package/templates/nestjs/clean/src/domain/entities/user.entity.ts.hbs +2 -2
- package/templates/nestjs/clean/src/domain/repositories/user.repository.ts.hbs +2 -2
- package/templates/nestjs/clean/src/infrastructure/database/repositories/prisma.user.repository.ts.hbs +18 -18
- package/templates/nestjs/clean/src/infrastructure/http/health.controller.ts.hbs +9 -0
- package/templates/nestjs/clean/src/main.ts.hbs +1 -4
- package/templates/nestjs/clean/src/payment.module.ts.hbs +12 -12
- package/templates/nestjs/hexagonal/.gitignore.hbs +42 -0
- package/templates/nestjs/hexagonal/Dockerfile.hbs +6 -3
- package/templates/nestjs/hexagonal/docker-compose.yml.hbs +1 -11
- package/templates/nestjs/hexagonal/src/adapters/inbound/health.controller.ts.hbs +9 -0
- package/templates/nestjs/hexagonal/src/app.module.ts.hbs +2 -1
- package/templates/nestjs/hexagonal/src/core/domain/user.entity.ts.hbs +6 -6
- package/templates/nestjs/hexagonal/src/core/ports/ports.ts.hbs +4 -4
- package/templates/nestjs/hexagonal/src/main.ts.hbs +1 -4
- package/templates/nestjs/mvc/.gitignore.hbs +42 -0
- package/templates/nestjs/mvc/Dockerfile.hbs +6 -3
- package/templates/nestjs/mvc/docker-compose.yml.hbs +1 -11
- package/templates/nestjs/mvc/src/auth/auth.controller.ts.hbs +11 -1
- package/templates/nestjs/mvc/src/auth/auth.service.ts.hbs +3 -1
- package/templates/nestjs/mvc/src/controllers/health.controller.ts.hbs +6 -6
- package/templates/nestjs/mvc/src/main.ts.hbs +1 -4
- package/templates/nestjs/mvc/src/models/create-item.dto.ts.hbs +5 -2
- package/templates/nestjs/mvc/src/prisma/prisma.service.ts.hbs +1 -0
- package/templates/nextjs/mvc/.gitignore.hbs +42 -0
- package/templates/nextjs/mvc/Dockerfile.hbs +23 -8
- package/templates/nextjs/mvc/docker-compose.yml.hbs +1 -1
- package/templates/nodejs-express/clean/.gitignore.hbs +42 -0
- package/templates/nodejs-express/clean/Dockerfile.hbs +6 -1
- package/templates/nodejs-express/clean/docker-compose.yml.hbs +2 -2
- package/templates/nodejs-express/clean/package.json.hbs +69 -69
- package/templates/nodejs-express/clean/src/config.ts.hbs +11 -0
- package/templates/nodejs-express/clean/src/domain/entities/User.ts.hbs +46 -8
- package/templates/nodejs-express/hexagonal/.gitignore.hbs +42 -0
- package/templates/nodejs-express/hexagonal/Dockerfile.hbs +1 -1
- package/templates/nodejs-express/hexagonal/docker-compose.yml.hbs +2 -2
- package/templates/nodejs-express/hexagonal/package.json.hbs +69 -69
- package/templates/nodejs-express/hexagonal/src/adapters/inbound/http/PaymentController.ts.hbs +21 -38
- package/templates/nodejs-express/hexagonal/src/adapters/outbound/persistence/prisma.ts.hbs +2 -0
- package/templates/nodejs-express/hexagonal/src/config.ts.hbs +9 -0
- package/templates/nodejs-express/hexagonal/src/core/AuthService.ts.hbs +5 -5
- package/templates/nodejs-express/hexagonal/src/core/PaymentService.ts.hbs +7 -22
- package/templates/nodejs-express/hexagonal/src/core/domain/entities/User.ts.hbs +24 -4
- package/templates/nodejs-express/mvc/.gitignore.hbs +42 -0
- package/templates/nodejs-express/mvc/package.json.hbs +67 -67
- package/templates/python-fastapi/clean/.gitignore.hbs +76 -0
- package/templates/python-fastapi/clean/app/application/services/payment_service.py.hbs +3 -3
- package/templates/python-fastapi/clean/app/config.py.hbs +6 -7
- package/templates/python-fastapi/clean/app/domain/usecases/login_user.py.hbs +15 -0
- package/templates/python-fastapi/clean/app/infrastructure/http/auth_controller.py.hbs +40 -6
- package/templates/python-fastapi/clean/app/infrastructure/http/payment_controller.py.hbs +5 -4
- package/templates/python-fastapi/clean/app/infrastructure/security/jwt.py.hbs +23 -0
- package/templates/python-fastapi/clean/app/main.py.hbs +3 -0
- package/templates/python-fastapi/clean/docker-compose.yml.hbs +5 -12
- package/templates/python-fastapi/clean/requirements.txt.hbs +3 -0
- package/templates/python-fastapi/hexagonal/.gitignore.hbs +76 -0
- package/templates/python-fastapi/hexagonal/app/adapters/inbound/http_adapter.py.hbs +6 -9
- package/templates/python-fastapi/hexagonal/app/adapters/inbound/payment_http_adapter.py.hbs +4 -3
- package/templates/python-fastapi/hexagonal/app/adapters/outbound/stripe_adapter.py.hbs +30 -19
- package/templates/python-fastapi/hexagonal/app/config.py.hbs +14 -4
- package/templates/python-fastapi/hexagonal/app/core/domain/user.py.hbs +3 -1
- package/templates/python-fastapi/hexagonal/app/core/payment_service.py.hbs +28 -18
- package/templates/python-fastapi/hexagonal/app/core/ports/__init__.py.hbs +3 -0
- package/templates/python-fastapi/hexagonal/app/core/ports/user_repository.py.hbs +15 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/database/session.py.hbs +7 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/database/user_repository.py.hbs +53 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/security/__init__.py.hbs +0 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/security/adapters.py.hbs +23 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/security/jwt.py.hbs +23 -0
- package/templates/python-fastapi/hexagonal/docker-compose.yml.hbs +5 -12
- package/templates/python-fastapi/hexagonal/requirements.txt.hbs +4 -0
- package/templates/python-fastapi/mvc/.gitignore.hbs +76 -0
- package/templates/python-fastapi/mvc/app/controllers/payments.py.hbs +3 -17
- package/templates/python-fastapi/mvc/app/middleware/security.py.hbs +24 -3
- package/templates/python-fastapi/mvc/app/schemas/item.py.hbs +3 -1
- package/templates/python-fastapi/mvc/docker-compose.yml.hbs +5 -12
- package/templates/python-fastapi/mvc/requirements.txt.hbs +3 -1
- package/templates/nodejs-express/hexagonal/src/adapters/outbound/persistence/prisma.ts +0 -5
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { AppError } from '../../domain/shared/AppError';
|
|
2
|
+
import { OrderEntity } from '../../domain/checkout/order.entity';
|
|
3
|
+
import { IOrderRepository } from '../../domain/checkout/order.repository';
|
|
4
|
+
import { IPaymentAdapter } from '../ports/payment.port';
|
|
5
|
+
import { ICartRepository } from '../../domain/cart/cart.repository';
|
|
6
|
+
import { IProductRepository } from '../../domain/catalog/product.repository';
|
|
7
|
+
|
|
8
|
+
interface CheckoutDto {
|
|
9
|
+
userId: string;
|
|
10
|
+
shippingCost: number;
|
|
11
|
+
shippingAddress: Record<string, string> | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class CheckoutService {
|
|
15
|
+
constructor(
|
|
16
|
+
private readonly orderRepo: IOrderRepository,
|
|
17
|
+
private readonly paymentAdapter: IPaymentAdapter,
|
|
18
|
+
private readonly cartRepo: ICartRepository,
|
|
19
|
+
private readonly productRepo: IProductRepository,
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
async checkout(dto: CheckoutDto): Promise<{ orderId: string; clientSecret: string }> {
|
|
23
|
+
const { userId, shippingCost, shippingAddress } = dto;
|
|
24
|
+
|
|
25
|
+
// 1. Get cart — throw if empty
|
|
26
|
+
const cart = await this.cartRepo.findByUserId(userId);
|
|
27
|
+
if (!cart || cart.items.length === 0) {
|
|
28
|
+
throw new AppError('Carrinho vazio', 400);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 2. Validate stock for every item and snapshot current levels
|
|
32
|
+
const stockSnapshot: Array<{
|
|
33
|
+
productId: string;
|
|
34
|
+
variantId: string;
|
|
35
|
+
currentStock: number;
|
|
36
|
+
newStock: number;
|
|
37
|
+
}> = [];
|
|
38
|
+
|
|
39
|
+
for (const item of cart.items) {
|
|
40
|
+
const product = await this.productRepo.findById(item.productId);
|
|
41
|
+
if (!product) throw new AppError('Produto não encontrado', 404);
|
|
42
|
+
const variant = product.variants.find((v) => v.id === item.variantId);
|
|
43
|
+
if (!variant) throw new AppError('Variante não encontrada', 404);
|
|
44
|
+
if (variant.stock < item.qty) {
|
|
45
|
+
throw new AppError('Estoque insuficiente', 409);
|
|
46
|
+
}
|
|
47
|
+
stockSnapshot.push({
|
|
48
|
+
productId: item.productId,
|
|
49
|
+
variantId: item.variantId,
|
|
50
|
+
currentStock: variant.stock,
|
|
51
|
+
newStock: variant.stock - item.qty,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 3. Compute totals
|
|
56
|
+
const subtotal = cart.subtotal;
|
|
57
|
+
const discount = cart.discount;
|
|
58
|
+
const tax = 0; // TaxService placeholder — returns 0
|
|
59
|
+
const total = subtotal - discount + shippingCost + tax;
|
|
60
|
+
|
|
61
|
+
// 4. Create Order (PENDING)
|
|
62
|
+
const orderItems = cart.items.map((item) => ({
|
|
63
|
+
id: crypto.randomUUID(),
|
|
64
|
+
variantId: item.variantId,
|
|
65
|
+
productId: item.productId,
|
|
66
|
+
name: item.name,
|
|
67
|
+
sku: item.sku,
|
|
68
|
+
price: item.price,
|
|
69
|
+
qty: item.qty,
|
|
70
|
+
image: item.image,
|
|
71
|
+
}));
|
|
72
|
+
|
|
73
|
+
const order = OrderEntity.create({
|
|
74
|
+
userId,
|
|
75
|
+
items: orderItems,
|
|
76
|
+
subtotal,
|
|
77
|
+
discount,
|
|
78
|
+
shippingCost,
|
|
79
|
+
tax,
|
|
80
|
+
total,
|
|
81
|
+
couponCode: cart.coupon?.code ?? null,
|
|
82
|
+
paymentIntentId: null,
|
|
83
|
+
shippingAddress,
|
|
84
|
+
});
|
|
85
|
+
await this.orderRepo.create(order);
|
|
86
|
+
|
|
87
|
+
// 5. Decrement stock
|
|
88
|
+
for (const snap of stockSnapshot) {
|
|
89
|
+
await this.productRepo.updateVariantStock(snap.productId, snap.variantId, snap.newStock);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 6. Create PaymentIntent — rollback on failure
|
|
93
|
+
try {
|
|
94
|
+
const pi = await this.paymentAdapter.createPaymentIntent(total, 'brl', { orderId: order.id });
|
|
95
|
+
|
|
96
|
+
// Attach paymentIntentId to order
|
|
97
|
+
const updatedOrder = order.withPaymentIntentId(pi.id);
|
|
98
|
+
await this.orderRepo.update(updatedOrder);
|
|
99
|
+
|
|
100
|
+
// 7. Clear cart
|
|
101
|
+
await this.cartRepo.delete({ userId });
|
|
102
|
+
|
|
103
|
+
return { orderId: order.id, clientSecret: pi.clientSecret };
|
|
104
|
+
} catch (err) {
|
|
105
|
+
// Rollback stock to previous levels
|
|
106
|
+
for (const snap of stockSnapshot) {
|
|
107
|
+
await this.productRepo.updateVariantStock(snap.productId, snap.variantId, snap.currentStock);
|
|
108
|
+
}
|
|
109
|
+
// Mark order as failed
|
|
110
|
+
await this.orderRepo.update(order.fail());
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { AppError } from '../../domain/shared/AppError';
|
|
2
|
+
import { OrderEntity } from '../../domain/checkout/order.entity';
|
|
3
|
+
import type { OrderStatus } from '../../domain/checkout/order.entity';
|
|
4
|
+
import { IOrderRepository } from '../../domain/checkout/order.repository';
|
|
5
|
+
import { IEmailService } from '../ports/email.port';
|
|
6
|
+
import { IUserRepository } from '../../domain/auth/user.repository';
|
|
7
|
+
|
|
8
|
+
export class OrderService {
|
|
9
|
+
constructor(
|
|
10
|
+
private readonly orderRepo: IOrderRepository,
|
|
11
|
+
private readonly emailService?: IEmailService,
|
|
12
|
+
private readonly userRepo?: IUserRepository,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
async getOrdersByUser(
|
|
16
|
+
userId: string,
|
|
17
|
+
opts?: { cursor?: string; limit?: number },
|
|
18
|
+
): Promise<{ items: OrderEntity[]; nextCursor: string | null }> {
|
|
19
|
+
return this.orderRepo.findByUserId(userId, opts);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async getOrderById(orderId: string, userId: string): Promise<OrderEntity> {
|
|
23
|
+
const order = await this.orderRepo.findById(orderId);
|
|
24
|
+
if (!order) throw new AppError('Pedido não encontrado', 404);
|
|
25
|
+
if (order.userId !== userId) throw new AppError('Acesso negado', 403);
|
|
26
|
+
return order;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async updateOrderStatus(
|
|
30
|
+
orderId: string,
|
|
31
|
+
newStatus: OrderStatus,
|
|
32
|
+
trackingCode?: string,
|
|
33
|
+
): Promise<OrderEntity> {
|
|
34
|
+
const order = await this.orderRepo.findById(orderId);
|
|
35
|
+
if (!order) throw new AppError('Pedido não encontrado', 404);
|
|
36
|
+
|
|
37
|
+
let updated: OrderEntity;
|
|
38
|
+
switch (newStatus) {
|
|
39
|
+
case 'PAID':
|
|
40
|
+
updated = order.pay();
|
|
41
|
+
break;
|
|
42
|
+
case 'FAILED':
|
|
43
|
+
updated = order.fail();
|
|
44
|
+
break;
|
|
45
|
+
case 'SHIPPED':
|
|
46
|
+
updated = order.ship(trackingCode);
|
|
47
|
+
break;
|
|
48
|
+
case 'DELIVERED':
|
|
49
|
+
updated = order.deliver();
|
|
50
|
+
break;
|
|
51
|
+
case 'CANCELLED':
|
|
52
|
+
updated = order.cancel();
|
|
53
|
+
break;
|
|
54
|
+
default:
|
|
55
|
+
throw new AppError(`Status desconhecido: ${newStatus}`, 400);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const saved = await this.orderRepo.update(updated);
|
|
59
|
+
await this._sendStatusEmail(saved, newStatus);
|
|
60
|
+
return saved;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async _sendStatusEmail(order: OrderEntity, status: OrderStatus): Promise<void> {
|
|
64
|
+
if (!this.emailService || !this.userRepo) return;
|
|
65
|
+
if (status !== 'SHIPPED' && status !== 'DELIVERED') return;
|
|
66
|
+
try {
|
|
67
|
+
const user = await this.userRepo.findById(order.userId);
|
|
68
|
+
if (!user) return;
|
|
69
|
+
if (status === 'SHIPPED') {
|
|
70
|
+
const html = `
|
|
71
|
+
<h2>Seu pedido foi enviado! 🚚</h2>
|
|
72
|
+
<p>Olá ${user.name}, seu pedido <strong>#${order.id.slice(0, 8)}</strong> está a caminho.</p>
|
|
73
|
+
<p>Código de rastreio: <strong>${order.trackingCode ?? '—'}</strong></p>
|
|
74
|
+
`;
|
|
75
|
+
await this.emailService.send(user.email, 'Pedido enviado 🚚', html);
|
|
76
|
+
} else {
|
|
77
|
+
const html = `
|
|
78
|
+
<h2>Pedido entregue! ✅</h2>
|
|
79
|
+
<p>Olá ${user.name}, seu pedido <strong>#${order.id.slice(0, 8)}</strong> foi entregue. Aproveite!</p>
|
|
80
|
+
`;
|
|
81
|
+
await this.emailService.send(user.email, 'Pedido entregue ✅', html);
|
|
82
|
+
}
|
|
83
|
+
} catch {
|
|
84
|
+
// Email failure must never break the order update flow
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async getAllOrders(
|
|
89
|
+
opts?: { status?: OrderStatus; cursor?: string; limit?: number },
|
|
90
|
+
): Promise<{ items: OrderEntity[]; nextCursor: string | null }> {
|
|
91
|
+
return this.orderRepo.findAll(opts);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// ── PaymentError ──────────────────────────────────────────────────────────────
|
|
2
|
+
export class PaymentError extends Error {
|
|
3
|
+
constructor(
|
|
4
|
+
public readonly code: string,
|
|
5
|
+
message: string,
|
|
6
|
+
) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'PaymentError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// ── IPaymentAdapter ───────────────────────────────────────────────────────────
|
|
13
|
+
export interface PaymentIntentResult {
|
|
14
|
+
id: string;
|
|
15
|
+
clientSecret: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IPaymentAdapter {
|
|
19
|
+
createPaymentIntent(
|
|
20
|
+
amount: number,
|
|
21
|
+
currency: string,
|
|
22
|
+
metadata: Record<string, string>,
|
|
23
|
+
): Promise<PaymentIntentResult>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface TokenPayload {
|
|
2
|
+
sub: string;
|
|
3
|
+
email: string;
|
|
4
|
+
role?: string;
|
|
5
|
+
jti?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ITokenService {
|
|
9
|
+
generateAccessToken(payload: TokenPayload): string;
|
|
10
|
+
generateRefreshToken(payload: TokenPayload): string;
|
|
11
|
+
verifyAccessToken(token: string): TokenPayload;
|
|
12
|
+
verifyRefreshToken(token: string): TokenPayload;
|
|
13
|
+
invalidateRefreshToken(token: string): Promise<undefined>;
|
|
14
|
+
isRefreshTokenBlacklisted(token: string): Promise<boolean>;
|
|
15
|
+
generatePasswordResetToken(userId: string): Promise<string>;
|
|
16
|
+
verifyPasswordResetToken(token: string): Promise<string>;
|
|
17
|
+
invalidatePasswordResetToken(token: string): Promise<undefined>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { compare, hash } from 'bcryptjs';
|
|
2
|
+
import { IUserRepository } from '../../domain/auth/user.repository';
|
|
3
|
+
import { PublicUser } from '../../domain/auth/user.entity';
|
|
4
|
+
import { AppError } from '../../domain/shared/AppError';
|
|
5
|
+
|
|
6
|
+
export interface UpdateProfileDto {
|
|
7
|
+
name?: string;
|
|
8
|
+
email?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ChangePasswordDto {
|
|
12
|
+
currentPassword: string;
|
|
13
|
+
newPassword: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const BCRYPT_ROUNDS = 10;
|
|
17
|
+
|
|
18
|
+
export class ProfileService {
|
|
19
|
+
constructor(private readonly userRepository: IUserRepository) {}
|
|
20
|
+
|
|
21
|
+
async getProfile(userId: string): Promise<PublicUser> {
|
|
22
|
+
const user = await this.userRepository.findById(userId);
|
|
23
|
+
if (!user) throw new AppError('Usuário não encontrado', 404);
|
|
24
|
+
return user.toPublic();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async updateProfile(userId: string, dto: UpdateProfileDto): Promise<PublicUser> {
|
|
28
|
+
const user = await this.userRepository.findById(userId);
|
|
29
|
+
if (!user) throw new AppError('Usuário não encontrado', 404);
|
|
30
|
+
|
|
31
|
+
let updated = user;
|
|
32
|
+
|
|
33
|
+
if (dto.name !== undefined) {
|
|
34
|
+
const name = dto.name.trim();
|
|
35
|
+
if (name.length < 2) throw new AppError('Nome deve ter pelo menos 2 caracteres', 400);
|
|
36
|
+
updated = updated.withName(name);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (dto.email !== undefined) {
|
|
40
|
+
const email = dto.email.toLowerCase();
|
|
41
|
+
if (email !== user.email) {
|
|
42
|
+
const existing = await this.userRepository.findByEmail(email);
|
|
43
|
+
if (existing) throw new AppError('Email já está em uso', 409);
|
|
44
|
+
}
|
|
45
|
+
updated = updated.withEmail(email);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const saved = await this.userRepository.update(updated);
|
|
49
|
+
return saved.toPublic();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async changePassword(userId: string, dto: ChangePasswordDto): Promise<void> {
|
|
53
|
+
const user = await this.userRepository.findById(userId);
|
|
54
|
+
if (!user) throw new AppError('Usuário não encontrado', 404);
|
|
55
|
+
|
|
56
|
+
const valid = await compare(dto.currentPassword, user.passwordHash);
|
|
57
|
+
if (!valid) throw new AppError('Senha atual incorreta', 401);
|
|
58
|
+
|
|
59
|
+
if (dto.newPassword.length < 8) {
|
|
60
|
+
throw new AppError('Nova senha deve ter pelo menos 8 caracteres', 400);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const newHash = await hash(dto.newPassword, BCRYPT_ROUNDS);
|
|
64
|
+
await this.userRepository.update(user.withPasswordHash(newHash));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async deleteAccount(userId: string, password: string): Promise<void> {
|
|
68
|
+
const user = await this.userRepository.findById(userId);
|
|
69
|
+
if (!user) throw new AppError('Usuário não encontrado', 404);
|
|
70
|
+
|
|
71
|
+
const valid = await compare(password, user.passwordHash);
|
|
72
|
+
if (!valid) throw new AppError('Senha incorreta', 401);
|
|
73
|
+
|
|
74
|
+
await this.userRepository.delete(userId);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { AppError } from '../shared/AppError';
|
|
4
|
+
|
|
5
|
+
// ── Types ─────────────────────────────────────────────────────────────────────
|
|
6
|
+
export type UserRole = 'CUSTOMER' | 'ADMIN';
|
|
7
|
+
|
|
8
|
+
export interface UserProps {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
email: string;
|
|
12
|
+
passwordHash: string;
|
|
13
|
+
role: UserRole;
|
|
14
|
+
createdAt: Date;
|
|
15
|
+
updatedAt: Date;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CreateUserInput {
|
|
19
|
+
name: string;
|
|
20
|
+
email: string;
|
|
21
|
+
passwordHash: string;
|
|
22
|
+
role?: UserRole;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PublicUser {
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
email: string;
|
|
29
|
+
role: UserRole;
|
|
30
|
+
createdAt: Date;
|
|
31
|
+
// Allow casting to Record<string, unknown> in tests to verify absent keys
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ── Validation schemas ────────────────────────────────────────────────────────
|
|
36
|
+
const emailSchema = z.string().email('Email inválido');
|
|
37
|
+
const nameSchema = z.string().trim().min(1, 'Nome não pode ser vazio');
|
|
38
|
+
|
|
39
|
+
// ── Entity ────────────────────────────────────────────────────────────────────
|
|
40
|
+
export class UserEntity {
|
|
41
|
+
private constructor(private readonly props: UserProps) {}
|
|
42
|
+
|
|
43
|
+
// ── Factories ──────────────────────────────────────────────────────────────
|
|
44
|
+
static create(input: CreateUserInput): UserEntity {
|
|
45
|
+
const emailResult = emailSchema.safeParse(input.email);
|
|
46
|
+
if (!emailResult.success) {
|
|
47
|
+
throw new AppError(emailResult.error.errors[0]?.message ?? 'Email inválido', 422);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const nameResult = nameSchema.safeParse(input.name);
|
|
51
|
+
if (!nameResult.success) {
|
|
52
|
+
throw new AppError(nameResult.error.errors[0]?.message ?? 'Nome inválido', 422);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return new UserEntity({
|
|
56
|
+
id: randomUUID(),
|
|
57
|
+
name: input.name.trim(),
|
|
58
|
+
email: input.email.toLowerCase(),
|
|
59
|
+
passwordHash: input.passwordHash,
|
|
60
|
+
role: input.role ?? 'CUSTOMER',
|
|
61
|
+
createdAt: new Date(),
|
|
62
|
+
updatedAt: new Date(),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static reconstitute(props: UserProps): UserEntity {
|
|
67
|
+
return new UserEntity(props);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ── Getters ────────────────────────────────────────────────────────────────
|
|
71
|
+
get id(): string { return this.props.id; }
|
|
72
|
+
get name(): string { return this.props.name; }
|
|
73
|
+
get email(): string { return this.props.email; }
|
|
74
|
+
get passwordHash(): string { return this.props.passwordHash; }
|
|
75
|
+
get role(): UserRole { return this.props.role; }
|
|
76
|
+
get createdAt(): Date { return this.props.createdAt; }
|
|
77
|
+
get updatedAt(): Date { return this.props.updatedAt; }
|
|
78
|
+
|
|
79
|
+
withPasswordHash(hash: string): UserEntity {
|
|
80
|
+
return new UserEntity({ ...this.props, passwordHash: hash, updatedAt: new Date() });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
withName(name: string): UserEntity {
|
|
84
|
+
return new UserEntity({ ...this.props, name: name.trim(), updatedAt: new Date() });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
withEmail(email: string): UserEntity {
|
|
88
|
+
return new UserEntity({ ...this.props, email: email.toLowerCase(), updatedAt: new Date() });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
withRole(role: UserRole): UserEntity {
|
|
92
|
+
return new UserEntity({ ...this.props, role, updatedAt: new Date() });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ── Projections ────────────────────────────────────────────────────────────
|
|
96
|
+
toPublic(): PublicUser {
|
|
97
|
+
return {
|
|
98
|
+
id: this.props.id,
|
|
99
|
+
name: this.props.name,
|
|
100
|
+
email: this.props.email,
|
|
101
|
+
role: this.props.role,
|
|
102
|
+
createdAt: this.props.createdAt,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
toRecord(): UserProps {
|
|
107
|
+
return { ...this.props };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UserEntity } from './user.entity';
|
|
2
|
+
import type { UserRole } from './user.entity';
|
|
3
|
+
|
|
4
|
+
export interface IUserRepository {
|
|
5
|
+
findByEmail(email: string): Promise<UserEntity | null>;
|
|
6
|
+
findById(id: string): Promise<UserEntity | null>;
|
|
7
|
+
create(user: UserEntity): Promise<UserEntity>;
|
|
8
|
+
update(user: UserEntity): Promise<UserEntity>;
|
|
9
|
+
delete(id: string): Promise<void>;
|
|
10
|
+
findAll(opts?: { role?: UserRole; cursor?: string; limit?: number }): Promise<{ items: UserEntity[]; nextCursor: string | null }>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { AppError } from '../shared/AppError';
|
|
2
|
+
import type { CouponEntity } from './coupon.entity';
|
|
3
|
+
|
|
4
|
+
// ── Types ─────────────────────────────────────────────────────────────────────
|
|
5
|
+
export interface CartItemProps {
|
|
6
|
+
variantId: string;
|
|
7
|
+
productId: string;
|
|
8
|
+
name: string;
|
|
9
|
+
sku: string;
|
|
10
|
+
price: number;
|
|
11
|
+
qty: number;
|
|
12
|
+
image: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface CartProps {
|
|
16
|
+
id: string;
|
|
17
|
+
userId: string | null;
|
|
18
|
+
sessionId: string | null;
|
|
19
|
+
items: CartItemProps[];
|
|
20
|
+
coupon: CouponEntity | null;
|
|
21
|
+
createdAt: Date;
|
|
22
|
+
updatedAt: Date;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ── CartEntity ────────────────────────────────────────────────────────────────
|
|
26
|
+
export class CartEntity {
|
|
27
|
+
private constructor(private readonly props: CartProps) {}
|
|
28
|
+
|
|
29
|
+
// ── Factory ───────────────────────────────────────────────────────────────
|
|
30
|
+
static create(userId: string | null, sessionId?: string | null): CartEntity {
|
|
31
|
+
return new CartEntity({
|
|
32
|
+
id: crypto.randomUUID(),
|
|
33
|
+
userId: userId ?? null,
|
|
34
|
+
sessionId: sessionId ?? null,
|
|
35
|
+
items: [],
|
|
36
|
+
coupon: null,
|
|
37
|
+
createdAt: new Date(),
|
|
38
|
+
updatedAt: new Date(),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static reconstitute(props: CartProps): CartEntity {
|
|
43
|
+
return new CartEntity({ ...props });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ── Getters ───────────────────────────────────────────────────────────────
|
|
47
|
+
get id(): string { return this.props.id; }
|
|
48
|
+
get userId(): string | null { return this.props.userId; }
|
|
49
|
+
get sessionId(): string | null { return this.props.sessionId; }
|
|
50
|
+
get items(): ReadonlyArray<CartItemProps> { return this.props.items; }
|
|
51
|
+
get coupon(): CouponEntity | null { return this.props.coupon; }
|
|
52
|
+
get createdAt(): Date { return this.props.createdAt; }
|
|
53
|
+
|
|
54
|
+
get subtotal(): number {
|
|
55
|
+
return this.props.items.reduce((sum, item) => sum + item.price * item.qty, 0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get discount(): number {
|
|
59
|
+
if (!this.props.coupon) return 0;
|
|
60
|
+
const coupon = this.props.coupon;
|
|
61
|
+
if (coupon.discountType === 'percent') {
|
|
62
|
+
return Math.round(this.subtotal * coupon.discountValue) / 100;
|
|
63
|
+
}
|
|
64
|
+
return Math.min(coupon.discountValue, this.subtotal);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
get total(): number {
|
|
68
|
+
return Math.max(0, this.subtotal - this.discount);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ── Operations (immutable) ────────────────────────────────────────────────
|
|
72
|
+
addItem(item: CartItemProps): CartEntity {
|
|
73
|
+
const existing = this.props.items.find((i) => i.variantId === item.variantId);
|
|
74
|
+
let newItems: CartItemProps[];
|
|
75
|
+
if (existing) {
|
|
76
|
+
newItems = this.props.items.map((i) =>
|
|
77
|
+
i.variantId === item.variantId ? { ...i, qty: i.qty + item.qty } : i,
|
|
78
|
+
);
|
|
79
|
+
} else {
|
|
80
|
+
newItems = [...this.props.items, { ...item }];
|
|
81
|
+
}
|
|
82
|
+
return new CartEntity({ ...this.props, items: newItems, updatedAt: new Date() });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
updateItem(variantId: string, qty: number): CartEntity {
|
|
86
|
+
if (qty <= 0) {
|
|
87
|
+
return this.removeItem(variantId);
|
|
88
|
+
}
|
|
89
|
+
const newItems = this.props.items.map((i) =>
|
|
90
|
+
i.variantId === variantId ? { ...i, qty } : i,
|
|
91
|
+
);
|
|
92
|
+
return new CartEntity({ ...this.props, items: newItems, updatedAt: new Date() });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
removeItem(variantId: string): CartEntity {
|
|
96
|
+
const newItems = this.props.items.filter((i) => i.variantId !== variantId);
|
|
97
|
+
return new CartEntity({ ...this.props, items: newItems, updatedAt: new Date() });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
applyCoupon(coupon: CouponEntity): CartEntity {
|
|
101
|
+
if (coupon.isExpired()) {
|
|
102
|
+
throw new AppError('Cupom expirado', 400);
|
|
103
|
+
}
|
|
104
|
+
if (this.subtotal < coupon.minOrderValue) {
|
|
105
|
+
throw new AppError(
|
|
106
|
+
`Valor mínimo para este cupom é R$${coupon.minOrderValue.toFixed(2)}`,
|
|
107
|
+
400,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return new CartEntity({ ...this.props, coupon, updatedAt: new Date() });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
removeCoupon(): CartEntity {
|
|
114
|
+
return new CartEntity({ ...this.props, coupon: null, updatedAt: new Date() });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
withUserId(userId: string): CartEntity {
|
|
118
|
+
return new CartEntity({ ...this.props, userId, updatedAt: new Date() });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ── Serialization ─────────────────────────────────────────────────────────
|
|
122
|
+
toRecord() {
|
|
123
|
+
return {
|
|
124
|
+
id: this.props.id,
|
|
125
|
+
userId: this.props.userId,
|
|
126
|
+
sessionId: this.props.sessionId,
|
|
127
|
+
items: [...this.props.items],
|
|
128
|
+
couponCode: this.props.coupon?.code ?? null,
|
|
129
|
+
subtotal: this.subtotal,
|
|
130
|
+
discount: this.discount,
|
|
131
|
+
total: this.total,
|
|
132
|
+
createdAt: this.props.createdAt,
|
|
133
|
+
updatedAt: this.props.updatedAt,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CartEntity } from './cart.entity';
|
|
2
|
+
|
|
3
|
+
export interface ICartRepository {
|
|
4
|
+
findByUserId(userId: string): Promise<CartEntity | null>;
|
|
5
|
+
findBySessionId(sessionId: string): Promise<CartEntity | null>;
|
|
6
|
+
save(cart: CartEntity): Promise<CartEntity>;
|
|
7
|
+
delete(key: { userId?: string; sessionId?: string }): Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// ── Types ─────────────────────────────────────────────────────────────────────
|
|
2
|
+
export type DiscountType = 'percent' | 'fixed';
|
|
3
|
+
|
|
4
|
+
export interface CouponProps {
|
|
5
|
+
id: string;
|
|
6
|
+
code: string;
|
|
7
|
+
discountType: DiscountType;
|
|
8
|
+
discountValue: number;
|
|
9
|
+
minOrderValue: number;
|
|
10
|
+
usageLimit: number | null;
|
|
11
|
+
usageCount: number;
|
|
12
|
+
expiresAt: Date | null;
|
|
13
|
+
createdAt: Date;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ── CouponEntity ──────────────────────────────────────────────────────────────
|
|
17
|
+
export class CouponEntity {
|
|
18
|
+
private constructor(private readonly props: CouponProps) {}
|
|
19
|
+
|
|
20
|
+
static create(input: Omit<CouponProps, 'id' | 'usageCount' | 'createdAt'>): CouponEntity {
|
|
21
|
+
return new CouponEntity({
|
|
22
|
+
...input,
|
|
23
|
+
id: crypto.randomUUID(),
|
|
24
|
+
usageCount: 0,
|
|
25
|
+
createdAt: new Date(),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static reconstitute(props: CouponProps): CouponEntity {
|
|
30
|
+
return new CouponEntity({ ...props });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── Getters ───────────────────────────────────────────────────────────────
|
|
34
|
+
get id(): string { return this.props.id; }
|
|
35
|
+
get code(): string { return this.props.code; }
|
|
36
|
+
get discountType(): DiscountType { return this.props.discountType; }
|
|
37
|
+
get discountValue(): number { return this.props.discountValue; }
|
|
38
|
+
get minOrderValue(): number { return this.props.minOrderValue; }
|
|
39
|
+
get usageLimit(): number | null { return this.props.usageLimit; }
|
|
40
|
+
get usageCount(): number { return this.props.usageCount; }
|
|
41
|
+
get expiresAt(): Date | null { return this.props.expiresAt; }
|
|
42
|
+
|
|
43
|
+
// ── Business rules ────────────────────────────────────────────────────────
|
|
44
|
+
isExpired(): boolean {
|
|
45
|
+
if (!this.props.expiresAt) return false;
|
|
46
|
+
return this.props.expiresAt < new Date();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
isUsageLimitReached(): boolean {
|
|
50
|
+
if (this.props.usageLimit === null) return false;
|
|
51
|
+
return this.props.usageCount >= this.props.usageLimit;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ── Serialization ─────────────────────────────────────────────────────────
|
|
55
|
+
toRecord(): CouponProps {
|
|
56
|
+
return { ...this.props };
|
|
57
|
+
}
|
|
58
|
+
}
|