feat(infra): conecta ao banco ERP libreplast e fixa rep 29 como usuário dev
- sar-erp-schema.sql: corrige grupo.nome (era descricao), tp_pauta inexistente
em pauxpro, COALESCE(id_empresa,1) em vw_clientes para bancos single-tenant,
e nome do cliente via COALESCE(NULLIF(TRIM(nome),''), TRIM(razao))
- WorkspacePrismaPool: PrismaPg({ schema: 'sar' }) + options search_path=sar
para ORM e queries raw funcionarem no schema correto
- JwtAuthGuard: força DEV_REP_CODE/DEV_EMPRESA_ID em não-prod — filtro
global sem tocar em nenhum service
- env.schema: adiciona DEV_REP_CODE e DEV_EMPRESA_ID com defaults 29 e 1
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,9 @@ import { IS_PUBLIC_KEY } from './public.decorator';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class JwtAuthGuard implements CanActivate {
|
export class JwtAuthGuard implements CanActivate {
|
||||||
private readonly secret: Uint8Array;
|
private readonly secret: Uint8Array;
|
||||||
|
private readonly isProd: boolean;
|
||||||
|
private readonly devRepCode: string;
|
||||||
|
private readonly devEmpresaId: number;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly reflector: Reflector,
|
private readonly reflector: Reflector,
|
||||||
@@ -26,6 +29,9 @@ export class JwtAuthGuard implements CanActivate {
|
|||||||
config: ConfigService<Env, true>,
|
config: ConfigService<Env, true>,
|
||||||
) {
|
) {
|
||||||
this.secret = new TextEncoder().encode(config.get('MASTER_LOGIN_JWT_SECRET', { infer: true }));
|
this.secret = new TextEncoder().encode(config.get('MASTER_LOGIN_JWT_SECRET', { infer: true }));
|
||||||
|
this.isProd = config.get('NODE_ENV', { infer: true }) === 'production';
|
||||||
|
this.devRepCode = String(config.get('DEV_REP_CODE', { infer: true }));
|
||||||
|
this.devEmpresaId = config.get('DEV_EMPRESA_ID', { infer: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
@@ -45,18 +51,18 @@ export class JwtAuthGuard implements CanActivate {
|
|||||||
|
|
||||||
(req as Request & { user: JwtPayload }).user = payload as JwtPayload;
|
(req as Request & { user: JwtPayload }).user = payload as JwtPayload;
|
||||||
|
|
||||||
// Sobrescreve CLS com idEmpresa real do JWT (corre depois do middleware).
|
// Em dev: força representante fixo (DEV_REP_CODE / DEV_EMPRESA_ID) ignorando o JWT.
|
||||||
const idEmpresa = payload.id_empresa;
|
// Em prod: usa os valores reais do JWT.
|
||||||
|
const idEmpresa = this.isProd ? payload.id_empresa : this.devEmpresaId;
|
||||||
|
const userId = this.isProd ? payload.sub : this.devRepCode;
|
||||||
this.cls.set('idEmpresa', idEmpresa);
|
this.cls.set('idEmpresa', idEmpresa);
|
||||||
this.cls.set('userId', payload.sub);
|
this.cls.set('userId', userId);
|
||||||
this.cls.set('role', payload.role);
|
this.cls.set('role', payload.role);
|
||||||
|
|
||||||
// URL inclui ?schema=sar para o Prisma rotear ao schema correto no ERP
|
|
||||||
const baseUrl =
|
const baseUrl =
|
||||||
process.env['DATABASE_URL'] ??
|
process.env['DATABASE_URL'] ??
|
||||||
'postgresql://sar:sar_dev_password@localhost:5432/sar_workspace_dev';
|
'postgresql://sar:sar_dev_password@localhost:5432/sar_workspace_dev';
|
||||||
const dbUrl = baseUrl.includes('?') ? `${baseUrl}&schema=sar` : `${baseUrl}?schema=sar`;
|
this.cls.set('prisma', this.pool.getOrCreate(idEmpresa, baseUrl));
|
||||||
this.cls.set('prisma', this.pool.getOrCreate(idEmpresa, dbUrl));
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ export const EnvSchema = z
|
|||||||
// Multi-tenancy — workspace de dev (até master-login real entrar)
|
// Multi-tenancy — workspace de dev (até master-login real entrar)
|
||||||
DEFAULT_WORKSPACE_ID: z.string().min(1).default('dev-workspace'),
|
DEFAULT_WORKSPACE_ID: z.string().min(1).default('dev-workspace'),
|
||||||
|
|
||||||
|
// Representante fixo de dev — forçado no guard enquanto não há login real
|
||||||
|
DEV_REP_CODE: z.coerce.number().int().positive().default(29),
|
||||||
|
DEV_EMPRESA_ID: z.coerce.number().int().positive().default(1),
|
||||||
|
|
||||||
// Postgres (Prisma virá depois)
|
// Postgres (Prisma virá depois)
|
||||||
DATABASE_URL: z.string().optional(),
|
DATABASE_URL: z.string().optional(),
|
||||||
MIGRATION_DATABASE_URL: z.string().optional(),
|
MIGRATION_DATABASE_URL: z.string().optional(),
|
||||||
|
|||||||
@@ -37,8 +37,12 @@ export class WorkspacePrismaPool implements OnModuleDestroy {
|
|||||||
this.evictOldest();
|
this.evictOldest();
|
||||||
}
|
}
|
||||||
|
|
||||||
const pgPool = new pg.Pool({ connectionString: dbUrl, max: PG_POOL_SIZE });
|
const pgPool = new pg.Pool({
|
||||||
const adapter = new PrismaPg(pgPool);
|
connectionString: dbUrl,
|
||||||
|
max: PG_POOL_SIZE,
|
||||||
|
options: '-c search_path=sar',
|
||||||
|
});
|
||||||
|
const adapter = new PrismaPg(pgPool, { schema: 'sar' });
|
||||||
const client = new PrismaClient({ adapter });
|
const client = new PrismaClient({ adapter });
|
||||||
this.cache.set(key, { client, pgPool });
|
this.cache.set(key, { client, pgPool });
|
||||||
this.logger.log(`pool criado: idEmpresa=${idEmpresa} total=${this.cache.size}`);
|
this.logger.log(`pool criado: idEmpresa=${idEmpresa} total=${this.cache.size}`);
|
||||||
|
|||||||
@@ -154,9 +154,9 @@ SELECT
|
|||||||
m.vl_fator,
|
m.vl_fator,
|
||||||
-- Filtros de segmento (opcionais — NULL = meta geral)
|
-- Filtros de segmento (opcionais — NULL = meta geral)
|
||||||
m.cod_grupo,
|
m.cod_grupo,
|
||||||
grp.descricao AS desc_grupo,
|
grp.nome AS desc_grupo,
|
||||||
m.cod_subgrupo,
|
m.cod_subgrupo,
|
||||||
sub.descricao AS desc_subgrupo,
|
sub.nome AS desc_subgrupo,
|
||||||
m.cod_produto,
|
m.cod_produto,
|
||||||
m.cod_marca,
|
m.cod_marca,
|
||||||
mrc.nome AS nome_marca,
|
mrc.nome AS nome_marca,
|
||||||
@@ -172,11 +172,11 @@ LEFT JOIN gestao.marca mrc ON mrc.codigo = m.cod_marca AND mrc.id_empre
|
|||||||
-- -----------------------------------------------------------------------------
|
-- -----------------------------------------------------------------------------
|
||||||
CREATE OR REPLACE VIEW sar.vw_clientes AS
|
CREATE OR REPLACE VIEW sar.vw_clientes AS
|
||||||
SELECT
|
SELECT
|
||||||
c.id_empresa,
|
COALESCE(c.id_empresa, 1) AS id_empresa,
|
||||||
c.id_corrent AS id_cliente,
|
c.id_corrent AS id_cliente,
|
||||||
c.ativo,
|
c.ativo,
|
||||||
c.nome,
|
COALESCE(NULLIF(TRIM(c.nome), ''), TRIM(c.razao)) AS nome,
|
||||||
c.razao,
|
TRIM(c.razao) AS razao,
|
||||||
c.pesso AS pessoa, -- 0=PJ 1=PF
|
c.pesso AS pessoa, -- 0=PJ 1=PF
|
||||||
c.consfinal,
|
c.consfinal,
|
||||||
c.cgcpf,
|
c.cgcpf,
|
||||||
@@ -258,9 +258,9 @@ SELECT
|
|||||||
COALESCE(p.vl_preco2, 0) AS vl_preco2,
|
COALESCE(p.vl_preco2, 0) AS vl_preco2,
|
||||||
COALESCE(p.vl_preco3, 0) AS vl_preco3,
|
COALESCE(p.vl_preco3, 0) AS vl_preco3,
|
||||||
p.cod_grupo,
|
p.cod_grupo,
|
||||||
grp.descricao AS grupo,
|
grp.nome AS grupo,
|
||||||
p.cod_subgrupo,
|
p.cod_subgrupo,
|
||||||
sub.descricao AS subgrupo,
|
sub.nome AS subgrupo,
|
||||||
sub.desc_max,
|
sub.desc_max,
|
||||||
COALESCE(p.grupo_st, '') AS grupo_st,
|
COALESCE(p.grupo_st, '') AS grupo_st,
|
||||||
p.cod_marca,
|
p.cod_marca,
|
||||||
@@ -378,7 +378,7 @@ SELECT
|
|||||||
COALESCE(pp.preco2, 0) AS preco2,
|
COALESCE(pp.preco2, 0) AS preco2,
|
||||||
COALESCE(pp.preco3, 0) AS preco3,
|
COALESCE(pp.preco3, 0) AS preco3,
|
||||||
COALESCE(pp.valor_pauta_icms_st, 0) AS valor_pauta_icms_st,
|
COALESCE(pp.valor_pauta_icms_st, 0) AS valor_pauta_icms_st,
|
||||||
pp.tp_pauta
|
NULL::integer AS tp_pauta
|
||||||
FROM gestao.pauxpro pp;
|
FROM gestao.pauxpro pp;
|
||||||
|
|
||||||
-- -----------------------------------------------------------------------------
|
-- -----------------------------------------------------------------------------
|
||||||
@@ -486,7 +486,7 @@ CREATE OR REPLACE VIEW sar.vw_grupos AS
|
|||||||
SELECT
|
SELECT
|
||||||
id_empresa,
|
id_empresa,
|
||||||
codigo,
|
codigo,
|
||||||
descricao,
|
nome AS descricao,
|
||||||
int_sar,
|
int_sar,
|
||||||
produto_variacao,
|
produto_variacao,
|
||||||
desc_max,
|
desc_max,
|
||||||
|
|||||||
Reference in New Issue
Block a user