feat: add vehicle selection and downgrade flow - M5 (refs #55)

This commit is contained in:
Eric Gullickson
2026-01-18 16:44:45 -06:00
parent 94d1c677bc
commit 6c1a100eb9
11 changed files with 509 additions and 7 deletions

View File

@@ -246,4 +246,65 @@ export class SubscriptionsController {
});
}
}
/**
* POST /api/subscriptions/downgrade - Downgrade subscription with vehicle selection
*/
async downgrade(
request: FastifyRequest<{
Body: {
targetTier: 'free' | 'pro';
vehicleIdsToKeep: string[];
};
}>,
reply: FastifyReply
): Promise<void> {
try {
const userId = (request as any).user.sub;
const { targetTier, vehicleIdsToKeep } = request.body;
// Validate inputs
if (!targetTier || !vehicleIdsToKeep) {
reply.status(400).send({
error: 'Missing required fields',
message: 'targetTier and vehicleIdsToKeep are required',
});
return;
}
if (!['free', 'pro'].includes(targetTier)) {
reply.status(400).send({
error: 'Invalid tier',
message: 'targetTier must be "free" or "pro"',
});
return;
}
if (!Array.isArray(vehicleIdsToKeep)) {
reply.status(400).send({
error: 'Invalid vehicle selection',
message: 'vehicleIdsToKeep must be an array',
});
return;
}
// Downgrade subscription
const updatedSubscription = await this.service.downgradeSubscription(
userId,
targetTier,
vehicleIdsToKeep
);
reply.status(200).send(updatedSubscription);
} catch (error: any) {
logger.error('Failed to downgrade subscription', {
userId: (request as any).user?.sub,
error: error.message,
});
reply.status(500).send({
error: 'Failed to downgrade subscription',
message: error.message,
});
}
}
}