All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import db from "../../db"
|
|
import { Context } from "../../types"
|
|
import { SheetProxy } from "../../types/sheetProxy"
|
|
|
|
/**
|
|
* 校验表格请求的参数
|
|
* @param {Context.Data} ctx - 上下文数据,包含请求体和响应生成器
|
|
* @returns {Promise<false | Response>} 如果校验失败,返回响应对象;否则返回 false
|
|
*/
|
|
const validateSheetReq = async (
|
|
ctx: Context.Data
|
|
): Promise<false | Response> => {
|
|
const { body, genResp } = ctx
|
|
if (!body.api_key) {
|
|
return genResp.badRequest("api_key is required")
|
|
}
|
|
if (!body.sheet_token) {
|
|
return genResp.badRequest("sheet_token is required")
|
|
}
|
|
if (!body.range) {
|
|
return genResp.badRequest("range is required")
|
|
}
|
|
if (!body.values) {
|
|
return genResp.badRequest("values is required")
|
|
}
|
|
|
|
if (!SheetProxy.isType(body.type)) {
|
|
return genResp.badRequest("type is invalid")
|
|
}
|
|
return false
|
|
}
|
|
|
|
/**
|
|
* 处理表格请求
|
|
* @param {Context.Data} ctx - 上下文数据,包含请求体、响应生成器和 Lark 服务
|
|
* @returns {Promise<Response>} 返回响应对象
|
|
*/
|
|
export const manageSheetReq = async (ctx: Context.Data): Promise<Response> => {
|
|
const { body: rawBody, genResp, larkService } = ctx
|
|
const body = rawBody as SheetProxy.Body
|
|
|
|
// 校验参数
|
|
const validateRes = await validateSheetReq(ctx)
|
|
if (validateRes) return validateRes
|
|
|
|
// 校验 api_key
|
|
const apiKeyInfo = await db.apiKey.getOne(body.api_key)
|
|
if (!apiKeyInfo) {
|
|
return genResp.notFound("api key not found")
|
|
}
|
|
|
|
// 获取 app name
|
|
const appName = apiKeyInfo.expand?.app?.name
|
|
if (!appName) {
|
|
return genResp.notFound("app name not found")
|
|
}
|
|
|
|
if (body.type === "insert") {
|
|
// 插入行
|
|
const insertRes = await larkService
|
|
.child(appName)
|
|
.sheet.insertRows(body.sheet_token, body.range, body.values)
|
|
if (insertRes?.code !== 0) {
|
|
return genResp.serverError(insertRes?.message)
|
|
}
|
|
// 返回插入结果
|
|
return genResp.ok(insertRes?.data)
|
|
}
|
|
|
|
// 默认返回成功响应
|
|
return genResp.ok()
|
|
}
|