60 lines
1.5 KiB
TypeScript
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()
|
|
}
|