u
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import express from 'express';
|
||||
import * as db from '../../db/brand.js';
|
||||
import { emit } from '../sse.js';
|
||||
|
||||
const AVIF_BODY_LIMIT = 10 * 1024 * 1024;
|
||||
|
||||
@@ -18,11 +19,20 @@ function brandErrorStatus(error) {
|
||||
if (error.message === 'Company not found') return 404;
|
||||
if (error.message === 'Brand not found') return 404;
|
||||
if (error.message === 'Invalid brand') return 400;
|
||||
if (error.message === 'Brand already exists') return 409;
|
||||
if (error.message === 'Brand in use') return 409;
|
||||
if (error.message === 'Image not found') return 404;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/** Notify SSE subscribers after brand change; payload is `{ sender, brandId }` so receivers know which brand changed and the sender can ignore its own event. */
|
||||
function emitBrandSignal(req, brandId = null) {
|
||||
const companyId = req.user?.selectedCompanyId;
|
||||
if (companyId == null) return;
|
||||
const clientId = req.headers?.['x-client-id'];
|
||||
emit('brand', { companyId, data: { sender: typeof clientId === 'string' ? clientId : '', brandId } });
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers brand routes for the authenticated user's selected company.
|
||||
* @param {import('express').Application} app
|
||||
@@ -75,14 +85,44 @@ export function register(app, basePath = '') {
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* put:
|
||||
*/
|
||||
app.get(`${basePath}/brand`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const brands = await db.list(req.user.id);
|
||||
res.status(200).json({ brands });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
if (status === 500) console.error('List brands failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /brand:
|
||||
* post:
|
||||
* tags: [Brand]
|
||||
* summary: Upsert brand
|
||||
* summary: Create brand
|
||||
* description: |
|
||||
* Insert or update brand by `name` for the selected company. `gsprpinfo` optional.
|
||||
* Creates a brand for the selected company. Fails with `Brand already exists`
|
||||
* if the name is taken. Emits SSE signal `brand` with payload
|
||||
* `{ sender, brandId }` (`sender` echoes the caller's `x-client-id`).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: brand/upsert.md
|
||||
* x-signals: [brand]
|
||||
* x-markdown: brand/create.md
|
||||
* parameters:
|
||||
* - in: header
|
||||
* name: x-client-id
|
||||
* required: false
|
||||
* schema:
|
||||
* type: string
|
||||
* description: Sender id echoed as SSE payload so the sender can ignore its own event.
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
@@ -99,8 +139,8 @@ export function register(app, basePath = '') {
|
||||
* name: Acme
|
||||
* gsprpinfo: 'Acme GmbH'
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Upserted brand
|
||||
* 201:
|
||||
* description: Created brand
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
@@ -132,24 +172,14 @@ export function register(app, basePath = '') {
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 409:
|
||||
* description: Brand already exists
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/brand`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const brands = await db.list(req.user.id);
|
||||
res.status(200).json({ brands });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
if (status === 500) console.error('List brands failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.put(`${basePath}/brand`, async (req, res) => {
|
||||
app.post(`${basePath}/brand`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
@@ -158,14 +188,137 @@ export function register(app, basePath = '') {
|
||||
const { name, gsprpinfo } = req.body ?? {};
|
||||
|
||||
try {
|
||||
const brand = await db.upsert(req.user.id, {
|
||||
const brand = await db.create(req.user.id, {
|
||||
name,
|
||||
gsprpinfo,
|
||||
});
|
||||
emitBrandSignal(req, brand.id);
|
||||
res.status(201).json({ brand });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
if (status === 500) console.error('Create brand failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /brand/{id}:
|
||||
* patch:
|
||||
* tags: [Brand]
|
||||
* summary: Rename brand
|
||||
* description: |
|
||||
* Renames/updates a brand by id for the selected company. Fails with
|
||||
* `Brand already exists` if the name is taken by another brand.
|
||||
* When the name changes, linked articles (`brand_id`) get their
|
||||
* `updated_at` bumped; the response reports `touchedArticleCount`.
|
||||
* Emits SSE signal `brand` with payload `{ sender, brandId }`
|
||||
* (`sender` echoes the caller's `x-client-id`).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-signals: [brand]
|
||||
* x-markdown: brand/update.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* - in: header
|
||||
* name: x-client-id
|
||||
* required: false
|
||||
* schema:
|
||||
* type: string
|
||||
* description: Sender id echoed as SSE payload so the sender can ignore its own event.
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* gsprpinfo:
|
||||
* type: string
|
||||
* example:
|
||||
* name: Acme GmbH
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated brand
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* brand:
|
||||
* type: object
|
||||
* required: [id, name, touchedArticleCount]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* name:
|
||||
* type: string
|
||||
* gsprpinfo:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* touchedArticleCount:
|
||||
* type: integer
|
||||
* description: Number of linked articles whose updated_at was bumped.
|
||||
* 400:
|
||||
* description: Invalid brand, invalid id, or no company selected
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 401:
|
||||
* description: Unauthorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 403:
|
||||
* description: Not authorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Brand or company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 409:
|
||||
* description: Brand already exists
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.patch(`${basePath}/brand/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return res.status(400).json({ error: 'id is required' });
|
||||
}
|
||||
|
||||
const { name, gsprpinfo } = req.body ?? {};
|
||||
|
||||
try {
|
||||
const brand = await db.update(req.user.id, id, {
|
||||
name,
|
||||
gsprpinfo,
|
||||
});
|
||||
emitBrandSignal(req, id);
|
||||
res.status(200).json({ brand });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
if (status === 500) console.error('Upsert brand failed:', error);
|
||||
if (status === 500) console.error('Update brand failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
@@ -178,9 +331,11 @@ export function register(app, basePath = '') {
|
||||
* summary: Delete brand
|
||||
* description: |
|
||||
* Deletes the brand for the selected company. Fails with `Brand in use`
|
||||
* if any article still references it.
|
||||
* if any article still references it. Emits SSE signal `brand`
|
||||
* with payload `{ sender, brandId }`.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-signals: [brand]
|
||||
* x-markdown: brand/delete.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
@@ -235,6 +390,7 @@ export function register(app, basePath = '') {
|
||||
|
||||
try {
|
||||
await db.remove(req.user.id, id);
|
||||
emitBrandSignal(req, id);
|
||||
res.sendStatus(204);
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
@@ -251,9 +407,11 @@ export function register(app, basePath = '') {
|
||||
* summary: Add or replace brand image
|
||||
* description: |
|
||||
* Uploads an image for the brand (AVIF, WebP, or octet-stream). Converted to AVIF full and thumbnail.
|
||||
* Replaces any existing image for this brand.
|
||||
* Replaces any existing image for this brand. Emits SSE signal `brand`
|
||||
* with payload `{ sender, brandId }`.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-signals: [brand]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
@@ -328,9 +486,10 @@ export function register(app, basePath = '') {
|
||||
* delete:
|
||||
* tags: [Brand]
|
||||
* summary: Delete brand image
|
||||
* description: Removes the brand's image. Sets thumbW, thumbH, thumbHash back to null.
|
||||
* description: Removes the brand's image. Sets thumbW, thumbH, thumbHash back to null. Emits SSE signal `brand` with payload `{ sender, brandId }`.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-signals: [brand]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
@@ -410,6 +569,7 @@ export function register(app, basePath = '') {
|
||||
|
||||
try {
|
||||
const brand = await db.addImage(req.user.id, id, imageBuffer);
|
||||
emitBrandSignal(req, id);
|
||||
res.status(201).json({ brand });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
@@ -432,6 +592,7 @@ export function register(app, basePath = '') {
|
||||
|
||||
try {
|
||||
const brand = await db.deleteImage(req.user.id, id);
|
||||
emitBrandSignal(req, id);
|
||||
res.status(200).json({ brand });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
|
||||
@@ -38,6 +38,12 @@ export function register(app, basePath = '') {
|
||||
* | `category` | `POST /category`, `PATCH /category/{id}`, `PUT /category/order`, `DELETE /category/{id}`, `POST /category/{id}/image`, `DELETE /category/{id}/image` |
|
||||
* | `articles` | `POST /article`, `PATCH /article/{sku}`, `DELETE /article/{sku}` |
|
||||
* | `article-<sku>` | `PUT /article/{sku}/weight`, `PUT /article/{sku}/prices`, `PUT /article/{sku}/eans`, `PUT /article/{sku}/categories`, `PUT /article/{sku}/brand`, `PUT /article/{sku}/images/order`, `POST /article/{sku}/image`, `DELETE /article/{sku}/image/{fullHash}` |
|
||||
* | `brand` | `POST /brand`, `PATCH /brand/{id}`, `DELETE /brand/{id}`, `POST /brand/{id}/image`, `DELETE /brand/{id}/image` |
|
||||
*
|
||||
* The `brand` signal carries a JSON payload `{ sender, brandId }`:
|
||||
* `sender` echoes the caller's `x-client-id` header (empty string if
|
||||
* absent) so the sender can ignore its own event; `brandId` is the
|
||||
* affected brand id (`null` only if unknown). Other signals send `{}`.
|
||||
*
|
||||
* Signals are only sent when the mutation actually succeeds (2xx).
|
||||
* Failed or no-op validation errors do not emit. The `article-<sku>`
|
||||
@@ -49,7 +55,8 @@ export function register(app, basePath = '') {
|
||||
* 200:
|
||||
* description: |
|
||||
* SSE stream (`text/event-stream`). Each signal is an SSE event
|
||||
* whose name is the signal id and whose data is `{}`.
|
||||
* whose name is the signal id. Most signals send `{}` as data;
|
||||
* `brand` sends `{ "sender": "<x-client-id>", "brandId": 12 }`.
|
||||
* content:
|
||||
* text/event-stream:
|
||||
* schema:
|
||||
@@ -63,6 +70,9 @@ export function register(app, basePath = '') {
|
||||
*
|
||||
* event: article-42000045
|
||||
* data: {}
|
||||
*
|
||||
* event: brand
|
||||
* data: {"sender":"tab-1","brandId":12}
|
||||
* 400:
|
||||
* description: No company selected
|
||||
* content:
|
||||
|
||||
Reference in New Issue
Block a user