diff --git a/deployment/database/init.sql b/deployment/database/init.sql new file mode 100644 index 0000000..3c8299b --- /dev/null +++ b/deployment/database/init.sql @@ -0,0 +1,103 @@ +-- eliminar los drop para prod +drop domain if exists imei_type cascade; +CREATE DOMAIN imei_type as varchar(15); +drop domain if exists iccid_type cascade; +CREATE DOMAIN iccid_type as varchar(22); +drop domain if exists imsi_type cascade; +CREATE DOMAIN imsi_type as varchar(15); + + +CREATE table if not exists sim_cards ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + imei imei_type, + iccid iccid_type, + imsi imsi_type, + user_id BIGINT, + subscription_id BIGINT, + created_at TIMESTAMP, + last_update TIMESTAMP, + deleted_at TIMESTAMP +); + +CREATE TABLE if not exists sim_operations ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + sim_id BIGINT, + operation_type TEXT NOT NULL, + happened_at TIMESTAMP, + + CONSTRAINT valid_operations CHECK ( + operation_type in ('free','preactivate','activate','pause','cancel') + ), + + CONSTRAINT fk_sim_id + FOREIGN KEY(sim_id) + REFERENCES sim_cards(id) +); + +CREATE TABLE if not exists sim_envio ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + codigo_origen TEXT, + codigo_distrito TEXT, + pedido_id BIGINT, + sim_id BIGINT, + + fecha_envio TIMESTAMP, + fecha_email TIMESTAMP, + is_preactivado BOOLEAN, + fecha_devolucion TIMESTAMP, + created_at TIMESTAMP, + + CONSTRAINT fk_sim_id + FOREIGN KEY(sim_id) REFERENCES sim_cards(id) +); + +-- Mock, No es parte de SIMs +CREATE TABLE if not exists sf_subscription ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY +); + +-- No habria que meterle las propiedades del tipo de subscripcion +CREATE TABLE if not exists sim_subscription_types ( + id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + subscription TEXT NOT NULL, + created_at TIMESTAMP, + updated_at TIMESTAMP, + deleted_at TIMESTAMP +); + +CREATE TABLE if not exists sim_company ( + id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + name TEXT, + created_at TIMESTAMP, + updated_at TIMESTAMP, + deleted_at TIMESTAMP +); + +CREATE TABLE sim_subscription ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + company_id INT, + subscription_type_id INT, + sim_id BIGINT, + order_id BIGINT, + + created_at TIMESTAMP, + updated_at TIMESTAMP, + deleted_at TIMESTAMP, + + CONSTRAINT fk_sim_id + FOREIGN KEY(sim_id) REFERENCES sim_cards(id), + + CONSTRAINT fk_company_id + FOREIGN KEY(company_id) REFERENCES sim_company(id), + + CONSTRAINT fk_subscription_type_id + FOREIGN KEY(subscription_type_id) REFERENCES sim_subscription_types(id) +); + +-- Se supone que indica un cambio +CREATE TABLE sim_subscription_historic ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + subscription_id BIGINT, + iccid iccid_type, + company_id INT +); diff --git a/deployment/database/test.sql b/deployment/database/test.sql deleted file mode 100644 index 9694aa5..0000000 --- a/deployment/database/test.sql +++ /dev/null @@ -1,31 +0,0 @@ -CREATE TABLE sim_job_queue ( - id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - job_type TEXT NOT NULL, - sim_id INT, -- NOT NULL??? - payload JSONB NOT NULL, - status TEXT NOT NULL DEFAULT "pending", -- pasar a enum - priority SMALLINT NOT NULL DEFAULT 100, -- trabajamos entre 200 y 0 para 0 la maxima prioridad - retry_count INT NOT NULL DEFAULT 0, - max_retries INT NOT NULL DEFAULT 3, -- lo definiria en runtime - scheduled_for TIMESTAMP NOT NULL DEFAULT now(), - created_at TIMESTAMP NOT NULL DEFAULT now(), - started_at TIMESTAMP, - completed_at TIMESTAMP, - error_msg TEXT, - - -- En teoria el check es mas flexible que el enum - CONSTRAINT valid_status CHECK ( - status IN ('pending', 'processing', 'completed', 'failed', 'dead') - ) - - -- Revisar si interesa asociar cada trabajo a un id o permitir que sean independientes - CONSTRAINT fk_sim_id - FOREIGN KEY(sim_id) REFERENCES tableName(tarjetas_sim) -) - --- Indice de pendientes de consumir -CREATE INDEX idx_job_fetch ON sim_job_queue (status, scheduled_for, priority DESC, created_at) - WHERE status = 'pending'; - --- Indice del resto de procesos -CREATE INDEX idx_job_monitor ON sim_job_queue (job_type, status, created_at); diff --git a/deployment/local/docker/docker-compose.yaml b/deployment/local/docker/docker-compose.yaml index f5fb0fd..7aefd33 100644 --- a/deployment/local/docker/docker-compose.yaml +++ b/deployment/local/docker/docker-compose.yaml @@ -58,7 +58,7 @@ services: - "5432:${DEV_POSTGRES_PORT}" volumes: - ./sql-data/:/var/lib/postgres/data - - ./deployment/database/test.sql:/docker-entrypoint-initdb.d/init.sql + - ./deployment/database/init.sql:/docker-entrypoint-initdb.d/init.sql healthcheck: test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] interval: 5s diff --git a/packages/shared/domain/SimCard.ts b/packages/shared/domain/SimCard.ts new file mode 100644 index 0000000..7d1e041 --- /dev/null +++ b/packages/shared/domain/SimCard.ts @@ -0,0 +1,20 @@ +import { User } from "./User" + +export type SimCard = { + iccid: string, + imei: string, + + /* Pedido de shopify */ + orderdId?: string, // Pasar a tipo + + /* Subscripcion de shopify */ + subscriptionId?: string, // Pasar a tipo + + user?: User + + createdAt?: Date, + updatedAt?: Date, + + codigoOrigen?: string, +} + diff --git a/packages/shared/domain/SimEvents.ts b/packages/shared/domain/SimEvents.ts index 0aa845a..e42cdb0 100644 --- a/packages/shared/domain/SimEvents.ts +++ b/packages/shared/domain/SimEvents.ts @@ -36,4 +36,14 @@ export namespace SimEvents { options: { } } + + export type save = DomainEvent & { + key: "sim.save", + payload: { + iccid: string, + imei: string + }, + options: { + } + } } diff --git a/packages/shared/domain/SimOrder.ts b/packages/shared/domain/SimOrder.ts new file mode 100644 index 0000000..e69de29 diff --git a/packages/shared/domain/SimSubscription.ts b/packages/shared/domain/SimSubscription.ts new file mode 100644 index 0000000..e69de29 diff --git a/packages/shared/domain/User.ts b/packages/shared/domain/User.ts new file mode 100644 index 0000000..b3199ef --- /dev/null +++ b/packages/shared/domain/User.ts @@ -0,0 +1,6 @@ +export type User = { + userId: string, + userName?: string, + email?: string, + tlfn?: string, +} diff --git a/packages/sim-entrada-eventos/aplication/Sim.usecases.ts b/packages/sim-entrada-eventos/aplication/Sim.usecases.ts index 8a67c1e..c3976e3 100644 --- a/packages/sim-entrada-eventos/aplication/Sim.usecases.ts +++ b/packages/sim-entrada-eventos/aplication/Sim.usecases.ts @@ -17,6 +17,23 @@ export class SimUsecases { this.eventBus = args.eventBus } + /** + * Crea una nueva sim de la que no se tenia registro anteriormente + * Si ya existia se modifican los campos pero no se hace un cambio + * de estado. + */ + async save(args: { iccid: string, imei: string }) { + const activationEvent = { + key: "sim.save", + payload: { + iccid: args.iccid, + imei: args.imei + } + } + + return this.eventBus.publish([activationEvent]) + } + async activation(args: { iccid: string }) { const activationEvent = { key: "sim.activation",