This commit is contained in:
2026-03-05 17:37:30 +01:00
commit 37e41a0130
14 changed files with 2366 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
CREATE EXTENSION pgcrypto; -- para los random bytes
-- 1. Función de generacion de uuidv7 copiada porque no esta en postgre 19
CREATE OR REPLACE FUNCTION
uuid_generate_v7()
RETURNS
uuid
LANGUAGE
plpgsql
PARALLEL SAFE
AS $$
DECLARE
-- The current UNIX timestamp in milliseconds
unix_time_ms CONSTANT bytea NOT NULL DEFAULT substring(int8send((extract(epoch FROM clock_timestamp()) * 1000)::bigint) from 3);
-- The buffer used to create the UUID, starting with the UNIX timestamp and followed by random bytes
buffer bytea NOT NULL DEFAULT unix_time_ms || gen_random_bytes(10);
BEGIN
-- Set most significant 4 bits of 7th byte to 7 (for UUID v7), keeping the last 4 bits unchanged
buffer = set_byte(buffer, 6, (b'0111' || get_byte(buffer, 6)::bit(4))::bit(8)::int);
-- Set most significant 2 bits of 9th byte to 2 (the UUID variant specified in RFC 4122), keeping the last 6 bits unchanged
buffer = set_byte(buffer, 8, (b'10' || get_byte(buffer, 8)::bit(6))::bit(8)::int);
RETURN encode(buffer, 'hex');
END
$$
;
-- 2. Tabla de Tarjetas
-- NUNCA se guarda el numero completo (PAN). Solo los últimos 4 dígitos.
CREATE TABLE payment_cards (
card_id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
user_id UUID REFERENCES users(user_id),
pan_last_four VARCHAR(4) NOT NULL,
card_holder_name VARCHAR(100) NOT NULL,
status VARCHAR(20) DEFAULT 'PENDING_ACTIVATION', -- PENDING, ACTIVE, BLOCKED, EXPIRED
created_at TIMESTAMPTZ DEFAULT NOW(),
activated_at TIMESTAMPTZ
);
-- 3. Tabla de Códigos de Activación
CREATE TABLE activation_codes (
code_id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
card_id UUID REFERENCES payment_cards(card_id), -- Una tarjeta, maximo un un código activo borrar o solo con expires_at?
code_hash TEXT NOT NULL, -- Guardar el código hasheado, el original se imprime y se manda
is_used BOOLEAN DEFAULT FALSE,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- 4. Registro de Auditoría y Dispositivos (Log de Uso)
CREATE TABLE activation_logs (
log_id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
card_id UUID REFERENCES payment_cards(card_id),
code_id UUID REFERENCES activation_codes(code_id),
action_type VARCHAR(50) NOT NULL, -- TODO: CREAR ENUM'GENERATED', 'ATTEMPT_FAILED', 'ACTIVATED'
ip_address INET,
device_info JSONB, -- Almacena user-agent, modelo, SO, etc.
geo_location VARCHAR(100), -- Opcional: Ciudad/País derivado de IP
created_at TIMESTAMPTZ DEFAULT NOW()
);

View File

@@ -0,0 +1,22 @@
# Stage base para coordinar las fases de build y ejecucion
FROM node:22-alpine AS base
# Hace falta para la herramienta de migraciones, cuando se publique se
# sustituira por el paquete de npm
RUN apk --no-cache add git=latest
WORKDIR /usr/local/app
COPY ./package.json ./package.lock ./
# copia el codigo en general
COPY tsconfig*.json ./
COPY .env* ./
COPY ./.yarnrc.yml ./
COPY ./deployment/local/docker/start.sh ./
# Copiar el archivo de migrations? porque ahora no creo que se esté lanzando nada
COPY ./deployment/database/migrations ./deployment/database/migrations
RUN npm install --production && \
npm build && \
chmod +x start.sh
EXPOSE ${PORT}
ENTRYPOINT [ "./start.sh" ]

View File

@@ -0,0 +1,57 @@
name: sf-nfc-server
networks:
default:
driver: bridge
name: network-test # Tiene que coincidir con el compose objetivo
services:
sf-nfc-api:
container_name: sf-nfc-api
image: sf-nfc-api
build:
context: ./
dockerfile: deployment/local/docker/Dockerfile.local
args:
PORT: "${PORT:-3000}"
develop:
watch:
- path: ./src
action: sync
target: /usr/local/app/packages
- path: ./package.json
action: rebuild
ports:
- ${PORT}:${PORT}
env_file:
- .env
restart: unless-stopped
healthcheck:
test:
[
"CMD-SHELL",
'node -e "fetch(''http://localhost:'' + (process.env.PORT || 3000) + ''/health'').then(r => { if (!r.ok) process.exit(1) }).catch(() => process.exit(1))"',
]
interval: 10s
timeout: 5s
retries: 5
start_period: 15s
depends_on:
postgresql-nfc:
condition: service_healthy
postgresql-nfc:
container_name: postgresql-nfc
image: postgres:16.1
env_file:
- .env
ports:
- "${POSTGRES_PORT}:${POSTGRES_PORT}"
volumes:
- ./sql-data/:/var/lib/postgres/data
- ./deployment/database/init.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
retries: 5
start_period: 5s
timeout: 5s

View File

@@ -0,0 +1,3 @@
#!/bin/sh
echo "Lanzando migraciones e iniciando servidor"
npm run migrate && npm run start