Skip to main content

Unassign Products from Brand

This endpoint is used to remove the brand association from multiple products. It will clear the brand reference for all specified products and automatically update the product count for the brand.

Endpoint

  • URL: /brands/unassign-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 unassign from the brand.

Sample Request Body

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

Sample Request with Multiple Products

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

Response

  • Status Code: 200 OK

Success Response Fields

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

Example Success Response

{
"success": true,
"message": "Products unassigned from brand successfully"
}

Detailed Success Response (if applicable)

Some implementations may include additional details:

{
"success": true,
"message": "Products unassigned from brand successfully",
"data": {
"brandId": "brand_id_here",
"unassignedCount": 3,
"productCount": 12
}
}

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 unassigned from a brand, the system automatically:

  1. Updates Product Records - Each specified product's brand field is set to null or removed.
  2. Updates Product Count - The brand's productCount field is recalculated to reflect the remaining products.
  3. Syncs with Elasticsearch - All affected products are updated in Elasticsearch for search functionality.
  4. Safe Operation - Products that don't have this brand assigned are safely skipped (no errors).

Example Use Cases

Use Case 1: Discontinuing Product Line

When removing discontinued Nike products:

curl --location '<BASE-URL>/brands/unassign-products' \
--header 'Content-Type: application/json' \
--header 'accessToken: <ACCESS-TOKEN>' \
--data '{
"id": "nike_brand_id",
"productIds": ["old_shoe_1", "old_shoe_2", "old_shoe_3"]
}'

Use Case 2: Moving Products to Another Brand

When reassigning products from Adidas to Puma:

// Step 1: Unassign from Adidas
{
"id": "adidas_brand_id",
"productIds": ["prod_001", "prod_002", "prod_003"]
}

// Step 2: Assign to Puma (use assign-products endpoint)

Use Case 3: Clearing Brand Association

When products should not be associated with any brand:

{
"id": "generic_brand_id",
"productIds": ["prod_101", "prod_102", "prod_103", "prod_104", "prod_105"]
}

Important Notes

Product Validation

  • All product IDs in the array should exist in the system.
  • If a product ID doesn't exist or isn't associated with this brand, it will be skipped without causing an error.

Performance

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

Safe Operation

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

Difference from Delete Brand

  • Unassign Products: Removes brand association from specific products only.
  • Delete Brand: Removes the brand entirely AND unassigns it from ALL products.

Use this endpoint when:

  • You want to keep the brand but remove it from specific products
  • You're reorganizing product-brand relationships
  • You're preparing to assign products to a different brand

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 unassigned or none are.
  • After unassigning, products will have no brand association (brand field will be null).