forjajs 5.0.0 → 5.1.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 +68 -2
- package/README.md +4 -2
- package/README.pt-BR.md +4 -1
- 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/bin/create-memory-nest-kit.js +0 -0
- package/dist/bin/forja.js +0 -0
- package/dist/bin/init-project.js +1 -0
- package/dist/lib/core/journey.js +10 -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-upgrade.d.ts +0 -4
- package/dist/lib/project-upgrade.js +19 -1
- package/dist/lib/project-wiring.d.ts +8 -0
- package/dist/lib/project-wiring.js +74 -2
- package/dist/lib/validators/structure-validator.js +0 -1
- package/dist/packages/llm/src/index.js +12 -2
- package/dist/scripts/llm-fit.js +23 -1
- package/dist/scripts/project-upgrade.js +9 -6
- package/docs/init-project.md +1 -1
- package/docs/llm-fit-loop.md +16 -0
- package/memory/90-decisions/0086-projeto-conectado-e-jornada-como-gate.md +1 -1
- 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/package.json +2 -2
- 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/templates/project/AGENTS.md +9 -0
- 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,23 +0,0 @@
|
|
|
1
|
-
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
-
import { Reflector } from '@nestjs/core';
|
|
3
|
-
|
|
4
|
-
@Injectable()
|
|
5
|
-
export class RolesGuard implements CanActivate {
|
|
6
|
-
constructor(private reflector: Reflector) {}
|
|
7
|
-
|
|
8
|
-
canActivate(context: ExecutionContext): boolean {
|
|
9
|
-
const requiredRoles = this.reflector.get<string[]>('roles', context.getHandler());
|
|
10
|
-
if (!requiredRoles) {
|
|
11
|
-
return true;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const request = context.switchToHttp().getRequest();
|
|
15
|
-
const user = request.user;
|
|
16
|
-
|
|
17
|
-
if (!user || !user.roles) {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return requiredRoles.some((role) => user.roles.includes(role));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
|
|
3
|
-
import { ConfigService } from '@nestjs/config';
|
|
4
|
-
import { User } from '../modules/users/entities/user.entity';
|
|
5
|
-
import { Organization } from '../modules/organizations/entities/organization.entity';
|
|
6
|
-
import { Subscription } from '../modules/subscriptions/entities/subscription.entity';
|
|
7
|
-
import { Invoice } from '../modules/billing/entities/invoice.entity';
|
|
8
|
-
|
|
9
|
-
@Injectable()
|
|
10
|
-
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
|
|
11
|
-
constructor(private configService: ConfigService) {}
|
|
12
|
-
|
|
13
|
-
createTypeOrmOptions(): TypeOrmModuleOptions {
|
|
14
|
-
return {
|
|
15
|
-
type: 'postgres',
|
|
16
|
-
host: this.configService.get('DB_HOST', 'localhost'),
|
|
17
|
-
port: this.configService.get('DB_PORT', 5432),
|
|
18
|
-
username: this.configService.get('DB_USER', 'postgres'),
|
|
19
|
-
password: this.configService.get('DB_PASSWORD', 'postgres'),
|
|
20
|
-
database: this.configService.get('DB_NAME', 'saas_starter'),
|
|
21
|
-
entities: [User, Organization, Subscription, Invoice],
|
|
22
|
-
synchronize: this.configService.get('NODE_ENV') === 'development',
|
|
23
|
-
logging: this.configService.get('DB_LOGGING') === 'true',
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { Controller, Post, Body, UseGuards, Get, UnauthorizedException } from '@nestjs/common';
|
|
2
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
3
|
-
import { AuthService } from './auth.service';
|
|
4
|
-
import { SignupDto, LoginDto, RefreshTokenDto } from './dto/auth.dto';
|
|
5
|
-
|
|
6
|
-
@Controller('auth')
|
|
7
|
-
export class AuthController {
|
|
8
|
-
constructor(private authService: AuthService) {}
|
|
9
|
-
|
|
10
|
-
@Post('signup')
|
|
11
|
-
async signup(@Body() signupDto: SignupDto) {
|
|
12
|
-
const result = await this.authService.signup(signupDto);
|
|
13
|
-
return { data: result };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Post('login')
|
|
17
|
-
@UseGuards(AuthGuard('local'))
|
|
18
|
-
async login(@Body() _loginDto: LoginDto) {
|
|
19
|
-
return { data: 'Use POST /api/auth/login with email & password' };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
@Post('login-email')
|
|
23
|
-
async loginWithEmail(@Body() loginDto: LoginDto) {
|
|
24
|
-
const result = await this.authService.login(loginDto.email, loginDto.password);
|
|
25
|
-
return { data: result };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
@Post('refresh')
|
|
29
|
-
async refresh(@Body() refreshTokenDto: RefreshTokenDto) {
|
|
30
|
-
const payload = this.authService.verifyRefreshToken(refreshTokenDto.refreshToken);
|
|
31
|
-
const tokens = this.authService.generateTokens({ ...payload });
|
|
32
|
-
return { data: tokens };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
@Get('me')
|
|
36
|
-
@UseGuards(AuthGuard('jwt'))
|
|
37
|
-
async getMe(@Body() req: any) {
|
|
38
|
-
return { data: req.user };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
@Post('logout')
|
|
42
|
-
@UseGuards(AuthGuard('jwt'))
|
|
43
|
-
async logout() {
|
|
44
|
-
return { message: 'Logged out successfully' };
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Module } from '@nestjs/common';
|
|
2
|
-
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
3
|
-
import { JwtModule } from '@nestjs/jwt';
|
|
4
|
-
import { PassportModule } from '@nestjs/passport';
|
|
5
|
-
import { User } from '../users/entities/user.entity';
|
|
6
|
-
import { Organization } from '../organizations/entities/organization.entity';
|
|
7
|
-
import { AuthService } from './auth.service';
|
|
8
|
-
import { AuthController } from './auth.controller';
|
|
9
|
-
import { JwtStrategy } from './strategies/jwt.strategy';
|
|
10
|
-
import { LocalStrategy } from './strategies/local.strategy';
|
|
11
|
-
|
|
12
|
-
@Module({
|
|
13
|
-
imports: [
|
|
14
|
-
TypeOrmModule.forFeature([User, Organization]),
|
|
15
|
-
PassportModule,
|
|
16
|
-
JwtModule.register({
|
|
17
|
-
secret: process.env.JWT_SECRET || 'dev-secret-key',
|
|
18
|
-
signOptions: { expiresIn: '15m' },
|
|
19
|
-
}),
|
|
20
|
-
],
|
|
21
|
-
controllers: [AuthController],
|
|
22
|
-
providers: [AuthService, JwtStrategy, LocalStrategy],
|
|
23
|
-
exports: [AuthService],
|
|
24
|
-
})
|
|
25
|
-
export class AuthModule {}
|
|
@@ -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
|
-
}
|