adding brand management

This commit is contained in:
fahed
2026-02-10 21:03:36 +03:00
parent 334727b232
commit f3e6fc848d
15 changed files with 568 additions and 864 deletions

View File

@@ -591,10 +591,10 @@ app.get('/api/brands', requireAuth, async (req, res) => {
});
app.post('/api/brands', requireAuth, requireRole('superadmin', 'manager'), async (req, res) => {
const { name, priority, color, icon } = req.body;
const { name, name_ar, priority, color, icon } = req.body;
if (!name) return res.status(400).json({ error: 'Name is required' });
try {
const created = await nocodb.create('Brands', { name, priority: priority || 2, color: color || null, icon: icon || null });
const created = await nocodb.create('Brands', { name, name_ar: name_ar || null, priority: priority || 2, color: color || null, icon: icon || null });
const brand = await nocodb.get('Brands', created.Id);
res.status(201).json(brand);
} catch (err) {
@@ -607,7 +607,7 @@ app.patch('/api/brands/:id', requireAuth, requireRole('superadmin', 'manager'),
const existing = await nocodb.get('Brands', req.params.id);
if (!existing) return res.status(404).json({ error: 'Brand not found' });
const data = {};
for (const f of ['name', 'priority', 'color', 'icon']) {
for (const f of ['name', 'name_ar', 'priority', 'color', 'icon', 'logo']) {
if (req.body[f] !== undefined) data[f] = req.body[f];
}
if (Object.keys(data).length === 0) return res.status(400).json({ error: 'No fields to update' });
@@ -619,6 +619,50 @@ app.patch('/api/brands/:id', requireAuth, requireRole('superadmin', 'manager'),
}
});
app.post('/api/brands/:id/logo', requireAuth, requireRole('superadmin', 'manager'), upload.single('file'), async (req, res) => {
try {
const existing = await nocodb.get('Brands', req.params.id);
if (!existing) return res.status(404).json({ error: 'Brand not found' });
if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
await nocodb.update('Brands', req.params.id, { logo: req.file.filename });
const brand = await nocodb.get('Brands', req.params.id);
res.json(brand);
} catch (err) {
res.status(500).json({ error: 'Failed to upload logo' });
}
});
app.delete('/api/brands/:id', requireAuth, requireRole('superadmin', 'manager'), async (req, res) => {
try {
const existing = await nocodb.get('Brands', req.params.id);
if (!existing) return res.status(404).json({ error: 'Brand not found' });
await nocodb.delete('Brands', req.params.id);
res.json({ success: true });
} catch (err) {
res.status(500).json({ error: 'Failed to delete brand' });
}
});
// One-time: copy Arabic names from "name" to "name_ar" for brands that have no name_ar yet
app.post('/api/brands/migrate-names', requireAuth, requireRole('superadmin'), async (req, res) => {
try {
const brands = await nocodb.list('Brands', { limit: 200 });
const updates = [];
for (const b of brands) {
if (!b.name_ar && b.name) {
updates.push({ Id: b.Id, name_ar: b.name });
}
}
if (updates.length > 0) {
await nocodb.bulkUpdate('Brands', updates);
}
res.json({ migrated: updates.length, message: `Copied ${updates.length} brand name(s) to name_ar. Now update the "name" field with English names in NocoDB.` });
} catch (err) {
console.error('Brand name migration failed:', err);
res.status(500).json({ error: 'Migration failed' });
}
});
// ─── POSTS ──────────────────────────────────────────────────────
app.get('/api/posts/stats', requireAuth, async (req, res) => {