F4.5 · SMTP Superadmin y Report Word al Inspector
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class PhaseF4SmtpSuperadmin1790049600000 implements MigrationInterface {
|
||||
name = 'PhaseF4SmtpSuperadmin1790049600000';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE smtp_settings (
|
||||
id smallint PRIMARY KEY,
|
||||
enabled boolean NOT NULL DEFAULT false,
|
||||
host varchar(255),
|
||||
port integer,
|
||||
security_mode varchar(20),
|
||||
username varchar(255),
|
||||
password_encrypted text,
|
||||
from_name varchar(160),
|
||||
from_email varchar(255),
|
||||
reply_to varchar(255),
|
||||
updated_by uuid,
|
||||
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT chk_smtp_settings_singleton CHECK (id = 1),
|
||||
CONSTRAINT chk_smtp_settings_port CHECK (port IS NULL OR port BETWEEN 1 AND 65535),
|
||||
CONSTRAINT chk_smtp_settings_security CHECK (
|
||||
security_mode IS NULL OR security_mode IN ('TLS','STARTTLS')
|
||||
),
|
||||
CONSTRAINT fk_smtp_settings_updated_by
|
||||
FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL
|
||||
)
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
INSERT INTO smtp_settings (id, enabled)
|
||||
VALUES (1, false)
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
INSERT INTO permissions (code, description)
|
||||
VALUES ('system_mail.manage', 'Configurar la salida SMTP del sistema y probar el transporte')
|
||||
ON CONFLICT (code) DO UPDATE SET description = EXCLUDED.description
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
INSERT INTO role_permissions (role_id, permission_id)
|
||||
SELECT role.id, permission.id
|
||||
FROM roles role
|
||||
CROSS JOIN permissions permission
|
||||
WHERE role.code = 'admin'
|
||||
AND permission.code = 'system_mail.manage'
|
||||
ON CONFLICT DO NOTHING
|
||||
`);
|
||||
|
||||
await queryRunner.query(`
|
||||
UPDATE inspection_document_deliveries delivery
|
||||
SET recipient_kind = 'INSPECTOR',
|
||||
recipient_user_id = visit.lead_inspector_user_id,
|
||||
recipient_key = visit.lead_inspector_user_id,
|
||||
recipient_email = inspector.email,
|
||||
status = CASE WHEN inspector.email IS NULL THEN 'WAITING_RECIPIENT' ELSE 'PENDING' END,
|
||||
last_error = NULL,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
FROM inspection_acts act
|
||||
INNER JOIN inspection_visits visit ON visit.id = act.visit_id
|
||||
INNER JOIN users inspector ON inspector.id = visit.lead_inspector_user_id
|
||||
WHERE delivery.act_id = act.id
|
||||
AND delivery.document_kind = 'REPORT_WORD'
|
||||
AND delivery.recipient_kind = 'DIRECTOR'
|
||||
AND delivery.status <> 'SENT'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM inspection_document_deliveries existing
|
||||
WHERE existing.act_id = delivery.act_id
|
||||
AND existing.document_kind = 'REPORT_WORD'
|
||||
AND existing.recipient_kind = 'INSPECTOR'
|
||||
AND existing.recipient_key = visit.lead_inspector_user_id
|
||||
)
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
DELETE FROM role_permissions
|
||||
WHERE permission_id IN (
|
||||
SELECT id FROM permissions WHERE code = 'system_mail.manage'
|
||||
)
|
||||
`);
|
||||
await queryRunner.query(`DELETE FROM permissions WHERE code = 'system_mail.manage'`);
|
||||
await queryRunner.query(`DROP TABLE IF EXISTS smtp_settings`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user