Skip to main content

Assign Products to Brand

This endpoint is used to assign multiple products to a specific brand. It will update the brand reference for all specified products and automatically update the product count for the brand.

Endpoint

  • URL: /brands/assign-products
  • Method: POST

Authentication

  • Header: accessToken
  • Type: Bearer Token
  • Value: <ACCESS-TOKEN>

Request Headers

HeaderTypeDescription
Content-TypestringMust be application/json.
accessTokenstringAccess token for authentication. (required)

Request Body

The request body should be a JSON object containing the following fields:

Required Fields

FieldTypeDescription
idstringThe unique identifier of the brand.
productIdsarrayArray of product IDs (strings) to assign to the brand.

Sample Request Body

{
"id": "brand_id_here",
"productIds": ["product_id_1", "product_id_2", "product_id_3"]
}

Sample Request with Multiple Products

{
"id": "663a0cbe516313e5bc94aa87",
"productIds": [
"prod_123abc",
"prod_456def",
"prod_789ghi",
"prod_012jkl",
"prod_345mno"
]
}

Response

  • Status Code: 200 OK

Success Response Fields

FieldTypeDescription
successbooleanIndicates whether the products were assigned successfully.
messagestringA message conveying the outcome of the operation.

Example Success Response

{
"success": true,
"message": "Products assigned to brand successfully"
}

Detailed Success Response (if applicable)

Some implementations may include additional details:

{
"success": true,
"message": "Products assigned to brand successfully",
"data": {
"brandId": "brand_id_here",
"assignedCount": 3,
"skippedCount": 0,
"productCount": 15
}
}

Error Responses

Status CodeDescription
400Bad Request: Provided data is invalid or malformed.
401Unauthorized: Access token is invalid or missing.
404Not Found: Brand ID does not exist.
500Internal Server Error: An error occurred on the server.

Automatic Features

When products are assigned to a brand, the system automatically:

  1. Updates Product Records - Each specified product's brand field is updated to reference the brand.
  2. Updates Product Count - The brand's productCount field is recalculated based on all assigned products.
  3. Syncs with Elasticsearch - All affected products are updated in Elasticsearch for search functionality.
  4. Skips Already Assigned Products - Products that already have this brand assigned are not updated again (optimization).

Example Use Cases

Use Case 1: New Product Launch

When launching a new product line for Nike:

curl --location '<BASE-URL>/brands/assign-products' \
--header 'Content-Type: application/json' \
--header 'accessToken: <ACCESS-TOKEN>' \
--data '{
"id": "nike_brand_id",
"productIds": ["new_shoe_1", "new_shoe_2", "new_shoe_3"]
}'

Use Case 2: Bulk Brand Assignment

When importing products that need to be assigned to a brand:

{
"id": "adidas_brand_id",
"productIds": [
"prod_001",
"prod_002",
"prod_003",
"prod_004",
"prod_005",
"prod_006",
"prod_007",
"prod_008",
"prod_009",
"prod_010"
]
}

Use Case 3: Re-assigning Products

When moving products from one brand to another:

  1. First, unassign from the old brand (optional, as assigning will overwrite)
  2. Then, assign to the new brand using this endpoint

Important Notes

Product Validation

  • All product IDs in the array must exist in the system.
  • If any product ID is invalid, the operation may fail or skip that product (depends on implementation).

Performance

  • The endpoint is optimized to handle bulk assignments efficiently.
  • Large arrays (100+ products) are processed in batches internally.
  • Elasticsearch updates are queued and processed asynchronously for better performance.

Idempotent Operation

  • Calling this endpoint multiple times with the same data is safe.
  • Products already assigned to this brand will be skipped.
  • The product count will remain accurate regardless of duplicate calls.

Notes

  • Ensure the brand ID is valid and exists before making the request.
  • Product IDs must be valid MongoDB ObjectIDs or the format used by your system.
  • You must provide a valid accessToken for authentication.
  • The operation is transactional - either all products are assigned or none are.