init
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
.env*
|
||||
*.js
|
||||
.json
|
||||
61
deployment/database/migrations/1.0.0_base.sql
Normal file
61
deployment/database/migrations/1.0.0_base.sql
Normal 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()
|
||||
);
|
||||
22
deployment/local/docker/Dockerfile.local
Normal file
22
deployment/local/docker/Dockerfile.local
Normal 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" ]
|
||||
|
||||
57
deployment/local/docker/docker-compose.yaml
Normal file
57
deployment/local/docker/docker-compose.yaml
Normal 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
|
||||
3
deployment/local/start.sh
Normal file
3
deployment/local/start.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
echo "Lanzando migraciones e iniciando servidor"
|
||||
npm run migrate && npm run start
|
||||
9
docs/nfc-server/.gitignore
vendored
Normal file
9
docs/nfc-server/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# Secrets
|
||||
.env*
|
||||
|
||||
# Dependencies
|
||||
node_modules
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
10
docs/nfc-server/opencollection.yml
Normal file
10
docs/nfc-server/opencollection.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
opencollection: 1.0.0
|
||||
|
||||
info:
|
||||
name: nfc-server
|
||||
bundled: false
|
||||
extensions:
|
||||
bruno:
|
||||
ignore:
|
||||
- node_modules
|
||||
- .git
|
||||
2052
package-lock.json
generated
Normal file
2052
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
29
package.json
Normal file
29
package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "sf-nfc-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "tsx src/main.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/main.js",
|
||||
"migrate": "db-migrate -e .env -m ./deployment/database/migrations/ -t 99.0.0"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/express": "^5.0.6",
|
||||
"@types/node": "^25.3.3",
|
||||
"@types/pg": "^8.18.0",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.9.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"db-migrate": "^0.11.14",
|
||||
"dotenv": "^17.3.1",
|
||||
"express": "^5.2.1",
|
||||
"pg": "^8.20.0"
|
||||
}
|
||||
}
|
||||
17
src/aplication/middleware.ts
Normal file
17
src/aplication/middleware.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { type Request, type Response, type NextFunction } from 'express';
|
||||
|
||||
export interface AppError extends Error {
|
||||
status?: number;
|
||||
}
|
||||
|
||||
export const errorHandler = (
|
||||
err: AppError,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
console.error(err);
|
||||
res.status(err.status || 500).json({
|
||||
message: err.message || 'Internal Server Error',
|
||||
});
|
||||
};
|
||||
14
src/config/env.config.ts
Normal file
14
src/config/env.config.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
process.loadEnvFile(".env")
|
||||
|
||||
export const env = {
|
||||
|
||||
PORT: Number(process.env.PORT),
|
||||
HOST: process.env.HOST,
|
||||
|
||||
POSTGRES_HOST: process.env.POSTGRES_HOST,
|
||||
POSTGRES_PORT: process.env.POSTGRES_PORT,
|
||||
POSTGRES_USER: process.env.POSTGRES_USER,
|
||||
POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD
|
||||
}
|
||||
|
||||
console.log(env)
|
||||
12
src/domain/nfcRegistry.ts
Normal file
12
src/domain/nfcRegistry.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export type nfcRegsitry = {
|
||||
account_id: string,
|
||||
account_number: string
|
||||
}
|
||||
|
||||
export type activationCodes = {
|
||||
id: number,
|
||||
code: string,
|
||||
account_id: string,
|
||||
creation_date: string,
|
||||
expiration_date: string
|
||||
}
|
||||
29
src/main.ts
Normal file
29
src/main.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import express, { Router, type Request, type Response } from 'express';
|
||||
import { errorHandler } from './aplication/middleware.js';
|
||||
import { env } from './config/env.config.js';
|
||||
import assert from 'assert';
|
||||
|
||||
const PORT = env.PORT
|
||||
const HOSTNAME = env.HOST
|
||||
|
||||
assert(HOSTNAME != undefined)
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/health", (req: Request, res: Response) => {
|
||||
res.json({ ok: true })
|
||||
})
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
// Routes
|
||||
app.use('/', router);
|
||||
|
||||
// Global error handler (should be after routes)
|
||||
app.use(errorHandler);
|
||||
|
||||
app.listen(PORT, HOSTNAME, (n) => {
|
||||
console.log(`[o] Servidor escuchando en ${HOSTNAME}:${PORT}`)
|
||||
})
|
||||
45
tsconfig.json
Normal file
45
tsconfig.json
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
// Visit https://aka.ms/tsconfig to read more about this file
|
||||
"compilerOptions": {
|
||||
// File Layout
|
||||
"baseUrl": "./",
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
// Environment Settings
|
||||
// See also https://aka.ms/tsconfig/module
|
||||
"module": "nodenext",
|
||||
"target": "esnext",
|
||||
"moduleResolution": "nodenext",
|
||||
// For nodejs:
|
||||
"lib": [
|
||||
"esnext"
|
||||
],
|
||||
"types": [
|
||||
"node"
|
||||
],
|
||||
// and npm install -D @types/node
|
||||
// Other Outputs
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
// Stricter Typechecking Options
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
// Style Options
|
||||
// "noImplicitReturns": true,
|
||||
// "noImplicitOverride": true,
|
||||
// "noUnusedLocals": true,
|
||||
// "noUnusedParameters": true,
|
||||
// "noFallthroughCasesInSwitch": true,
|
||||
// "noPropertyAccessFromIndexSignature": true,
|
||||
// Recommended Options
|
||||
"strict": true,
|
||||
"jsx": "react-jsx",
|
||||
"verbatimModuleSyntax": true,
|
||||
"isolatedModules": true,
|
||||
"noUncheckedSideEffectImports": true,
|
||||
"moduleDetection": "force",
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user