This commit is contained in:
seb
2026-09-24 23:26:25 +02:00
parent 075b1f0ddc
commit f8f61a1212
40 changed files with 1245 additions and 337 deletions

View File

@@ -0,0 +1,88 @@
export const CHECK_MANIFESTARTICLE_ACCESS = `
SELECT ma.id
FROM manifestarticle ma
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.id = @manifestarticleId AND m.company_id = @companyId;
`;
export const GET_LATEST_ROW = `
SELECT TOP 1
id,
manifestarticle_id,
transaction_type,
amount,
transaction_text,
hash_version,
prev_hash,
created_at
FROM ledger WITH (UPDLOCK, HOLDLOCK)
ORDER BY id DESC;
`;
export const INSERT_ROW = `
INSERT INTO ledger (
manifestarticle_id,
transaction_type,
amount,
transaction_text,
hash_version,
prev_hash
)
OUTPUT
INSERTED.id,
INSERTED.manifestarticle_id,
INSERTED.transaction_type,
INSERTED.amount,
INSERTED.transaction_text,
INSERTED.hash_version,
INSERTED.prev_hash,
INSERTED.created_at
VALUES (
@manifestarticleId,
@transactionType,
@amount,
@transactionText,
@hashVersion,
@prevHash
);
`;
export const LIST_BY_MANIFESTARTICLE = `
SELECT
l.id,
l.manifestarticle_id,
ma.sku,
l.transaction_type,
l.amount,
l.transaction_text,
l.hash_version,
l.prev_hash,
l.created_at
FROM ledger l
INNER JOIN manifestarticle ma ON l.manifestarticle_id = ma.id
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE l.manifestarticle_id = @manifestarticleId
AND m.company_id = @companyId
AND (@transactionType IS NULL OR l.transaction_type = @transactionType)
ORDER BY l.id ASC;
`;
export const LIST_BY_SKU = `
SELECT
l.id,
l.manifestarticle_id,
ma.sku,
l.transaction_type,
l.amount,
l.transaction_text,
l.hash_version,
l.prev_hash,
l.created_at
FROM ledger l
INNER JOIN manifestarticle ma ON l.manifestarticle_id = ma.id
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.sku = @sku
AND m.company_id = @companyId
AND (@transactionType IS NULL OR l.transaction_type = @transactionType)
ORDER BY l.id ASC;
`;

View File

@@ -600,6 +600,7 @@ BEGIN
transaction_text NVARCHAR(MAX) NOT NULL,
hash_version INT NOT NULL,
prev_hash NVARCHAR(128) NULL,
created_at DATETIME2 NOT NULL CONSTRAINT DF_ledger_created_at DEFAULT SYSUTCDATETIME(),
CONSTRAINT FK_ledger_manifestarticle FOREIGN KEY (manifestarticle_id) REFERENCES manifestarticle(id) ON DELETE NO ACTION
) WITH (LEDGER = ON (APPEND_ONLY = ON));
END

View File

@@ -385,6 +385,18 @@ export class BetterSqlite3Provider extends BetterSqlite3Executor {
this.db.exec(script);
}
}
// Ensure created_at exists on ledger table if created previously
try {
const ledgerCols = this.db.pragma('table_info("ledger")').map((c) => c.name);
if (ledgerCols.length > 0 && !ledgerCols.includes('created_at')) {
this.db.exec(
`ALTER TABLE ledger ADD COLUMN created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`,
);
}
} catch {
// ignore
}
}
/**

View File

@@ -0,0 +1,89 @@
export const CHECK_MANIFESTARTICLE_ACCESS = `
SELECT ma.id
FROM manifestarticle ma
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.id = @manifestarticleId AND m.company_id = @companyId;
`;
export const GET_LATEST_ROW = `
SELECT
id,
manifestarticle_id,
transaction_type,
amount,
transaction_text,
hash_version,
prev_hash,
created_at
FROM ledger
ORDER BY id DESC
LIMIT 1;
`;
export const INSERT_ROW = `
INSERT INTO ledger (
manifestarticle_id,
transaction_type,
amount,
transaction_text,
hash_version,
prev_hash
)
VALUES (
@manifestarticleId,
@transactionType,
@amount,
@transactionText,
@hashVersion,
@prevHash
)
RETURNING
id,
manifestarticle_id,
transaction_type,
amount,
transaction_text,
hash_version,
prev_hash,
created_at;
`;
export const LIST_BY_MANIFESTARTICLE = `
SELECT
l.id,
l.manifestarticle_id,
ma.sku,
l.transaction_type,
l.amount,
l.transaction_text,
l.hash_version,
l.prev_hash,
l.created_at
FROM ledger l
INNER JOIN manifestarticle ma ON l.manifestarticle_id = ma.id
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE l.manifestarticle_id = @manifestarticleId
AND m.company_id = @companyId
AND (@transactionType IS NULL OR l.transaction_type = @transactionType)
ORDER BY l.id ASC;
`;
export const LIST_BY_SKU = `
SELECT
l.id,
l.manifestarticle_id,
ma.sku,
l.transaction_type,
l.amount,
l.transaction_text,
l.hash_version,
l.prev_hash,
l.created_at
FROM ledger l
INNER JOIN manifestarticle ma ON l.manifestarticle_id = ma.id
INNER JOIN manifest m ON ma.manifest_id = m.id
WHERE ma.sku = @sku
AND m.company_id = @companyId
AND (@transactionType IS NULL OR l.transaction_type = @transactionType)
ORDER BY l.id ASC;
`;

View File

@@ -432,6 +432,7 @@ CREATE TABLE IF NOT EXISTS ledger (
transaction_text TEXT NOT NULL,
hash_version INTEGER NOT NULL,
prev_hash TEXT,
created_at TEXT NOT NULL DEFAULT (${UTC_NOW}),
FOREIGN KEY (manifestarticle_id) REFERENCES manifestarticle(id) ON DELETE NO ACTION
);
`;