Las operaciones basicas del repositorio de pause/cancel funcionan y
tienen test
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { after, before, describe, it } from "node:test";
|
||||
import { CreatePauseCancelTaskDTO, PauseCancelTaskRepository } from "./PauseCancelTaskRepository.js";
|
||||
import { postgrClient } from "#config/postgreConfig.js";
|
||||
import assert from "node:assert";
|
||||
|
||||
const testTask: CreatePauseCancelTaskDTO = {
|
||||
iccid: "1234",
|
||||
activation_date: new Date(),
|
||||
next_check: new Date()
|
||||
}
|
||||
|
||||
describe("Test PauseCancelTaskRepository - DB", () => {
|
||||
function clean() {
|
||||
|
||||
}
|
||||
|
||||
const createdIds: number[] = [];
|
||||
|
||||
const pauseRepo = new PauseCancelTaskRepository(postgrClient)
|
||||
|
||||
before(() => {
|
||||
|
||||
})
|
||||
after(() => {
|
||||
|
||||
})
|
||||
|
||||
it("Should create a task", async () => {
|
||||
const created = await pauseRepo.addTask(testTask)
|
||||
assert.ok(created != undefined, "A value must be returned always")
|
||||
assert.ok(created.error == undefined, "Should not return a error")
|
||||
assert.ok(created.data != undefined, "Data mus be returned")
|
||||
createdIds.push(created.data.id)
|
||||
})
|
||||
|
||||
it("Should update a existing task", async () => {
|
||||
const updated = await pauseRepo.updateTask({
|
||||
id: createdIds[0],
|
||||
next_check: new Date()
|
||||
})
|
||||
|
||||
assert.ok(updated != undefined, "A value must be returned always")
|
||||
assert.ok(updated.error == undefined, "Should not return a error")
|
||||
assert.ok(updated.data != undefined, "Data mus be returned")
|
||||
})
|
||||
|
||||
it("Should finish a existing task", async () => {
|
||||
const finish = await pauseRepo.finishTask({
|
||||
id: createdIds[0],
|
||||
error: "ok"
|
||||
})
|
||||
|
||||
assert.ok(finish != undefined, "A value must be returned always")
|
||||
assert.ok(finish.error == undefined, "Should not return a error")
|
||||
assert.ok(finish.data != undefined, "Data mus be returned")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,121 @@
|
||||
import { Result } from "sim-shared/domain/Result.js";
|
||||
import { QueryResult } from "pg";
|
||||
import { PgClient } from "sim-shared/infrastructure/PgClient.js";
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
export type PauseCancelTask = {
|
||||
id: number;
|
||||
iccid: string;
|
||||
last_checked?: Date | null;
|
||||
activation_date?: Date | null;
|
||||
next_check?: Date | null;
|
||||
completed_date?: Date | null;
|
||||
error?: string | null;
|
||||
}
|
||||
|
||||
export type CreatePauseCancelTaskDTO = Pick<PauseCancelTask, "iccid" | "activation_date" | "next_check">
|
||||
export type UpdatePauseCancelTaskDTO = Pick<PauseCancelTask, "id" | "next_check">
|
||||
export type FinishPauseCancelTaskDTO = Pick<PauseCancelTask, "id" | "error">
|
||||
|
||||
export class PauseCancelTaskRepository {
|
||||
constructor(
|
||||
private readonly pgClient: PgClient
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene las siguientes que se pueden lanzar, puede haber más pero
|
||||
* estan pendientes
|
||||
*/
|
||||
public async getPending(): Promise<Result<string, PauseCancelTask[]>> {
|
||||
const sql = `
|
||||
SELECT * FROM pause_cancel_tasks
|
||||
WHERE completed_date IS NULL
|
||||
AND (next_check <= NOW() OR next_check IS NULL)
|
||||
ORDER BY id ASC;
|
||||
`;
|
||||
|
||||
try {
|
||||
const res: QueryResult<PauseCancelTask> = await this.pgClient.query(sql);
|
||||
return {
|
||||
data: res.rows
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
error: (e as AxiosError).message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async addTask(task: CreatePauseCancelTaskDTO): Promise<Result<string, PauseCancelTask>> {
|
||||
|
||||
const sql = `
|
||||
INSERT INTO pause_cancel_tasks (iccid, activation_date, next_check, last_checked)
|
||||
VALUES ($1, $2, $3, now())
|
||||
RETURNING *;
|
||||
`;
|
||||
try {
|
||||
const values = [task.iccid, task.activation_date, task.next_check];
|
||||
const res: QueryResult<PauseCancelTask> = await this.pgClient.query(sql, values);
|
||||
return {
|
||||
data: res.rows[0]
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
error: (e as AxiosError).message
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Se ha vuelto a comprobar la tarea pero sigue en test
|
||||
*/
|
||||
public async updateTask(updateData: UpdatePauseCancelTaskDTO): Promise<Result<string, PauseCancelTask>> {
|
||||
|
||||
const sql = `
|
||||
UPDATE pause_cancel_tasks
|
||||
SET last_checked = now(), next_check = $1
|
||||
WHERE id = $2
|
||||
RETURNING *;
|
||||
`;
|
||||
try {
|
||||
const res = await this.pgClient.query<PauseCancelTask>(sql, [updateData.next_check, updateData.id]);
|
||||
return {
|
||||
data: res.rows[0]
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
error: (e as AxiosError).message
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* La tarea ha termiando bien o mal
|
||||
*/
|
||||
public async finishTask(finishData: FinishPauseCancelTaskDTO) {
|
||||
const sql = `
|
||||
UPDATE pause_cancel_tasks
|
||||
SET completed_date = NOW(), error = $1
|
||||
WHERE id = $2
|
||||
RETURNING *;
|
||||
`;
|
||||
|
||||
try {
|
||||
const res = await this.pgClient.query(sql, [finishData.error, finishData.id]);
|
||||
return {
|
||||
data: res.rows[0]
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
error: (e as AxiosError).message
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -53,7 +53,7 @@
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"test": "node --import tsx --test ./**/*.test.ts",
|
||||
"dev": "tsx watch index.ts",
|
||||
"build": "tsc --build && yarn tsc-alias -p tsconfig.json && cp .env package.json ../../dist/packages/sim-consumidor-objenious/",
|
||||
"start": "node ../../dist/packages/sim-consumidor-objenious/index.js",
|
||||
|
||||
Reference in New Issue
Block a user