feat(M08-A): 接入顾客端充值入口
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import type { FastifyInstance, FastifyReply } from 'fastify';
|
||||
import { z } from 'zod';
|
||||
import type { AuthRepository } from '../auth/auth-repository.js';
|
||||
import { authenticateAccessToken } from '../auth/authenticate.js';
|
||||
import type { AccessProfile } from '../auth/rbac-repository.js';
|
||||
import { RechargeError, type RechargeService } from '../wallets/recharge-service.js';
|
||||
|
||||
const listSchema = z.object({
|
||||
storeId: z.string().regex(/^[1-9]\d{0,19}$/).optional()
|
||||
});
|
||||
const createSchema = z.object({
|
||||
planId: z.string().regex(/^[1-9]\d{0,19}$/),
|
||||
storeId: z.string().regex(/^[1-9]\d{0,19}$/).nullable().optional(),
|
||||
clientRequestId: z.string().min(8).max(128)
|
||||
}).strict();
|
||||
|
||||
export interface RechargeRouteOptions {
|
||||
service: Pick<RechargeService, 'listAvailablePlans' | 'createRechargeOrder'>;
|
||||
authRepository: Pick<AuthRepository, 'validateSession'>;
|
||||
accessControl: { getAccessProfile(tenantId: string, userId: string): Promise<AccessProfile> };
|
||||
jwtSecret: string;
|
||||
}
|
||||
|
||||
export async function registerRechargeRoutes(
|
||||
app: FastifyInstance,
|
||||
options: RechargeRouteOptions
|
||||
) {
|
||||
app.get('/app-api/recharge/plans', async (request, reply) => {
|
||||
const auth = await authenticate(request.headers.authorization, options);
|
||||
const query = listSchema.safeParse(request.query);
|
||||
if (!auth) return unauthorized(reply, request.traceId);
|
||||
if (!query.success) return invalid(reply, request.traceId);
|
||||
return handle(reply, request.traceId, async () => ({
|
||||
code: 0,
|
||||
data: await options.service.listAvailablePlans({
|
||||
tenantId: auth.tenantId,
|
||||
storeId: query.data.storeId ?? null
|
||||
}),
|
||||
traceId: request.traceId
|
||||
}));
|
||||
});
|
||||
|
||||
app.post('/app-api/recharge/orders', async (request, reply) => {
|
||||
const auth = await authenticate(request.headers.authorization, options);
|
||||
const body = createSchema.safeParse(request.body);
|
||||
if (!auth) return unauthorized(reply, request.traceId);
|
||||
if (!body.success) return invalid(reply, request.traceId);
|
||||
return handle(reply, request.traceId, async () => reply.status(201).send({
|
||||
code: 0,
|
||||
data: await options.service.createRechargeOrder({
|
||||
tenantId: auth.tenantId,
|
||||
userId: auth.userId,
|
||||
planId: body.data.planId,
|
||||
storeId: body.data.storeId ?? null,
|
||||
clientRequestId: body.data.clientRequestId,
|
||||
traceId: request.traceId
|
||||
}),
|
||||
traceId: request.traceId
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
async function authenticate(
|
||||
authorization: string | undefined,
|
||||
options: RechargeRouteOptions
|
||||
) {
|
||||
const result = await authenticateAccessToken(
|
||||
authorization,
|
||||
options.authRepository,
|
||||
options.jwtSecret
|
||||
);
|
||||
if (!result) return null;
|
||||
const access = await options.accessControl.getAccessProfile(
|
||||
result.session.tenantId,
|
||||
result.session.user.id
|
||||
);
|
||||
if (!access.capabilities.includes('profile.read')) return null;
|
||||
return {
|
||||
tenantId: result.session.tenantId,
|
||||
userId: result.session.user.id
|
||||
};
|
||||
}
|
||||
|
||||
async function handle(reply: FastifyReply, traceId: string, work: () => Promise<unknown>) {
|
||||
try {
|
||||
return await work();
|
||||
} catch (error) {
|
||||
if (!(error instanceof RechargeError)) throw error;
|
||||
const status = error.code === 'RECHARGE_PLAN_NOT_FOUND'
|
||||
|| error.code === 'RECHARGE_ORDER_NOT_FOUND'
|
||||
? 404
|
||||
: error.code === 'RECHARGE_LIMIT_REACHED' ? 409 : 400;
|
||||
return reply.status(status).send({
|
||||
code: error.code,
|
||||
message: 'The recharge request is not available.',
|
||||
traceId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function unauthorized(reply: FastifyReply, traceId: string) {
|
||||
return reply.status(401).send({
|
||||
code: 'AUTH_SESSION_INVALID',
|
||||
message: 'Authentication required.',
|
||||
traceId
|
||||
});
|
||||
}
|
||||
|
||||
function invalid(reply: FastifyReply, traceId: string) {
|
||||
return reply.status(400).send({
|
||||
code: 'INVALID_RECHARGE_REQUEST',
|
||||
message: 'The recharge request is invalid.',
|
||||
traceId
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user