100 lines
2.7 KiB
TypeScript
Executable file
100 lines
2.7 KiB
TypeScript
Executable file
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Body,
|
|
Param,
|
|
Query,
|
|
HttpCode,
|
|
HttpStatus,
|
|
} from '@nestjs/common'
|
|
|
|
import { EntityType } from '../entities/attribute-definition.entity'
|
|
import { AttributeValueService } from '../services/attribute-value.service'
|
|
|
|
interface SetAttributesDto {
|
|
attributes: Record<string, any>
|
|
}
|
|
|
|
interface SetAttributeDto {
|
|
value: any
|
|
}
|
|
|
|
@Controller('attribute-values')
|
|
export class AttributeValueController {
|
|
constructor(
|
|
private readonly attributeValueService: AttributeValueService,
|
|
) {}
|
|
|
|
/**
|
|
* Get all attribute values for a specific entity
|
|
* GET /api/attribute-values?entityType=USER&entityId=uuid
|
|
* Returns: { "age_range": "25-34", "gender_identity": "female", ... }
|
|
*/
|
|
@Get()
|
|
async getAttributes(
|
|
@Query('entityType') entityType: EntityType,
|
|
@Query('entityId') entityId: string,
|
|
): Promise<Record<string, any>> {
|
|
return this.attributeValueService.getAttributes(entityType, entityId)
|
|
}
|
|
|
|
/**
|
|
* Set multiple attribute values at once (bulk upsert)
|
|
* POST /api/attribute-values?entityType=USER&entityId=uuid
|
|
* Body: { "attributes": { "age_range": "25-34", "gender_identity": "female" } }
|
|
*/
|
|
@Post()
|
|
@HttpCode(HttpStatus.OK)
|
|
async setAttributes(
|
|
@Query('entityType') entityType: EntityType,
|
|
@Query('entityId') entityId: string,
|
|
@Body() dto: SetAttributesDto,
|
|
): Promise<void> {
|
|
return this.attributeValueService.setAttributes(entityType, entityId, dto.attributes)
|
|
}
|
|
|
|
/**
|
|
* Set a single attribute value (upsert)
|
|
* PUT /api/attribute-values/:code?entityType=USER&entityId=uuid
|
|
* Body: { "value": "25-34" }
|
|
*/
|
|
@Put(':code')
|
|
async setAttribute(
|
|
@Param('code') code: string,
|
|
@Query('entityType') entityType: EntityType,
|
|
@Query('entityId') entityId: string,
|
|
@Body() dto: SetAttributeDto,
|
|
): Promise<void> {
|
|
await this.attributeValueService.setAttribute(entityType, entityId, code, dto.value)
|
|
}
|
|
|
|
/**
|
|
* Delete an attribute value
|
|
* DELETE /api/attribute-values/:code?entityType=USER&entityId=uuid
|
|
*/
|
|
@Delete(':code')
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
async deleteAttribute(
|
|
@Param('code') code: string,
|
|
@Query('entityType') entityType: EntityType,
|
|
@Query('entityId') entityId: string,
|
|
): Promise<void> {
|
|
return this.attributeValueService.deleteAttribute(entityType, entityId, code)
|
|
}
|
|
|
|
/**
|
|
* Get a single attribute value
|
|
* GET /api/attribute-values/:code?entityType=USER&entityId=uuid
|
|
*/
|
|
@Get(':code')
|
|
async getAttribute(
|
|
@Param('code') code: string,
|
|
@Query('entityType') entityType: EntityType,
|
|
@Query('entityId') entityId: string,
|
|
): Promise<any> {
|
|
return this.attributeValueService.getAttribute(entityType, entityId, code)
|
|
}
|
|
}
|