Филиалы: цвет при создании и редактировании
This commit is contained in:
@@ -2106,8 +2106,10 @@ async function ensureBranchesTable() {
|
||||
name VARCHAR(200) NOT NULL UNIQUE,
|
||||
address TEXT,
|
||||
phone VARCHAR(50),
|
||||
color VARCHAR(7) NOT NULL DEFAULT '#2563eb',
|
||||
created_at TIMESTAMPTZ DEFAULT now()
|
||||
)`);
|
||||
await pool.query('ALTER TABLE branches ADD COLUMN IF NOT EXISTS color VARCHAR(7) NOT NULL DEFAULT \'#2563eb\'');
|
||||
await pool.query(`ALTER TABLE groups ADD COLUMN IF NOT EXISTS branch_id INTEGER REFERENCES branches(id) ON DELETE SET NULL`);
|
||||
await pool.query(`ALTER TABLE groups ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ`);
|
||||
await pool.query(`ALTER TABLE groups ADD COLUMN IF NOT EXISTS purge_at TIMESTAMPTZ`);
|
||||
@@ -7875,8 +7877,8 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
|
||||
await client.query('DELETE FROM branches');
|
||||
for (const x of ndata.branches) {
|
||||
await client.query(
|
||||
'INSERT INTO branches (id, name, address, phone, created_at) VALUES ($1,$2,$3,$4,$5)',
|
||||
[x.id, x.name, x.address, x.phone, x.created_at]
|
||||
'INSERT INTO branches (id, name, address, phone, color, created_at) VALUES ($1,$2,$3,$4,$5,$6)',
|
||||
[x.id, x.name, x.address, x.phone, normalizeUserColor(x.color, DEFAULT_USER_COLOR), x.created_at]
|
||||
);
|
||||
}
|
||||
for (const x of ndata.users) {
|
||||
@@ -8965,12 +8967,13 @@ app.get('/api/branches', requireAuth, async (req, res) => {
|
||||
app.post('/api/branches', requireAdmin, async (req, res) => {
|
||||
const { name, address, phone } = req.body;
|
||||
if (!name?.trim()) return res.status(400).json({ error: 'Название обязательно' });
|
||||
const color = normalizeUserColor(req.body?.color, randomUserColor());
|
||||
try {
|
||||
const { rows } = await pool.query(
|
||||
'INSERT INTO branches (name, address, phone) VALUES ($1, $2, $3) RETURNING *',
|
||||
[name.trim(), address?.trim() || null, phone?.trim() || null]
|
||||
'INSERT INTO branches (name, address, phone, color) VALUES ($1, $2, $3, $4) RETURNING *',
|
||||
[name.trim(), address?.trim() || null, phone?.trim() || null, color]
|
||||
);
|
||||
await logAudit(req, 'branch.create', { id: rows[0].id, name: name.trim() });
|
||||
await logAudit(req, 'branch.create', { id: rows[0].id, name: name.trim(), color });
|
||||
invalidateGroups();
|
||||
res.status(201).json(rows[0]);
|
||||
} catch (e) {
|
||||
@@ -8982,17 +8985,19 @@ app.post('/api/branches', requireAdmin, async (req, res) => {
|
||||
app.put('/api/branches/:id', requireAdmin, async (req, res) => {
|
||||
const { name, address, phone } = req.body;
|
||||
if (!name?.trim()) return res.status(400).json({ error: 'Название обязательно' });
|
||||
const color = req.body?.color === undefined ? null : normalizeUserColor(req.body.color);
|
||||
try {
|
||||
const { rows } = await pool.query(
|
||||
`UPDATE branches SET
|
||||
name = $1,
|
||||
address = $2,
|
||||
phone = $3
|
||||
WHERE id = $4 RETURNING *`,
|
||||
[name.trim(), address?.trim() || null, phone?.trim() || null, req.params.id]
|
||||
phone = $3,
|
||||
color = COALESCE($4, color)
|
||||
WHERE id = $5 RETURNING *`,
|
||||
[name.trim(), address?.trim() || null, phone?.trim() || null, color, req.params.id]
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Не найдено' });
|
||||
await logAudit(req, 'branch.update', { id: req.params.id, ...req.body });
|
||||
await logAudit(req, 'branch.update', { id: req.params.id, ...req.body, color: color || undefined });
|
||||
invalidateGroups();
|
||||
res.json(rows[0]);
|
||||
} catch (e) {
|
||||
@@ -15139,7 +15144,7 @@ apiV1.get('/branches', async (req, res) => {
|
||||
where = ` WHERE b.id = ANY($${params.push(s.ids)}::int[])`;
|
||||
}
|
||||
const { rows } = await pool.query(
|
||||
`SELECT b.id, b.name, b.address, b.phone, b.created_at,
|
||||
`SELECT b.id, b.name, b.address, b.phone, b.color, b.created_at,
|
||||
count(g.id)::int AS groups_count
|
||||
FROM branches b
|
||||
LEFT JOIN groups g ON g.branch_id = b.id
|
||||
|
||||
Reference in New Issue
Block a user