Skip to content

Commit

Permalink
fix(sys-server): fix func & policy date & objectid type
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Dec 7, 2021
1 parent 8ba0761 commit 83cfe78
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 43 deletions.
6 changes: 3 additions & 3 deletions packages/system-server/src/api/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-12-07 09:42:03
* @LastEditTime: 2021-12-07 14:59:23
* @Description:
*/

Expand All @@ -24,8 +24,8 @@ export interface PolicyStruct {
rules: any
injector: string
hash: string
created_at: number
updated_at: number
created_at: Date
updated_at: Date
created_by: ObjectId
appid: string
}
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/lib/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export class ApplicationImporter {
rules: po.rules,
injector: po.injector ?? null,
hash: po.hash,
created_at: Date.now(),
updated_at: Date.now(),
created_at: new Date(),
updated_at: new Date(),
created_by: this.app.created_by,
appid: this.app.appid,
_id: undefined
Expand Down
9 changes: 5 additions & 4 deletions packages/system-server/src/router/function/create.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-01 13:07:07
* @LastEditTime: 2021-10-08 01:39:49
* @LastEditTime: 2021-12-07 14:49:40
* @Description:
*/

import { ObjectId } from 'bson'
import { Request, Response } from 'express'
import { ApplicationStruct } from '../../api/application'
import { checkPermission } from '../../api/permission'
Expand Down Expand Up @@ -54,9 +55,9 @@ export async function handleCreateFunction(req: Request, res: Response) {
label: body.label ?? 'New Function',
version: 0,
hash: hashFunctionCode(body.code),
created_at: Date.now(),
updated_at: Date.now(),
created_by: uid,
created_at: new Date(),
updated_at: new Date(),
created_by: new ObjectId(uid),
appid: app.appid
}

Expand Down
54 changes: 52 additions & 2 deletions packages/system-server/src/router/function/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-30 16:51:19
* @LastEditTime: 2021-11-05 14:12:38
* @LastEditTime: 2021-12-07 17:00:16
* @Description:
*/

Expand Down Expand Up @@ -149,4 +149,54 @@ export async function handleGetPublishedFunctions(req: Request, res: Response) {
return res.send({
data: docs
})
}
}


/**
* Get a function's change history
*/
export async function handleGetFunctionHistory(req: Request, res: Response) {
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 limit = Number(req.query?.limit || 10)
const page = Number(req.query?.page || 1)


// const doc = await getFunctionById(app.appid, new ObjectId(func_id))
const db = DatabaseAgent.db
const docs = await db.collection(Constants.cn.function_history)
.aggregate()
.match({
appid: app.appid,
func_id: new ObjectId(func_id),
})
.lookup({
from: Constants.cn.accounts,
localField: 'created_by',
foreignField: '_id',
as: 'account'
})
.project({
'account.password': 0,
'account.quota': 0,
'account.created_at': 0,
'account.updated_at': 0,
})
.skip((page - 1) * limit)
.limit(limit)
.sort({ created_at: -1 })
.toArray()

for (const doc of docs) {
doc.account = doc.account?.length ? doc.account[0] : null
}

return res.send({ data: docs })
}
16 changes: 13 additions & 3 deletions packages/system-server/src/router/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-08-29 11:35:05
* @LastEditTime: 2021-11-02 15:28:21
* @LastEditTime: 2021-12-07 22:34:06
* @Description:
*/

import { Router } from "express"
import { handleCreateFunction } from "./create"
import { handleGetAllFunctionTags, handleGetFunctionById, handleGetFunctions, handleGetPublishedFunctions } from "./get"
import { handleGetAllFunctionTags, handleGetFunctionById, handleGetFunctionHistory, handleGetFunctions, handleGetPublishedFunctions } from "./get"
import { handleGetFunctionLogs } from "./logs"
import { handlePublishFunctions, handlePublishOneFunction } from "./publish"
import { handleRemoveFunctionById } from "./remove"
import { handleCreateTrigger, handleRemoveTrigger, handleUpdateTrigger } from "./trigger"
import { handleUpdateFunction, handleUpdateFunctionCode } from "./update"
import { handleCompileFunctionCode, handleUpdateFunction, handleUpdateFunctionCode } from "./update"


export const FunctionRouter = Router()
Expand All @@ -32,6 +32,11 @@ FunctionRouter.get('/tags/all', handleGetAllFunctionTags)
*/
FunctionRouter.get('/:func_id', handleGetFunctionById)

/**
* Get a function change history
*/
FunctionRouter.get('/:func_id/changes', handleGetFunctionHistory)

/**
* Create a function
*/
Expand All @@ -47,6 +52,11 @@ FunctionRouter.post('/:func_id/info', handleUpdateFunction)
*/
FunctionRouter.post('/:func_id/code', handleUpdateFunctionCode)

/**
* Compile the function's code
*/
FunctionRouter.post('/:func_id/compile', handleCompileFunctionCode)

/**
* Remove a function by id of an application
*/
Expand Down
18 changes: 11 additions & 7 deletions packages/system-server/src/router/function/trigger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-03 19:55:26
* @LastEditTime: 2021-11-02 15:49:50
* @LastEditTime: 2021-12-07 15:21:42
* @Description:
*/

Expand Down Expand Up @@ -56,19 +56,21 @@ export async function handleCreateTrigger(req: Request, res: Response) {
duration: body.duration,
status: body.status ?? 0,
desc: body.desc,
created_at: Date.now(),
updated_at: Date.now()
created_at: new Date(),
updated_at: new Date()
}

let update_cmd: any = {
triggers: [trigger],
$inc: { version: 1 }
$inc: { version: 1 },
$set: { updated_at: new Date() }
}

