step 7.2: done
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// Expedition routes: POST /api/expeditions/start, GET /api/expeditions/active, POST /api/expeditions/claim.
|
||||
const zod = require('zod');
|
||||
const { AppError } = require('../utils/errors');
|
||||
const { requireAuth } = require('../middleware/session');
|
||||
const { getCharacterIdForUser } = require('../services/characterService');
|
||||
const { startExpedition, getActive, claimExpedition } = require('../services/expeditionService');
|
||||
|
||||
function wrap(fn) {
|
||||
return (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/expeditions/start — start a new expedition.
|
||||
*/
|
||||
async function handleStart(req, res) {
|
||||
const parsed = zod.strictObject({
|
||||
defId: zod.number().int().positive(),
|
||||
}).safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
throw new AppError('VALIDATION_ERROR', 'Request validation failed.', 400);
|
||||
}
|
||||
|
||||
const { defId } = parsed.data;
|
||||
const characterId = getCharacterIdForUser(req.session.userId);
|
||||
const result = startExpedition(characterId, defId);
|
||||
res.json({ ok: true, data: result });
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/expeditions/active — get the active (unclaimed) expedition.
|
||||
*/
|
||||
async function handleActive(_req, res) {
|
||||
const characterId = getCharacterIdForUser(_req.session.userId);
|
||||
const result = getActive(characterId);
|
||||
res.json({ ok: true, data: result });
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/expeditions/claim — claim a finished expedition.
|
||||
*/
|
||||
async function handleClaim(req, res) {
|
||||
const parsed = zod.strictObject({
|
||||
expeditionId: zod.number().int().positive(),
|
||||
}).safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
throw new AppError('VALIDATION_ERROR', 'Request validation failed.', 400);
|
||||
}
|
||||
|
||||
const { expeditionId } = parsed.data;
|
||||
const characterId = getCharacterIdForUser(req.session.userId);
|
||||
const result = claimExpedition(characterId, expeditionId);
|
||||
res.json({ ok: true, data: result });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
routes: {
|
||||
postStart: wrap(handleStart),
|
||||
getActive: wrap(handleActive),
|
||||
postClaim: wrap(handleClaim),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user