update
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import CustomField from '#base_modules/CustomField/model/custom_field'
|
||||
import { createCustomFieldValidator } from '#base_modules/CustomField/validators/custom_field_create_validator'
|
||||
|
||||
export default class CustomFieldsController {
|
||||
/**
|
||||
@@ -13,4 +14,27 @@ export default class CustomFieldsController {
|
||||
const fields = await CustomField.query().where('tenant_id', tenant.id).orderBy('id', 'asc')
|
||||
return response.ok({ data: fields })
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Create custom-field metadata for contacts in the current tenant
|
||||
* @requestBody {"entity_type":"contact","name":"Customer tier","key":"customer_tier","field_type":"select","required":true,"options":{"values":["bronze","silver","gold"]}}
|
||||
* @responseBody 201 - {"data":{"id":1,"entity_type":"contact","name":"Customer tier","key":"customer_tier","field_type":"select","required":true,"options":{"values":["bronze","silver","gold"]},"status":"active"}}
|
||||
* @responseBody 401 - {"message":"Unauthorized"}
|
||||
* @responseBody 403 - {"code":"E_TENANT_ACCESS_DENIED","message":"You do not have access to this tenant"}
|
||||
* @responseBody 422 - {"errors":[{"message":"The entity_type field must be contact","rule":"literal","field":"entity_type"}]}
|
||||
*/
|
||||
public async store({ request, response, tenant }: HttpContext) {
|
||||
const input = await createCustomFieldValidator.validate(request.all())
|
||||
const field = await CustomField.create({
|
||||
tenantId: tenant.id,
|
||||
entityType: input.entity_type,
|
||||
name: input.name,
|
||||
key: input.key,
|
||||
fieldType: input.field_type,
|
||||
required: input.required ?? false,
|
||||
options: input.options ?? null,
|
||||
status: 'active',
|
||||
})
|
||||
return response.created({ data: field })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ const CustomFieldsController = () =>
|
||||
router
|
||||
.group(() => {
|
||||
router.get('/', [CustomFieldsController, 'index'])
|
||||
router.post('/', [CustomFieldsController, 'store'])
|
||||
})
|
||||
.prefix('/api/custom-fields')
|
||||
.use([middleware.auth(), middleware.tenant()])
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import vine from '@vinejs/vine'
|
||||
import { jsonObjectSchema } from '#validators/schemas/json_object_schema'
|
||||
|
||||
export const createCustomFieldValidator = vine.compile(
|
||||
vine.object({
|
||||
entity_type: vine.literal('contact'),
|
||||
name: vine.string().trim().minLength(1),
|
||||
key: vine
|
||||
.string()
|
||||
.trim()
|
||||
.minLength(1)
|
||||
.regex(/^[a-z][a-z0-9_]*$/),
|
||||
field_type: vine.enum(['text', 'number', 'boolean', 'date', 'select'] as const),
|
||||
required: vine.boolean().optional(),
|
||||
options: jsonObjectSchema.optional(),
|
||||
})
|
||||
)
|
||||
Reference in New Issue
Block a user