Repositorio de codigos de activacion + test
This commit is contained in:
@@ -4,6 +4,7 @@ import { httpclient } from "config/httpclient.config.js";
|
||||
import { pgClient } from "config/pgclient.config.js";
|
||||
import { NfcRepository } from "./Nfc.repository.js";
|
||||
import type { ServerContext } from "domain/ServerContext.js";
|
||||
import { uuidv7 } from "uuidv7";
|
||||
|
||||
describe("NfcRepository Integration Tests", () => {
|
||||
const serverContext: ServerContext = {
|
||||
@@ -12,7 +13,7 @@ describe("NfcRepository Integration Tests", () => {
|
||||
};
|
||||
|
||||
const repo = new NfcRepository(serverContext);
|
||||
const testCardId = "test-card-" + Date.now();
|
||||
const testCardId = uuidv7()
|
||||
const testCode = "12345678";
|
||||
|
||||
// Clean up before and after tests to ensure isolation
|
||||
@@ -61,12 +62,14 @@ describe("NfcRepository Integration Tests", () => {
|
||||
});
|
||||
|
||||
test("findActivationCodes should return empty array if card has no codes", async () => {
|
||||
const result = await repo.findActivationCodes("non-existent-card");
|
||||
const nonExistentCard = uuidv7()
|
||||
const result = await repo.findActivationCodes(nonExistentCard);
|
||||
|
||||
if (result.error) {
|
||||
assert.fail(`findActivationCodes failed: ${result.error}`);
|
||||
}
|
||||
|
||||
|
||||
assert.ok(result.data, "Data should be returned");
|
||||
assert.strictEqual(result.data.length, 0);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ActivationCodeDTO } from "domain/NfcRegistry.js";
|
||||
import type { Result } from "domain/Result.js";
|
||||
import type { ServerContext } from "domain/ServerContext.js";
|
||||
import { constrainedMemory } from "node:process";
|
||||
|
||||
// TODO: Pasar a Result<E,T>
|
||||
|
||||
@@ -26,6 +27,7 @@ export class NfcRepository {
|
||||
data: codeResult.rows
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return {
|
||||
error: e as string
|
||||
}
|
||||
@@ -33,6 +35,8 @@ export class NfcRepository {
|
||||
}
|
||||
|
||||
public async createActivationCode(args: { cardId: string, code: string }): Promise<Result<string, ActivationCodeDTO>> {
|
||||
const conn = await this.ctx.PostgresClient.connect()
|
||||
await conn.query("BEGIN")
|
||||
const query = `
|
||||
INSERT INTO activation_codes(
|
||||
card_id,
|
||||
@@ -44,18 +48,19 @@ export class NfcRepository {
|
||||
$2,
|
||||
digest($2,'sha256')
|
||||
)
|
||||
RETURNING(
|
||||
RETURNING
|
||||
code_id,card_id,code_plain,code_hash,is_used,is_blocked,failed_attempts,created_at,expires_at
|
||||
)
|
||||
`
|
||||
const values = [args.cardId, args.code]
|
||||
try {
|
||||
const insertResult = await this.ctx.PostgresClient.query<ActivationCodeDTO>(query, values)
|
||||
const insertResult = await conn.query<ActivationCodeDTO>(query, values)
|
||||
await conn.query("COMMIT")
|
||||
return {
|
||||
data: insertResult.rows[0]!
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error createActivationCode: ", e)
|
||||
await conn.query("ROLLBACK")
|
||||
return {
|
||||
error: e as string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user