forjajs 5.0.1 → 5.3.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/CHANGELOG.md +114 -2
- package/README.md +40 -5
- package/README.pt-BR.md +42 -4
- package/boilerplates/02-saas-starter/README.md +58 -293
- package/boilerplates/02-saas-starter/backend/.env.example +13 -0
- package/boilerplates/02-saas-starter/backend/.oxlintrc.json +8 -0
- package/boilerplates/02-saas-starter/backend/nest-cli.json +6 -0
- package/boilerplates/02-saas-starter/backend/package.json +32 -73
- package/boilerplates/02-saas-starter/backend/src/app.module.ts +16 -16
- package/boilerplates/02-saas-starter/backend/src/billing/adapters/http/billing.controller.ts +55 -0
- package/boilerplates/02-saas-starter/backend/src/billing/adapters/persistence/orm-entities.ts +28 -0
- package/boilerplates/02-saas-starter/backend/src/billing/adapters/persistence/typeorm-billing.repository.ts +46 -0
- package/boilerplates/02-saas-starter/backend/src/billing/application/billing.service.spec.ts +58 -0
- package/boilerplates/02-saas-starter/backend/src/billing/application/billing.service.ts +68 -0
- package/boilerplates/02-saas-starter/backend/src/billing/billing.module.ts +11 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/plan.spec.ts +23 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/plan.ts +36 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/ports.ts +27 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/subscription.spec.ts +28 -0
- package/boilerplates/02-saas-starter/backend/src/billing/domain/subscription.ts +47 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/http/auth.dto.ts +22 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/http/identity.controller.ts +59 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/persistence/orm-entities.ts +25 -0
- package/boilerplates/02-saas-starter/backend/src/identity/adapters/persistence/typeorm-identity.repository.ts +47 -0
- package/boilerplates/02-saas-starter/backend/src/identity/application/auth.service.spec.ts +72 -0
- package/boilerplates/02-saas-starter/backend/src/identity/application/auth.service.ts +63 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/model.spec.ts +19 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/model.ts +42 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/password.spec.ts +17 -0
- package/boilerplates/02-saas-starter/backend/src/identity/domain/password.ts +26 -0
- package/boilerplates/02-saas-starter/backend/src/identity/identity.module.ts +12 -0
- package/boilerplates/02-saas-starter/backend/src/main.ts +19 -19
- package/boilerplates/02-saas-starter/backend/src/platform/auth/jwt-auth.guard.ts +17 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/principal.ts +22 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/roles.guard.spec.ts +23 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/roles.guard.ts +20 -0
- package/boilerplates/02-saas-starter/backend/src/platform/auth/token.service.ts +54 -0
- package/boilerplates/02-saas-starter/backend/src/platform/config.spec.ts +18 -0
- package/boilerplates/02-saas-starter/backend/src/platform/config.ts +33 -0
- package/boilerplates/02-saas-starter/backend/src/platform/platform.module.ts +14 -0
- package/boilerplates/02-saas-starter/backend/tsconfig.build.json +6 -0
- package/boilerplates/02-saas-starter/backend/tsconfig.json +16 -21
- package/boilerplates/02-saas-starter/backend/vitest.config.ts +7 -0
- package/boilerplates/02-saas-starter/boilerplate.manifest.json +19 -14
- package/boilerplates/02-saas-starter/docker-compose.yml +10 -24
- package/boilerplates/02-saas-starter/memory/30-domains/billing/context.md +15 -132
- package/boilerplates/02-saas-starter/memory/30-domains/identity/context.md +20 -0
- package/boilerplates/README.md +9 -10
- package/dist/apps/cli/src/index.d.ts +0 -1
- package/dist/apps/cli/src/index.js +43 -32
- package/dist/apps/dashboard/app/api/forja/[...path]/route.js +34 -1
- package/dist/apps/dashboard/app/api/forja/capabilities.d.ts +26 -0
- package/dist/apps/dashboard/app/api/forja/capabilities.js +39 -0
- package/dist/apps/dashboard/app/dashboard-client.jsx +607 -42
- package/dist/apps/server/src/forja-nest.module.d.ts +6 -0
- package/dist/apps/server/src/forja-nest.module.js +55 -0
- package/dist/apps/server/src/main.d.ts +21 -4
- package/dist/apps/server/src/main.js +79 -57
- package/dist/apps/server/src/task-planning-service.d.ts +29 -0
- package/dist/apps/server/src/task-planning-service.js +195 -0
- package/dist/apps/server/src/task-runtime-coordinator.d.ts +57 -0
- package/dist/apps/server/src/task-runtime-coordinator.js +229 -0
- package/dist/bin/forja.js +3 -2
- package/dist/bin/init-project.js +1 -0
- package/dist/lib/agent-evals.d.ts +138 -0
- package/dist/lib/agent-evals.js +349 -0
- package/dist/lib/cli-output.js +14 -2
- package/dist/lib/code-context.d.ts +33 -5
- package/dist/lib/code-context.js +70 -5
- package/dist/lib/context-builder.d.ts +49 -4
- package/dist/lib/context-builder.js +114 -47
- package/dist/lib/core/coverage-gate.d.ts +59 -0
- package/dist/lib/core/coverage-gate.js +58 -0
- package/dist/lib/core/doc-graph.d.ts +38 -0
- package/dist/lib/core/doc-graph.js +83 -0
- package/dist/lib/core/health.js +286 -5
- package/dist/lib/core/journey.js +36 -0
- package/dist/lib/core/registry.js +81 -6
- package/dist/lib/docs-reference.d.ts +35 -0
- package/dist/lib/docs-reference.js +57 -0
- package/dist/lib/generators/memory-generator.d.ts +0 -11
- package/dist/lib/generators/memory-generator.js +5 -161
- package/dist/lib/generators/readme-generator.js +1 -1
- package/dist/lib/llm/json-output.d.ts +14 -0
- package/dist/lib/llm/json-output.js +77 -0
- package/dist/lib/project-wiring.d.ts +8 -0
- package/dist/lib/project-wiring.js +79 -3
- package/dist/lib/session-memory.d.ts +38 -0
- package/dist/lib/session-memory.js +206 -0
- package/dist/lib/spec-trace.d.ts +46 -0
- package/dist/lib/spec-trace.js +184 -0
- package/dist/lib/validators/structure-validator.js +0 -1
- package/dist/packages/adapter-nest/src/index.d.ts +2 -1
- package/dist/packages/adapter-nest/src/index.js +59 -1
- package/dist/packages/adapter-sqlite/src/index.d.ts +15 -1
- package/dist/packages/adapter-sqlite/src/index.js +61 -1
- package/dist/packages/context/src/index.d.ts +2 -0
- package/dist/packages/context/src/index.js +36 -15
- package/dist/packages/contracts/src/index.d.ts +64 -0
- package/dist/packages/core/src/index.d.ts +9 -1
- package/dist/packages/core/src/index.js +28 -3
- package/dist/packages/graph/src/index.js +14 -5
- package/dist/packages/llm/src/index.js +12 -2
- package/dist/packages/mcp/src/index.d.ts +8 -0
- package/dist/packages/mcp/src/index.js +12 -1
- package/dist/packages/observability/src/index.d.ts +11 -0
- package/dist/packages/observability/src/index.js +3 -0
- package/dist/packages/orchestration/src/index.d.ts +15 -1
- package/dist/packages/orchestration/src/index.js +127 -21
- package/dist/packages/planner/src/index.d.ts +7 -1
- package/dist/packages/planner/src/index.js +67 -10
- package/dist/packages/planner/src/proposal-store.d.ts +30 -0
- package/dist/packages/planner/src/proposal-store.js +109 -0
- package/dist/packages/policy/src/index.d.ts +9 -16
- package/dist/packages/policy/src/index.js +114 -31
- package/dist/packages/runtime/src/index.d.ts +16 -2
- package/dist/packages/runtime/src/index.js +176 -61
- package/dist/scripts/build-smart-context.d.ts +2 -0
- package/dist/scripts/build-smart-context.js +12 -3
- package/dist/scripts/code-context.d.ts +3 -1
- package/dist/scripts/code-context.js +13 -3
- package/dist/scripts/coverage-gate.d.ts +2 -0
- package/dist/scripts/coverage-gate.js +101 -0
- package/dist/scripts/eval-agents.d.ts +11 -0
- package/dist/scripts/eval-agents.js +96 -0
- package/dist/scripts/gen-commands-doc.d.ts +2 -0
- package/dist/scripts/gen-commands-doc.js +18 -0
- package/dist/scripts/hook-session-memory.d.ts +9 -0
- package/dist/scripts/hook-session-memory.js +20 -0
- package/dist/scripts/hook-session-start.js +34 -0
- package/dist/scripts/llm-fit.js +23 -1
- package/dist/scripts/spec-trace.d.ts +10 -0
- package/dist/scripts/spec-trace.js +73 -0
- package/dist/scripts/sync-universal-memory.js +69 -4
- package/dist/scripts/token-benchmark.d.ts +33 -1
- package/dist/scripts/token-benchmark.js +179 -5
- package/dist/scripts/token-economy.js +3 -1
- package/dist/scripts/tools-doctor.d.ts +16 -1
- package/dist/scripts/tools-doctor.js +58 -17
- package/docs/commands.md +155 -0
- package/docs/fluxo.md +11 -7
- package/docs/init-project.md +3 -1
- package/docs/llm-fit-loop.md +36 -0
- package/docs/manual-operacional-cli-sdd-gsd.md +3 -2
- package/docs/produto/factsheet-marketing.md +7 -5
- package/docs/produto/roteiro-demo.md +14 -5
- package/docs/quick-reference.md +3 -3
- package/docs/token-optimization.md +10 -6
- package/memory/90-decisions/0088-templates-validados.md +3 -0
- package/memory/90-decisions/0089-skills-do-projeto.md +51 -0
- package/memory/90-decisions/0090-adapters-de-agente-somente-leitura.md +47 -0
- package/memory/90-decisions/0091-memoria-de-sessao-por-hooks.md +40 -0
- package/memory/90-decisions/0092-criterios-de-aceite-ligados-a-testes.md +37 -0
- package/memory/90-decisions/0093-avaliacoes-com-agentes-reais.md +40 -0
- package/package.json +4 -2
- package/specs/avaliacoes-de-agentes/spec.md +52 -0
- package/specs/criterios-ligados-a-testes/spec.md +42 -0
- package/specs/memoria-de-sessao/spec.md +48 -0
- package/specs/skills-do-projeto/plan.md +40 -0
- package/specs/skills-do-projeto/spec.md +63 -0
- package/specs/skills-do-projeto/tasks.md +27 -0
- package/specs/v6-agent-runtime-portable/spec.md +83 -0
- package/specs/v6-core-consolidation/spec.md +70 -0
- package/specs/v6-docs-drift/spec.md +85 -0
- package/specs/v6-quality-gates/spec.md +99 -0
- package/specs/v6-token-economy/spec.md +84 -0
- package/templates/evals/agents/adr.json +25 -0
- package/templates/evals/agents/memoria.json +32 -0
- package/templates/evals/agents/proxima-acao.json +29 -0
- package/templates/evals/agents/spec-nova.json +26 -0
- package/templates/evals/agents/teste-do-criterio.json +29 -0
- package/templates/project/AGENTS.md +16 -0
- package/templates/project/CLAUDE.md +3 -2
- package/templates/project/agents/context-engineer.md +3 -0
- package/templates/project/agents/governance.md +3 -0
- package/templates/project/agents/marketing.md +3 -0
- package/templates/project/agents/orchestrator.md +3 -0
- package/templates/project/agents/product.md +3 -0
- package/templates/project/agents/sdd-architect.md +3 -0
- package/templates/project/skills/README.md +14 -0
- package/templates/project/skills/accessibility-review/SKILL.md +89 -0
- package/templates/project/skills/api-design/SKILL.md +77 -0
- package/templates/project/skills/architecture-decision-record/SKILL.md +92 -0
- package/templates/project/skills/backlog-slicing/SKILL.md +82 -0
- package/templates/project/skills/bounded-context-mapping/SKILL.md +88 -0
- package/templates/project/skills/ci-pipeline-review/SKILL.md +83 -0
- package/templates/project/skills/commit-message/SKILL.md +143 -0
- package/templates/project/skills/data-migrations/SKILL.md +88 -0
- package/templates/project/skills/dependency-upgrade/SKILL.md +75 -0
- package/templates/project/skills/docker-compose-review/SKILL.md +85 -0
- package/templates/project/skills/domain-modeling/SKILL.md +120 -0
- package/templates/project/skills/event-storming/SKILL.md +95 -0
- package/templates/project/skills/executive-writing/SKILL.md +99 -0
- package/templates/project/skills/frontend-architecture/SKILL.md +97 -0
- package/templates/project/skills/incident-postmortem/SKILL.md +93 -0
- package/templates/project/skills/lgpd-compliance/SKILL.md +108 -0
- package/templates/project/skills/llm-provider-routing/SKILL.md +18 -0
- package/templates/project/skills/observability/SKILL.md +93 -0
- package/templates/project/skills/product-evaluation/SKILL.md +130 -0
- package/templates/project/skills/prompt-spec/SKILL.md +83 -0
- package/templates/project/skills/pull-request/SKILL.md +101 -0
- package/templates/project/skills/query-performance/SKILL.md +83 -0
- package/templates/project/skills/release-checklist/SKILL.md +106 -0
- package/templates/project/skills/release-notes/SKILL.md +87 -0
- package/templates/project/skills/requirements-elicitation/SKILL.md +127 -0
- package/templates/project/skills/requirements-gathering/SKILL.md +134 -0
- package/templates/project/skills/technical-spike/SKILL.md +101 -0
- package/templates/project/skills/testing/SKILL.md +109 -0
- package/templates/project/skills/threat-modeling/SKILL.md +87 -0
- package/templates/project/skills/ts-async-patterns/SKILL.md +101 -0
- package/templates/project/skills/ts-code-review/SKILL.md +121 -0
- package/templates/project/skills/ts-module-layout/SKILL.md +104 -0
- package/templates/project/skills/ts-testing/SKILL.md +89 -0
- package/templates/project/skills/web-performance/SKILL.md +72 -0
- package/boilerplates/02-saas-starter/.env.example +0 -25
- package/boilerplates/02-saas-starter/CHECKLIST-CRIACAO.md +0 -190
- package/boilerplates/02-saas-starter/INFO.md +0 -252
- package/boilerplates/02-saas-starter/QUICK_START.md +0 -194
- package/boilerplates/02-saas-starter/START-HERE.md +0 -259
- package/boilerplates/02-saas-starter/backend/src/common/decorators/roles.decorator.ts +0 -3
- package/boilerplates/02-saas-starter/backend/src/common/decorators/tenant.decorator.ts +0 -6
- package/boilerplates/02-saas-starter/backend/src/common/middleware/roles.guard.ts +0 -23
- package/boilerplates/02-saas-starter/backend/src/config/database.ts +0 -26
- package/boilerplates/02-saas-starter/backend/src/modules/auth/auth.controller.ts +0 -46
- package/boilerplates/02-saas-starter/backend/src/modules/auth/auth.module.ts +0 -25
- package/boilerplates/02-saas-starter/backend/src/modules/auth/auth.service.ts +0 -145
- package/boilerplates/02-saas-starter/backend/src/modules/auth/dto/auth.dto.ts +0 -30
- package/boilerplates/02-saas-starter/backend/src/modules/auth/strategies/jwt.strategy.ts +0 -24
- package/boilerplates/02-saas-starter/backend/src/modules/auth/strategies/local.strategy.ts +0 -19
- package/boilerplates/02-saas-starter/backend/src/modules/billing/billing.controller.ts +0 -42
- package/boilerplates/02-saas-starter/backend/src/modules/billing/billing.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/billing/billing.service.ts +0 -67
- package/boilerplates/02-saas-starter/backend/src/modules/billing/entities/invoice.entity.ts +0 -52
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/entities/organization.entity.ts +0 -36
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.controller.ts +0 -16
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.service.ts +0 -34
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/entities/subscription.entity.ts +0 -40
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.controller.ts +0 -36
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.service.ts +0 -91
- package/boilerplates/02-saas-starter/backend/src/modules/users/entities/user.entity.ts +0 -42
- package/boilerplates/02-saas-starter/backend/src/modules/users/users.controller.ts +0 -14
- package/boilerplates/02-saas-starter/backend/src/modules/users/users.module.ts +0 -13
- package/boilerplates/02-saas-starter/backend/src/modules/users/users.service.ts +0 -48
- package/boilerplates/02-saas-starter/memory/00-global/agent-contract.md +0 -227
- package/boilerplates/02-saas-starter/memory/00-global/context-policy.md +0 -141
- package/boilerplates/02-saas-starter/memory/00-global/index.md +0 -133
- package/boilerplates/02-saas-starter/memory/00-global/mission.md +0 -45
- package/boilerplates/02-saas-starter/memory/00-global/patterns.md +0 -223
- package/boilerplates/02-saas-starter/memory/10-product/business-rules.md +0 -240
- package/boilerplates/02-saas-starter/memory/10-product/nfrs.md +0 -239
- package/boilerplates/02-saas-starter/memory/10-product/personas.md +0 -195
- package/boilerplates/02-saas-starter/memory/10-product/vision.md +0 -63
- package/boilerplates/02-saas-starter/memory/20-architecture/overview.md +0 -232
- package/boilerplates/02-saas-starter/memory/30-domains/auth/api.md +0 -186
- package/boilerplates/02-saas-starter/memory/30-domains/auth/context.md +0 -85
- package/boilerplates/02-saas-starter/memory/30-domains/billing/api.md +0 -262
- package/boilerplates/02-saas-starter/memory/30-domains/subscriptions/api.md +0 -176
- package/boilerplates/02-saas-starter/memory/30-domains/subscriptions/context.md +0 -100
- package/boilerplates/02-saas-starter/memory/40-delivery/backlog.md +0 -81
- package/boilerplates/02-saas-starter/memory/40-delivery/roadmap.md +0 -97
- package/boilerplates/02-saas-starter/memory/40-delivery/sprint-atual.md +0 -57
- package/boilerplates/02-saas-starter/memory/50-orchestration/topology.md +0 -90
- package/boilerplates/02-saas-starter/memory/60-runs/index.md +0 -54
- package/boilerplates/02-saas-starter/memory/70-summaries/global.md +0 -50
- package/boilerplates/02-saas-starter/memory/80-data/schema.md +0 -185
- package/boilerplates/02-saas-starter/memory/90-decisions/adr-template.md +0 -75
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { Injectable, BadRequestException, UnauthorizedException } from '@nestjs/common';
|
|
2
|
-
import { JwtService } from '@nestjs/jwt';
|
|
3
|
-
import { InjectRepository } from '@nestjs/typeorm';
|
|
4
|
-
import { Repository } from 'typeorm';
|
|
5
|
-
import * as bcrypt from 'bcrypt';
|
|
6
|
-
import { v4 as uuid } from 'uuid';
|
|
7
|
-
import { User } from '../users/entities/user.entity';
|
|
8
|
-
import { Organization } from '../organizations/entities/organization.entity';
|
|
9
|
-
import { SignupDto, LoginDto } from './dto/auth.dto';
|
|
10
|
-
|
|
11
|
-
@Injectable()
|
|
12
|
-
export class AuthService {
|
|
13
|
-
constructor(
|
|
14
|
-
@InjectRepository(User)
|
|
15
|
-
private userRepository: Repository<User>,
|
|
16
|
-
@InjectRepository(Organization)
|
|
17
|
-
private orgRepository: Repository<Organization>,
|
|
18
|
-
private jwtService: JwtService,
|
|
19
|
-
) {}
|
|
20
|
-
|
|
21
|
-
async signup(signupDto: SignupDto) {
|
|
22
|
-
const existingUser = await this.userRepository.findOne({
|
|
23
|
-
where: { email: signupDto.email },
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
if (existingUser) {
|
|
27
|
-
throw new BadRequestException('Email already exists');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const tenantId = uuid();
|
|
31
|
-
const passwordHash = await bcrypt.hash(signupDto.password, 12);
|
|
32
|
-
|
|
33
|
-
const user = this.userRepository.create({
|
|
34
|
-
tenantId,
|
|
35
|
-
email: signupDto.email,
|
|
36
|
-
passwordHash,
|
|
37
|
-
roles: ['owner'],
|
|
38
|
-
status: 'active',
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
await this.userRepository.save(user);
|
|
42
|
-
|
|
43
|
-
const org = this.orgRepository.create({
|
|
44
|
-
tenantId,
|
|
45
|
-
name: signupDto.organizationName,
|
|
46
|
-
slug: signupDto.organizationName.toLowerCase().replace(/\s+/g, '-'),
|
|
47
|
-
ownerId: user.id,
|
|
48
|
-
plan: 'free',
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
await this.orgRepository.save(org);
|
|
52
|
-
|
|
53
|
-
const tokens = this.generateTokens(user);
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
user: {
|
|
57
|
-
id: user.id,
|
|
58
|
-
email: user.email,
|
|
59
|
-
roles: user.roles,
|
|
60
|
-
},
|
|
61
|
-
organization: {
|
|
62
|
-
id: org.id,
|
|
63
|
-
name: org.name,
|
|
64
|
-
plan: org.plan,
|
|
65
|
-
},
|
|
66
|
-
...tokens,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async validateUser(email: string, password: string) {
|
|
71
|
-
const user = await this.userRepository.findOne({ where: { email } });
|
|
72
|
-
|
|
73
|
-
if (!user) {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const passwordMatch = await bcrypt.compare(password, user.passwordHash);
|
|
78
|
-
|
|
79
|
-
if (!passwordMatch) {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return user;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async login(email: string, password: string) {
|
|
87
|
-
const user = await this.validateUser(email, password);
|
|
88
|
-
|
|
89
|
-
if (!user) {
|
|
90
|
-
throw new UnauthorizedException('Invalid credentials');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const org = await this.orgRepository.findOne({
|
|
94
|
-
where: { tenantId: user.tenantId },
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
const tokens = this.generateTokens(user);
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
user: {
|
|
101
|
-
id: user.id,
|
|
102
|
-
email: user.email,
|
|
103
|
-
roles: user.roles,
|
|
104
|
-
},
|
|
105
|
-
organization: {
|
|
106
|
-
id: org.id,
|
|
107
|
-
name: org.name,
|
|
108
|
-
plan: org.plan,
|
|
109
|
-
},
|
|
110
|
-
...tokens,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private generateTokens(user: User) {
|
|
115
|
-
const payload = {
|
|
116
|
-
sub: user.id,
|
|
117
|
-
email: user.email,
|
|
118
|
-
tenantId: user.tenantId,
|
|
119
|
-
roles: user.roles,
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const accessToken = this.jwtService.sign(payload, {
|
|
123
|
-
expiresIn: '15m',
|
|
124
|
-
secret: process.env.JWT_SECRET || 'dev-secret-key',
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
const refreshToken = this.jwtService.sign(payload, {
|
|
128
|
-
expiresIn: '7d',
|
|
129
|
-
secret: process.env.JWT_REFRESH_SECRET || 'dev-refresh-secret',
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
return { accessToken, refreshToken };
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
verifyRefreshToken(token: string) {
|
|
136
|
-
try {
|
|
137
|
-
const payload = this.jwtService.verify(token, {
|
|
138
|
-
secret: process.env.JWT_REFRESH_SECRET || 'dev-refresh-secret',
|
|
139
|
-
});
|
|
140
|
-
return payload;
|
|
141
|
-
} catch (e) {
|
|
142
|
-
throw new UnauthorizedException('Invalid refresh token');
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { IsString, IsEmail, MinLength, Matches, IsNotEmpty } from 'class-validator';
|
|
2
|
-
|
|
3
|
-
export class SignupDto {
|
|
4
|
-
@IsEmail()
|
|
5
|
-
email: string;
|
|
6
|
-
|
|
7
|
-
@IsString()
|
|
8
|
-
@MinLength(12)
|
|
9
|
-
@Matches(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/, {
|
|
10
|
-
message: 'Password must contain uppercase, lowercase, number and symbol',
|
|
11
|
-
})
|
|
12
|
-
password: string;
|
|
13
|
-
|
|
14
|
-
@IsString()
|
|
15
|
-
@IsNotEmpty()
|
|
16
|
-
organizationName: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class LoginDto {
|
|
20
|
-
@IsEmail()
|
|
21
|
-
email: string;
|
|
22
|
-
|
|
23
|
-
@IsString()
|
|
24
|
-
password: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class RefreshTokenDto {
|
|
28
|
-
@IsString()
|
|
29
|
-
refreshToken: string;
|
|
30
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { ConfigService } from '@nestjs/config';
|
|
3
|
-
import { PassportStrategy } from '@nestjs/passport';
|
|
4
|
-
import { ExtractJwt, Strategy } from 'passport-jwt';
|
|
5
|
-
|
|
6
|
-
@Injectable()
|
|
7
|
-
export class JwtStrategy extends PassportStrategy(Strategy) {
|
|
8
|
-
constructor(configService: ConfigService) {
|
|
9
|
-
super({
|
|
10
|
-
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
11
|
-
ignoreExpiration: false,
|
|
12
|
-
secretOrKey: configService.get('JWT_SECRET', 'dev-secret-key'),
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
validate(payload: any) {
|
|
17
|
-
return {
|
|
18
|
-
id: payload.sub,
|
|
19
|
-
email: payload.email,
|
|
20
|
-
tenantId: payload.tenantId,
|
|
21
|
-
roles: payload.roles || ['member'],
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { PassportStrategy } from '@nestjs/passport';
|
|
3
|
-
import { Strategy } from 'passport-local';
|
|
4
|
-
import { AuthService } from '../auth.service';
|
|
5
|
-
|
|
6
|
-
@Injectable()
|
|
7
|
-
export class LocalStrategy extends PassportStrategy(Strategy) {
|
|
8
|
-
constructor(private authService: AuthService) {
|
|
9
|
-
super({ usernameField: 'email' });
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async validate(email: string, password: string): Promise<any> {
|
|
13
|
-
const user = await this.authService.validateUser(email, password);
|
|
14
|
-
if (!user) {
|
|
15
|
-
throw new Error('Invalid credentials');
|
|
16
|
-
}
|
|
17
|
-
return user;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { Controller, Get, Post, Param, Body, UseGuards } from '@nestjs/common';
|
|
2
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
3
|
-
import { BillingService } from './billing.service';
|
|
4
|
-
import { TenantId } from '../../common/decorators/tenant.decorator';
|
|
5
|
-
|
|
6
|
-
@Controller('billing')
|
|
7
|
-
@UseGuards(AuthGuard('jwt'))
|
|
8
|
-
export class BillingController {
|
|
9
|
-
constructor(private billingService: BillingService) {}
|
|
10
|
-
|
|
11
|
-
@Post('charge')
|
|
12
|
-
async charge(
|
|
13
|
-
@Body() body: { amount: number; description: string },
|
|
14
|
-
@TenantId() tenantId: string,
|
|
15
|
-
@Body() req: any,
|
|
16
|
-
) {
|
|
17
|
-
const org = req.user.organization;
|
|
18
|
-
const invoice = await this.billingService.charge(
|
|
19
|
-
org.id,
|
|
20
|
-
tenantId,
|
|
21
|
-
body.amount,
|
|
22
|
-
body.description,
|
|
23
|
-
);
|
|
24
|
-
return { data: invoice };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
@Get('invoices')
|
|
28
|
-
async getInvoices(@TenantId() tenantId: string, @Body() req: any) {
|
|
29
|
-
const org = req.user.organization;
|
|
30
|
-
const invoices = await this.billingService.getInvoices(org.id, tenantId);
|
|
31
|
-
return { data: invoices };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
@Get('invoices/:id')
|
|
35
|
-
async getInvoice(
|
|
36
|
-
@Param('id') invoiceId: string,
|
|
37
|
-
@TenantId() tenantId: string,
|
|
38
|
-
) {
|
|
39
|
-
const invoice = await this.billingService.getInvoiceById(invoiceId, tenantId);
|
|
40
|
-
return { data: invoice };
|
|
41
|
-
}
|
|
42
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Module } from '@nestjs/common';
|
|
2
|
-
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
3
|
-
import { Invoice } from './entities/invoice.entity';
|
|
4
|
-
import { BillingService } from './billing.service';
|
|
5
|
-
import { BillingController } from './billing.controller';
|
|
6
|
-
|
|
7
|
-
@Module({
|
|
8
|
-
imports: [TypeOrmModule.forFeature([Invoice])],
|
|
9
|
-
controllers: [BillingController],
|
|
10
|
-
providers: [BillingService],
|
|
11
|
-
exports: [BillingService],
|
|
12
|
-
})
|
|
13
|
-
export class BillingModule {}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { InjectRepository } from '@nestjs/typeorm';
|
|
3
|
-
import { Repository } from 'typeorm';
|
|
4
|
-
import { Invoice } from './entities/invoice.entity';
|
|
5
|
-
import { v4 as uuid } from 'uuid';
|
|
6
|
-
|
|
7
|
-
@Injectable()
|
|
8
|
-
export class BillingService {
|
|
9
|
-
constructor(
|
|
10
|
-
@InjectRepository(Invoice)
|
|
11
|
-
private invoiceRepository: Repository<Invoice>,
|
|
12
|
-
) {}
|
|
13
|
-
|
|
14
|
-
async charge(organizationId: string, tenantId: string, amount: number, description: string) {
|
|
15
|
-
// Mock payment gateway (90% success rate)
|
|
16
|
-
const success = Math.random() > 0.1;
|
|
17
|
-
|
|
18
|
-
if (!success) {
|
|
19
|
-
throw new Error('Payment failed (mock)');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const invoice = this.invoiceRepository.create({
|
|
23
|
-
organizationId,
|
|
24
|
-
tenantId,
|
|
25
|
-
invoiceNumber: `INV-${Date.now()}-${uuid().slice(0, 8)}`,
|
|
26
|
-
amount,
|
|
27
|
-
currency: 'USD',
|
|
28
|
-
status: 'paid',
|
|
29
|
-
description,
|
|
30
|
-
issuedAt: new Date(),
|
|
31
|
-
dueDate: new Date(Date.now() + 14 * 24 * 60 * 60 * 1000),
|
|
32
|
-
paidAt: new Date(),
|
|
33
|
-
items: [{ description, amount, quantity: 1 }],
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
return this.invoiceRepository.save(invoice);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async getInvoices(organizationId: string, tenantId: string) {
|
|
40
|
-
return this.invoiceRepository.find({
|
|
41
|
-
where: { organizationId, tenantId },
|
|
42
|
-
order: { issuedAt: 'DESC' },
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async getInvoiceById(invoiceId: string, tenantId: string) {
|
|
47
|
-
return this.invoiceRepository.findOne({
|
|
48
|
-
where: { id: invoiceId, tenantId },
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async createInvoice(organizationId: string, tenantId: string, amount: number, description: string) {
|
|
53
|
-
const invoice = this.invoiceRepository.create({
|
|
54
|
-
organizationId,
|
|
55
|
-
tenantId,
|
|
56
|
-
invoiceNumber: `INV-${Date.now()}-${uuid().slice(0, 8)}`,
|
|
57
|
-
amount,
|
|
58
|
-
currency: 'USD',
|
|
59
|
-
status: 'pending',
|
|
60
|
-
description,
|
|
61
|
-
issuedAt: new Date(),
|
|
62
|
-
items: [{ description, amount, quantity: 1 }],
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
return this.invoiceRepository.save(invoice);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm';
|
|
2
|
-
|
|
3
|
-
@Entity('invoices')
|
|
4
|
-
@Index(['organizationId'])
|
|
5
|
-
@Index(['tenantId'])
|
|
6
|
-
@Index(['tenantId', 'status'])
|
|
7
|
-
export class Invoice {
|
|
8
|
-
@PrimaryGeneratedColumn('uuid')
|
|
9
|
-
id: string;
|
|
10
|
-
|
|
11
|
-
@Column('uuid')
|
|
12
|
-
organizationId: string;
|
|
13
|
-
|
|
14
|
-
@Column('uuid')
|
|
15
|
-
tenantId: string;
|
|
16
|
-
|
|
17
|
-
@Column({ unique: true })
|
|
18
|
-
invoiceNumber: string;
|
|
19
|
-
|
|
20
|
-
@Column('uuid', { nullable: true })
|
|
21
|
-
subscriptionId: string;
|
|
22
|
-
|
|
23
|
-
@Column('decimal', { precision: 10, scale: 2 })
|
|
24
|
-
amount: number;
|
|
25
|
-
|
|
26
|
-
@Column({ default: 'USD' })
|
|
27
|
-
currency: string;
|
|
28
|
-
|
|
29
|
-
@Column({ default: 'pending' })
|
|
30
|
-
status: string;
|
|
31
|
-
|
|
32
|
-
@Column({ nullable: true })
|
|
33
|
-
description: string;
|
|
34
|
-
|
|
35
|
-
@Column('jsonb', { nullable: true })
|
|
36
|
-
items: any[];
|
|
37
|
-
|
|
38
|
-
@Column()
|
|
39
|
-
issuedAt: Date;
|
|
40
|
-
|
|
41
|
-
@Column({ nullable: true })
|
|
42
|
-
dueDate: Date;
|
|
43
|
-
|
|
44
|
-
@Column({ nullable: true })
|
|
45
|
-
paidAt: Date;
|
|
46
|
-
|
|
47
|
-
@CreateDateColumn()
|
|
48
|
-
createdAt: Date;
|
|
49
|
-
|
|
50
|
-
@UpdateDateColumn()
|
|
51
|
-
updatedAt: Date;
|
|
52
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm';
|
|
2
|
-
|
|
3
|
-
@Entity('organizations')
|
|
4
|
-
@Index(['tenantId'])
|
|
5
|
-
@Index(['ownerId'])
|
|
6
|
-
export class Organization {
|
|
7
|
-
@PrimaryGeneratedColumn('uuid')
|
|
8
|
-
id: string;
|
|
9
|
-
|
|
10
|
-
@Column('uuid', { unique: true })
|
|
11
|
-
tenantId: string;
|
|
12
|
-
|
|
13
|
-
@Column()
|
|
14
|
-
name: string;
|
|
15
|
-
|
|
16
|
-
@Column({ unique: true })
|
|
17
|
-
slug: string;
|
|
18
|
-
|
|
19
|
-
@Column('uuid')
|
|
20
|
-
ownerId: string;
|
|
21
|
-
|
|
22
|
-
@Column({ default: 'free' })
|
|
23
|
-
plan: string;
|
|
24
|
-
|
|
25
|
-
@Column({ nullable: true })
|
|
26
|
-
stripeCustomerId: string;
|
|
27
|
-
|
|
28
|
-
@CreateDateColumn()
|
|
29
|
-
createdAt: Date;
|
|
30
|
-
|
|
31
|
-
@UpdateDateColumn()
|
|
32
|
-
updatedAt: Date;
|
|
33
|
-
|
|
34
|
-
@Column({ nullable: true })
|
|
35
|
-
deletedAt: Date;
|
|
36
|
-
}
|
package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.controller.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Controller, Get, UseGuards } from '@nestjs/common';
|
|
2
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
3
|
-
import { OrganizationsService } from './organizations.service';
|
|
4
|
-
import { TenantId } from '../../common/decorators/tenant.decorator';
|
|
5
|
-
|
|
6
|
-
@Controller('organizations')
|
|
7
|
-
@UseGuards(AuthGuard('jwt'))
|
|
8
|
-
export class OrganizationsController {
|
|
9
|
-
constructor(private orgsService: OrganizationsService) {}
|
|
10
|
-
|
|
11
|
-
@Get()
|
|
12
|
-
async getOrganization(@TenantId() tenantId: string) {
|
|
13
|
-
const org = await this.orgsService.findByTenantId(tenantId);
|
|
14
|
-
return { data: org };
|
|
15
|
-
}
|
|
16
|
-
}
|
package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.module.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Module } from '@nestjs/common';
|
|
2
|
-
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
3
|
-
import { Organization } from './entities/organization.entity';
|
|
4
|
-
import { OrganizationsService } from './organizations.service';
|
|
5
|
-
import { OrganizationsController } from './organizations.controller';
|
|
6
|
-
|
|
7
|
-
@Module({
|
|
8
|
-
imports: [TypeOrmModule.forFeature([Organization])],
|
|
9
|
-
controllers: [OrganizationsController],
|
|
10
|
-
providers: [OrganizationsService],
|
|
11
|
-
exports: [OrganizationsService],
|
|
12
|
-
})
|
|
13
|
-
export class OrganizationsModule {}
|
package/boilerplates/02-saas-starter/backend/src/modules/organizations/organizations.service.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { InjectRepository } from '@nestjs/typeorm';
|
|
3
|
-
import { Repository } from 'typeorm';
|
|
4
|
-
import { Organization } from './entities/organization.entity';
|
|
5
|
-
import { v4 as uuid } from 'uuid';
|
|
6
|
-
|
|
7
|
-
@Injectable()
|
|
8
|
-
export class OrganizationsService {
|
|
9
|
-
constructor(
|
|
10
|
-
@InjectRepository(Organization)
|
|
11
|
-
private orgRepository: Repository<Organization>,
|
|
12
|
-
) {}
|
|
13
|
-
|
|
14
|
-
async findByTenantId(tenantId: string) {
|
|
15
|
-
return this.orgRepository.findOne({ where: { tenantId } });
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async create(name: string, ownerId: string) {
|
|
19
|
-
const tenantId = uuid();
|
|
20
|
-
const org = this.orgRepository.create({
|
|
21
|
-
tenantId,
|
|
22
|
-
name,
|
|
23
|
-
slug: name.toLowerCase().replace(/\s+/g, '-'),
|
|
24
|
-
ownerId,
|
|
25
|
-
plan: 'free',
|
|
26
|
-
});
|
|
27
|
-
return this.orgRepository.save(org);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async updatePlan(tenantId: string, plan: string) {
|
|
31
|
-
await this.orgRepository.update({ tenantId }, { plan });
|
|
32
|
-
return this.findByTenantId(tenantId);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm';
|
|
2
|
-
|
|
3
|
-
@Entity('subscriptions')
|
|
4
|
-
@Index(['organizationId'])
|
|
5
|
-
@Index(['tenantId'])
|
|
6
|
-
@Index(['tenantId', 'status'])
|
|
7
|
-
export class Subscription {
|
|
8
|
-
@PrimaryGeneratedColumn('uuid')
|
|
9
|
-
id: string;
|
|
10
|
-
|
|
11
|
-
@Column('uuid')
|
|
12
|
-
organizationId: string;
|
|
13
|
-
|
|
14
|
-
@Column('uuid')
|
|
15
|
-
tenantId: string;
|
|
16
|
-
|
|
17
|
-
@Column()
|
|
18
|
-
plan: string;
|
|
19
|
-
|
|
20
|
-
@Column({ default: 'active' })
|
|
21
|
-
status: string;
|
|
22
|
-
|
|
23
|
-
@Column()
|
|
24
|
-
currentPeriodStart: Date;
|
|
25
|
-
|
|
26
|
-
@Column()
|
|
27
|
-
currentPeriodEnd: Date;
|
|
28
|
-
|
|
29
|
-
@Column({ default: false })
|
|
30
|
-
cancelAtPeriodEnd: boolean;
|
|
31
|
-
|
|
32
|
-
@Column({ nullable: true })
|
|
33
|
-
cancelledAt: Date;
|
|
34
|
-
|
|
35
|
-
@CreateDateColumn()
|
|
36
|
-
createdAt: Date;
|
|
37
|
-
|
|
38
|
-
@UpdateDateColumn()
|
|
39
|
-
updatedAt: Date;
|
|
40
|
-
}
|
package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.controller.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Controller, Get, Post, Body, Param, UseGuards } from '@nestjs/common';
|
|
2
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
3
|
-
import { SubscriptionsService } from './subscriptions.service';
|
|
4
|
-
import { TenantId } from '../../common/decorators/tenant.decorator';
|
|
5
|
-
|
|
6
|
-
@Controller('subscriptions')
|
|
7
|
-
@UseGuards(AuthGuard('jwt'))
|
|
8
|
-
export class SubscriptionsController {
|
|
9
|
-
constructor(private subsService: SubscriptionsService) {}
|
|
10
|
-
|
|
11
|
-
@Get()
|
|
12
|
-
async get(@TenantId() tenantId: string, @Body() req: any) {
|
|
13
|
-
const org = req.user.organization;
|
|
14
|
-
const sub = await this.subsService.getOrCreate(org.id, tenantId);
|
|
15
|
-
return { data: sub };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
@Post(':id/upgrade')
|
|
19
|
-
async upgrade(
|
|
20
|
-
@Param('id') id: string,
|
|
21
|
-
@Body() body: { newPlan: string },
|
|
22
|
-
@TenantId() tenantId: string,
|
|
23
|
-
@Body() req: any,
|
|
24
|
-
) {
|
|
25
|
-
const org = req.user.organization;
|
|
26
|
-
const upgraded = await this.subsService.upgrade(org.id, tenantId, body.newPlan);
|
|
27
|
-
return { data: upgraded };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@Get('usage')
|
|
31
|
-
async getUsage(@TenantId() tenantId: string, @Body() req: any) {
|
|
32
|
-
const org = req.user.organization;
|
|
33
|
-
const usage = await this.subsService.getUsage(org.id, tenantId);
|
|
34
|
-
return { data: usage };
|
|
35
|
-
}
|
|
36
|
-
}
|
package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.module.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Module } from '@nestjs/common';
|
|
2
|
-
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
3
|
-
import { Subscription } from './entities/subscription.entity';
|
|
4
|
-
import { SubscriptionsService } from './subscriptions.service';
|
|
5
|
-
import { SubscriptionsController } from './subscriptions.controller';
|
|
6
|
-
|
|
7
|
-
@Module({
|
|
8
|
-
imports: [TypeOrmModule.forFeature([Subscription])],
|
|
9
|
-
controllers: [SubscriptionsController],
|
|
10
|
-
providers: [SubscriptionsService],
|
|
11
|
-
exports: [SubscriptionsService],
|
|
12
|
-
})
|
|
13
|
-
export class SubscriptionsModule {}
|
package/boilerplates/02-saas-starter/backend/src/modules/subscriptions/subscriptions.service.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { Injectable, BadRequestException } from '@nestjs/common';
|
|
2
|
-
import { InjectRepository } from '@nestjs/typeorm';
|
|
3
|
-
import { Repository } from 'typeorm';
|
|
4
|
-
import { Subscription } from './entities/subscription.entity';
|
|
5
|
-
|
|
6
|
-
@Injectable()
|
|
7
|
-
export class SubscriptionsService {
|
|
8
|
-
private readonly plans = {
|
|
9
|
-
free: { price: 0, apiCalls: 1000, storage: 1 },
|
|
10
|
-
pro: { price: 99, apiCalls: 100000, storage: 100 },
|
|
11
|
-
enterprise: { price: 0, apiCalls: -1, storage: -1 },
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
constructor(
|
|
15
|
-
@InjectRepository(Subscription)
|
|
16
|
-
private subRepository: Repository<Subscription>,
|
|
17
|
-
) {}
|
|
18
|
-
|
|
19
|
-
async getOrCreate(organizationId: string, tenantId: string) {
|
|
20
|
-
let sub = await this.subRepository.findOne({
|
|
21
|
-
where: { organizationId, tenantId },
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
if (!sub) {
|
|
25
|
-
const now = new Date();
|
|
26
|
-
const nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
|
|
27
|
-
|
|
28
|
-
sub = this.subRepository.create({
|
|
29
|
-
organizationId,
|
|
30
|
-
tenantId,
|
|
31
|
-
plan: 'free',
|
|
32
|
-
status: 'active',
|
|
33
|
-
currentPeriodStart: now,
|
|
34
|
-
currentPeriodEnd: nextMonth,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
await this.subRepository.save(sub);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return sub;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async upgrade(organizationId: string, tenantId: string, newPlan: string) {
|
|
44
|
-
if (!this.plans[newPlan]) {
|
|
45
|
-
throw new BadRequestException('Invalid plan');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const sub = await this.subRepository.findOne({
|
|
49
|
-
where: { organizationId, tenantId },
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
if (!sub) {
|
|
53
|
-
throw new BadRequestException('Subscription not found');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const validTransitions = {
|
|
57
|
-
free: ['pro', 'enterprise'],
|
|
58
|
-
pro: ['free', 'enterprise'],
|
|
59
|
-
enterprise: ['enterprise'],
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
if (!validTransitions[sub.plan]?.includes(newPlan)) {
|
|
63
|
-
throw new BadRequestException('Invalid plan transition');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
sub.plan = newPlan;
|
|
67
|
-
await this.subRepository.save(sub);
|
|
68
|
-
|
|
69
|
-
return sub;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async getUsage(organizationId: string, tenantId: string) {
|
|
73
|
-
const sub = await this.subRepository.findOne({
|
|
74
|
-
where: { organizationId, tenantId },
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
if (!sub) {
|
|
78
|
-
throw new BadRequestException('Subscription not found');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const planDetails = this.plans[sub.plan];
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
plan: sub.plan,
|
|
85
|
-
apiCallsLimit: planDetails.apiCalls === -1 ? 'unlimited' : planDetails.apiCalls,
|
|
86
|
-
storageLimit: planDetails.storage === -1 ? 'unlimited' : `${planDetails.storage}GB`,
|
|
87
|
-
apiCallsUsed: 0,
|
|
88
|
-
storageUsed: 0,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
}
|