if (func.triggers) {
update_cmd = {
$addToSet: { triggers: trigger },
$inc: { version: 1 }
$inc: { version: 1 },
$set: { updated_at: new Date() }
}
}

Expand Down Expand Up @@ -125,7 +127,8 @@ export async function handleUpdateTrigger(req: Request, res: Response) {
"triggers.$.duration": body.duration,
"triggers.$.status": body.status ?? 0,
"triggers.$.desc": body.desc,
"triggers.$.updated_at": Date.now(),
"triggers.$.updated_at": new Date(),
updated_at: new Date()
},
$inc: { version: 1 }
}
Expand Down Expand Up @@ -163,7 +166,8 @@ export async function handleRemoveTrigger(req: Request, res: Response) {
{ _id: new ObjectId(func_id), appid: app.appid },
{
$pull: { triggers: { _id: trigger_id, status: 0 } },
$inc: { version: 1 }
$inc: { version: 1 },
$set: { updated_at: new Date() }
}
)

Expand Down
67 changes: 51 additions & 16 deletions packages/system-server/src/router/function/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-05 02:11:39
* @LastEditTime: 2021-11-02 15:51:47
* @LastEditTime: 2021-12-07 22:33:13
* @Description:
*/

Expand All @@ -17,7 +17,7 @@ import { DatabaseAgent } from '../../lib/db-agent'
import { hashFunctionCode } from '../../utils/hash'
import { compileTs2js } from '../../utils/lang'

const { FUNCTION_UPDATE } = permissions
const { FUNCTION_UPDATE, FUNCTION_DEBUG } = permissions

/**
* Update function's basic info
Expand Down Expand Up @@ -66,10 +66,10 @@ export async function handleUpdateFunction(req: Request, res: Response) {
status: body.status ?? func.status,
tags: body.tags ?? func.tags,
label: body.label ?? func.label,
updated_at: Date.now()
updated_at: new Date()
}

// add cloud function
// update cloud function
const ret = await db.collection(Constants.cn.functions)
.updateOne({
_id: new ObjectId(func_id),
Expand Down Expand Up @@ -110,18 +110,7 @@ export async function handleUpdateFunctionCode(req: Request, res: Response) {
compiledCode: compileTs2js(body.code),
hash: hashFunctionCode(body.code),
debugParams: body.debugParams || func.debugParams,
updated_at: Date.now()
}

// record the function change history
if (data.hash !== func.hash) {
const record = Object.assign({}, func)
await db.collection(Constants.cn.function_history)
.insertOne({
func_id: func._id,
data: record,
created_at: Date.now()
})
updated_at: new Date()
}

// update cloud function
Expand All @@ -136,5 +125,51 @@ export async function handleUpdateFunctionCode(req: Request, res: Response) {

const doc = await getFunctionById(app.appid, new ObjectId(func_id))

// record the function change history
if (doc.hash !== func.hash) {
const record = Object.assign({}, doc)
await db.collection(Constants.cn.function_history)
.insertOne({
appid: app.appid,
func_id: func._id,
data: record,
created_at: new Date(),
created_by: new ObjectId(uid),
})
}

return res.send({ data: doc })
}

/**
* Compile function's code
*/
export async function handleCompileFunctionCode(req: Request, res: Response) {
const uid = req['auth']?.uid
const app: ApplicationStruct = req['parsed-app']
const func_id = req.params.func_id

// check permission
const code = await checkPermission(uid, FUNCTION_DEBUG.name, app)
if (code) {
return res.status(code).send()
}

const body = req.body
if (!body.code) return res.status(422).send('code cannot be empty')

const func = await getFunctionById(app.appid, new ObjectId(func_id))
if (!func) return res.status(422).send('function not found')

// build the func data
const data = {
...func,
code: body.code,
compiledCode: compileTs2js(body.code),
hash: hashFunctionCode(body.code),
debugParams: body.debugParams || func.debugParams,
updated_at: new Date()
}

return res.send({ data: data })
}
6 changes: 3 additions & 3 deletions packages/system-server/src/router/policy/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-03 23:19:36
* @LastEditTime: 2021-12-07 13:37:40
* @LastEditTime: 2021-12-07 14:58:57
* @Description:
*/

Expand Down Expand Up @@ -49,8 +49,8 @@ export async function handleCreatePolicy(req: Request, res: Response) {
rules: body.rules,
injector: body.injector,
hash: hashFunctionCode(JSON.stringify(body.rules)),
created_at: Date.now(),
updated_at: Date.now(),
created_at: new Date(),
updated_at: new Date(),
created_by: new ObjectId(uid),
appid: app.appid
}
Expand Down
6 changes: 3 additions & 3 deletions packages/system-server/src/router/policy/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-09-03 23:09:23
* @LastEditTime: 2021-10-19 21:56:11
* @LastEditTime: 2021-12-07 15:02:13
* @Description:
*/

Expand Down Expand Up @@ -50,7 +50,7 @@ export async function handleUpdatePolicy(req: Request, res: Response) {
description: body.description ?? policy.description,
status: body.status ?? policy.status,
injector: body.injector ?? policy.injector,
updated_at: Date.now(),
updated_at: new Date(),
}

// do db query
Expand Down Expand Up @@ -99,7 +99,7 @@ export async function handleUpdatePolicyRules(req: Request, res: Response) {
const data = {
rules: body.rules,
hash: hashFunctionCode(JSON.stringify(body.rules)),
updated_at: Date.now(),
updated_at: new Date(),
}

// do db query
Expand Down

0 comments on commit 83cfe78

Please sign in to comment.