45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { Elysia } from 'elysia'
|
|
import { createMerchantBody, createMerchantResponseSchema } from './schema'
|
|
import CreateMerchantService from './commands/service'
|
|
import { basicAuthMiddleware } from '~/middlewares/basicAuth'
|
|
import { DataAlreadyExistsError } from '~/helpers/errors'
|
|
|
|
export const router = new Elysia({
|
|
name: 'modules.auth',
|
|
detail: { tags: ['Auth'] },
|
|
prefix: '/auth',
|
|
})
|
|
.post('/get-key', async ({ body, headers, set }) => {
|
|
basicAuthMiddleware(headers, set)
|
|
|
|
try {
|
|
const result = await CreateMerchantService.createMerchant(body)
|
|
return {
|
|
status: 'success' as const,
|
|
message: 'Merchant created successfully' as const,
|
|
data: result,
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof DataAlreadyExistsError) {
|
|
set.status = 409
|
|
return {
|
|
status: 'conflict' as const,
|
|
message: 'Merchant with this email or phone already exists' as const,
|
|
}
|
|
}
|
|
set.status = 500
|
|
return {
|
|
status: 'internal_error' as const,
|
|
message: 'Server error' as const,
|
|
}
|
|
}
|
|
}, {
|
|
body: createMerchantBody,
|
|
response: createMerchantResponseSchema,
|
|
detail: {
|
|
security: [{ basicAuth: [] }],
|
|
},
|
|
})
|
|
|
|
export default router
|