zhaoyingbo 6e65581bbf
All checks were successful
Egg CI/CD / build-image (push) Successful in 32s
Egg CI/CD / deploy (push) Successful in 37s
feat: 接入lint 和 husky
2024-07-25 01:48:22 +00:00

60 lines
1.5 KiB
TypeScript

import db from "../../db"
import service from "../../services"
import netTool from "../../services/netTool"
import { SheetProxy } from "../../types/sheetProxy"
const validateSheetReq = async (body: SheetProxy.Body) => {
if (!body.api_key) {
return netTool.badRequest("api_key is required")
}
if (!body.sheet_token) {
return netTool.badRequest("sheet_token is required")
}
if (!body.range) {
return netTool.badRequest("range is required")
}
if (!body.values) {
return netTool.badRequest("values is required")
}
if (!SheetProxy.isType(body.type)) {
return netTool.badRequest("type is invalid")
}
return false
}
export const manageSheetReq = async (req: Request) => {
const body = (await req.json()) as SheetProxy.Body
// 校验参数
const validateRes = await validateSheetReq(body)
if (validateRes) return validateRes
// 校验api_key
const apiKeyInfo = await db.apiKey.getOne(body.api_key)
if (!apiKeyInfo) {
return netTool.notFound("api key not found")
}
// 获取 app name
const appName = apiKeyInfo.expand?.app?.name
if (!appName) {
return netTool.notFound("app name not found")
}
if (body.type === "insert") {
// 插入行
const insertRes = await service.lark.sheet.insertRows(appName)(
body.sheet_token,
body.range,
body.values
)
if (insertRes?.code !== 0) {
return netTool.serverError(insertRes?.msg, insertRes?.data)
}
// 返回
return netTool.ok(insertRes?.data)
}
return netTool.ok()
}