forked from OdyxeeeLabs/Perigee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvaults.ts
More file actions
52 lines (46 loc) · 1.61 KB
/
Copy pathvaults.ts
File metadata and controls
52 lines (46 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Request, Response, Router } from 'express';
import { CreateVaultSchema, CreateVaultDto } from '../dtos/create-vault.dto';
const router = Router();
/**
* A mock database or service layer to interact with.
* In a real application, this would be a proper service class.
*/
const vaultService = {
create: async (data: CreateVaultDto) => {
console.log('Creating vault with validated data:', data);
// Simulate database record creation
const newVault = {
id: crypto.randomUUID(),
...data,
createdAt: new Date().toISOString(),
};
return newVault;
},
};
/**
* POST /vaults
*
* Creates a new white-label vault. The request body is validated against
* the CreateVaultSchema to ensure the managerId is a valid UUID, the name
* is non-empty, and the markup does not exceed the policy ceiling.
*/
router.post('/vaults', async (req: Request, res: Response) => {
// 1. Parse and validate the incoming request body using the Zod schema.
const validationResult = CreateVaultSchema.safeParse(req.body);
// 2. If validation fails, return a 400 Bad Request response.
if (!validationResult.success) {
return res.status(400).json({
error: 'Validation failed',
issues: validationResult.error.flatten().fieldErrors,
});
}
// 3. If validation succeeds, proceed with the business logic.
try {
const newVault = await vaultService.create(validationResult.data);
return res.status(201).json(newVault);
} catch (error) {
console.error('Failed to create vault:', error);
return res.status(500).json({ error: 'Internal Server Error' });
}
});
export default router;