116 lines
2.5 KiB
TypeScript
116 lines
2.5 KiB
TypeScript
import { Router } from 'express';
|
|
import { z } from 'zod';
|
|
import {
|
|
listAccounts,
|
|
createAccount,
|
|
updateAccount,
|
|
deleteAccount,
|
|
switchActiveAccount,
|
|
} from '../services/config';
|
|
import { authMiddleware, requireAdmin } from '../middleware/auth';
|
|
import { asyncHandler } from '../utils/asyncHandler';
|
|
|
|
const router = Router();
|
|
|
|
const accountSchema = z.object({
|
|
name: z.string().min(1),
|
|
accessKeyId: z.string().min(1),
|
|
accessKeySecret: z.string().min(1),
|
|
region: z.string().min(1),
|
|
endpoint: z.string().optional(),
|
|
});
|
|
|
|
router.get(
|
|
'/',
|
|
authMiddleware,
|
|
asyncHandler(async (_req, res) => {
|
|
const accounts = await listAccounts();
|
|
res.json({ code: 200, data: accounts });
|
|
})
|
|
);
|
|
|
|
router.post(
|
|
'/',
|
|
authMiddleware,
|
|
requireAdmin,
|
|
asyncHandler(async (req, res) => {
|
|
const parsed = accountSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
res.status(400).json({ code: 400, message: parsed.error.message });
|
|
return;
|
|
}
|
|
|
|
const account = await createAccount(parsed.data);
|
|
res.json({
|
|
code: 200,
|
|
data: {
|
|
id: account.id,
|
|
name: account.name,
|
|
accessKeyId: account.accessKeyId,
|
|
region: account.region,
|
|
endpoint: account.endpoint,
|
|
isActive: account.isActive,
|
|
createdAt: account.createdAt,
|
|
updatedAt: account.updatedAt,
|
|
},
|
|
});
|
|
})
|
|
);
|
|
|
|
router.put(
|
|
'/:id',
|
|
authMiddleware,
|
|
requireAdmin,
|
|
asyncHandler(async (req, res) => {
|
|
const parsed = accountSchema.partial().safeParse(req.body);
|
|
if (!parsed.success) {
|
|
res.status(400).json({ code: 400, message: parsed.error.message });
|
|
return;
|
|
}
|
|
|
|
const account = await updateAccount(req.params.id, parsed.data);
|
|
res.json({
|
|
code: 200,
|
|
data: {
|
|
id: account.id,
|
|
name: account.name,
|
|
accessKeyId: account.accessKeyId,
|
|
region: account.region,
|
|
endpoint: account.endpoint,
|
|
isActive: account.isActive,
|
|
createdAt: account.createdAt,
|
|
updatedAt: account.updatedAt,
|
|
},
|
|
});
|
|
})
|
|
);
|
|
|
|
router.delete(
|
|
'/:id',
|
|
authMiddleware,
|
|
requireAdmin,
|
|
asyncHandler(async (req, res) => {
|
|
await deleteAccount(req.params.id);
|
|
res.json({ code: 200, data: null });
|
|
})
|
|
);
|
|
|
|
router.post(
|
|
'/:id/switch',
|
|
authMiddleware,
|
|
requireAdmin,
|
|
asyncHandler(async (req, res) => {
|
|
const account = await switchActiveAccount(req.params.id);
|
|
res.json({
|
|
code: 200,
|
|
data: {
|
|
id: account.id,
|
|
name: account.name,
|
|
isActive: account.isActive,
|
|
},
|
|
});
|
|
})
|
|
);
|
|
|
|
export default router;
|