feat(repos): spaces, projects, tasks with audit stub
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2
lib/db/repos/audit_stub.js
Normal file
2
lib/db/repos/audit_stub.js
Normal file
@@ -0,0 +1,2 @@
|
||||
// Replaced in Task 16 with real audit_log writes.
|
||||
export async function recordAudit() { /* noop until 006 migration lands */ }
|
||||
50
lib/db/repos/projects.js
Normal file
50
lib/db/repos/projects.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import { pool } from '../pool.js';
|
||||
import { recordAudit } from './audit_stub.js';
|
||||
|
||||
export async function create({ space_id, slug, name, description, status = 'active', started_at }, actor) {
|
||||
const { rows: [r] } = await pool.query(
|
||||
`INSERT INTO projects(space_id, slug, name, description, status, started_at)
|
||||
VALUES($1,$2,$3,$4,$5,$6) RETURNING *`,
|
||||
[space_id, slug, name, description || null, status, started_at || null]
|
||||
);
|
||||
await recordAudit(actor, 'create', 'project', r.id, null, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function getById(id) {
|
||||
const { rows: [r] } = await pool.query(`SELECT * FROM projects WHERE id=$1`, [id]);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function listBySpace(space_id, { status } = {}) {
|
||||
const sql = status
|
||||
? `SELECT * FROM projects WHERE space_id=$1 AND status=$2 ORDER BY name`
|
||||
: `SELECT * FROM projects WHERE space_id=$1 ORDER BY name`;
|
||||
const args = status ? [space_id, status] : [space_id];
|
||||
const { rows } = await pool.query(sql, args);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function update(id, patch, actor) {
|
||||
const before = await getById(id);
|
||||
const fields = ['slug','name','description','status','started_at','completed_at'];
|
||||
const sets = [], vals = [];
|
||||
let i = 1;
|
||||
for (const f of fields) {
|
||||
if (patch[f] !== undefined) { sets.push(`${f}=$${i++}`); vals.push(patch[f]); }
|
||||
}
|
||||
sets.push(`updated_at=now()`);
|
||||
vals.push(id);
|
||||
const { rows: [r] } = await pool.query(
|
||||
`UPDATE projects SET ${sets.join(', ')} WHERE id=$${i} RETURNING *`,
|
||||
vals
|
||||
);
|
||||
await recordAudit(actor, 'update', 'project', id, before, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function del(id, actor) {
|
||||
const before = await getById(id);
|
||||
await pool.query(`DELETE FROM projects WHERE id=$1`, [id]);
|
||||
await recordAudit(actor, 'delete', 'project', id, before, null);
|
||||
}
|
||||
53
lib/db/repos/spaces.js
Normal file
53
lib/db/repos/spaces.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import { pool } from '../pool.js';
|
||||
import { recordAudit } from './audit_stub.js';
|
||||
|
||||
export async function create({ slug, name, description, theme }, actor) {
|
||||
const { rows: [r] } = await pool.query(
|
||||
`INSERT INTO spaces(slug, name, description, theme)
|
||||
VALUES($1,$2,$3,$4) RETURNING *`,
|
||||
[slug, name, description || null, theme || null]
|
||||
);
|
||||
await recordAudit(actor, 'create', 'space', r.id, null, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function getById(id) {
|
||||
const { rows: [r] } = await pool.query(
|
||||
`SELECT * FROM spaces WHERE id=$1`, [id]);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function getBySlug(slug) {
|
||||
const { rows: [r] } = await pool.query(
|
||||
`SELECT * FROM spaces WHERE slug=$1`, [slug]);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function list() {
|
||||
const { rows } = await pool.query(`SELECT * FROM spaces ORDER BY name`);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function update(id, patch, actor) {
|
||||
const before = await getById(id);
|
||||
const fields = ['name','description','theme','slug'];
|
||||
const sets = [], vals = [];
|
||||
let i = 1;
|
||||
for (const f of fields) {
|
||||
if (patch[f] !== undefined) { sets.push(`${f}=$${i++}`); vals.push(patch[f]); }
|
||||
}
|
||||
sets.push(`updated_at=now()`);
|
||||
vals.push(id);
|
||||
const { rows: [r] } = await pool.query(
|
||||
`UPDATE spaces SET ${sets.join(', ')} WHERE id=$${i} RETURNING *`,
|
||||
vals
|
||||
);
|
||||
await recordAudit(actor, 'update', 'space', id, before, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function del(id, actor) {
|
||||
const before = await getById(id);
|
||||
await pool.query(`DELETE FROM spaces WHERE id=$1`, [id]);
|
||||
await recordAudit(actor, 'delete', 'space', id, before, null);
|
||||
}
|
||||
58
lib/db/repos/tasks.js
Normal file
58
lib/db/repos/tasks.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import { pool } from '../pool.js';
|
||||
import { recordAudit } from './audit_stub.js';
|
||||
|
||||
export async function create({ space_id, project_id = null, title, body, priority, due_at, position }, actor) {
|
||||
const { rows: [r] } = await pool.query(
|
||||
`INSERT INTO tasks(space_id, project_id, title, body, priority, due_at, position)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
|
||||
[space_id, project_id, title, body || null, priority || null, due_at || null, position || null]
|
||||
);
|
||||
await recordAudit(actor, 'create', 'task', r.id, null, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function getById(id) {
|
||||
const { rows: [r] } = await pool.query(`SELECT * FROM tasks WHERE id=$1`, [id]);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function listByProject(project_id) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT * FROM tasks WHERE project_id=$1 ORDER BY position NULLS LAST, created_at`,
|
||||
[project_id]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function listBySpace(space_id, { status } = {}) {
|
||||
const sql = status
|
||||
? `SELECT * FROM tasks WHERE space_id=$1 AND status=$2 ORDER BY created_at`
|
||||
: `SELECT * FROM tasks WHERE space_id=$1 ORDER BY created_at`;
|
||||
const { rows } = await pool.query(sql, status ? [space_id, status] : [space_id]);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function update(id, patch, actor) {
|
||||
const before = await getById(id);
|
||||
const fields = ['title','body','status','priority','due_at','position','project_id'];
|
||||
const sets = [], vals = [];
|
||||
let i = 1;
|
||||
for (const f of fields) {
|
||||
if (patch[f] !== undefined) { sets.push(`${f}=$${i++}`); vals.push(patch[f]); }
|
||||
}
|
||||
if (patch.status === 'done') { sets.push(`completed_at=now()`); }
|
||||
sets.push(`updated_at=now()`);
|
||||
vals.push(id);
|
||||
const { rows: [r] } = await pool.query(
|
||||
`UPDATE tasks SET ${sets.join(', ')} WHERE id=$${i} RETURNING *`,
|
||||
vals
|
||||
);
|
||||
await recordAudit(actor, 'update', 'task', id, before, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function del(id, actor) {
|
||||
const before = await getById(id);
|
||||
await pool.query(`DELETE FROM tasks WHERE id=$1`, [id]);
|
||||
await recordAudit(actor, 'delete', 'task', id, before, null);
|
||||
}
|
||||
Reference in New Issue
Block a user