Skip to content

Commit

Permalink
fix(system-server): fix get function error;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 3, 2021
1 parent bc18c90 commit ea20a61
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
30 changes: 29 additions & 1 deletion packages/system-server/src/router/function/get.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-09-02 16:34:40
* @LastEditTime: 2021-09-03 20:30:19
* @Description:
*/

import { CloudFunctionStruct } from 'cloud-function-engine'
import { Request, Response } from 'express'
import { ApplicationStruct } from '../../api/application'
import { checkPermission } from '../../api/permission'
Expand Down Expand Up @@ -72,4 +73,31 @@ export async function handleGetFunctions(req: Request, res: Response) {
limit: limit,
page
})
}


/**
* Get a function by id
*/
export async function handleGetFunctionById(req: Request, res: Response) {
const db = DatabaseAgent.sys_db
const app: ApplicationStruct = req['parsed-app']
const func_id = req.params.func_id

// check permission
const code = await checkPermission(req['auth']?.uid, FUNCTION_READ.name, app)
if (code) {
return res.status(code).send()
}

const coll = db.collection(Constants.cn.functions)

// do db query
const ret = await coll
.where({ _id: func_id })
.getOne<CloudFunctionStruct>()

return res.send({
data: ret.data
})
}
9 changes: 7 additions & 2 deletions packages/system-server/src/router/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:05
* @LastEditTime: 2021-09-01 13:07:39
* @LastEditTime: 2021-09-03 20:25:43
* @Description:
*/

import { Router } from "express"
import { handleCreateFunction } from "./create"
import { handleGetFunctions } from "./get"
import { handleGetFunctionById, handleGetFunctions } from "./get"
import { handlePublishFunctions } from "./publish"
import { handlePublishTriggers } from "./trigger"

Expand All @@ -19,6 +19,11 @@ export const FunctionRouter = Router()
*/
FunctionRouter.get('/', handleGetFunctions)

/**
* Get a function by id of an application
*/
FunctionRouter.get('/:func_id', handleGetFunctionById)

/**
* Create a function
*/
Expand Down
14 changes: 5 additions & 9 deletions packages/system-server/src/router/function/publish.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-09-01 11:18:22
* @LastEditTime: 2021-09-03 20:02:25
* @Description:
*/

import { Request, Response } from 'express'
import { getApplicationByAppid } from '../../api/application'
import { ApplicationStruct } from '../../api/application'
import { publishFunctions } from '../../api/function'
import { checkPermission } from '../../api/permission'
import Config from '../../config'
Expand All @@ -20,20 +20,16 @@ const { PUBLISH_FUNCTION } = permissions
* Publish functions
*/
export async function handlePublishFunctions(req: Request, res: Response) {
const appid = req.params.appid
const app = await getApplicationByAppid(appid)
if (!app) {
return res.status(422).send('app not found')
}
const uid = req['auth']?.uid
const app: ApplicationStruct = req['parsed-app']

// check permission
const code = await checkPermission(req['auth']?.uid, PUBLISH_FUNCTION.name, app)
const code = await checkPermission(uid, PUBLISH_FUNCTION.name, app)
if (code) {
return res.status(code).send()
}

try {

await publishFunctions(app)

return res.send({
Expand Down

0 comments on commit ea20a61

Please sign in to comment.