-
-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { DatabaseAgent } from '../../db' | ||
import { Request, Response } from "express" | ||
import { CONST_DICTS } from "../../constants" | ||
import { IApplicationData, getApplicationByAppid } from "../../support/application" | ||
import { checkPermission } from "../../support/permission" | ||
import { publishFunctions, publishOneFunction } from '../../support/function' | ||
import { publishAccessPolicies } from '../../support/policy' | ||
|
||
/** | ||
* handle put replicates | ||
*/ | ||
export async function handleUpdateReplicas(req: Request, res: Response) { | ||
const app: IApplicationData = req["parsed-app"] | ||
const uid = req["auth"]?.uid | ||
const body = req.body | ||
|
||
// check login | ||
if (!uid) { | ||
res.status(401).send() | ||
} | ||
|
||
// check permission | ||
const { REPLICATES_PUT } = CONST_DICTS.permissions | ||
const code = await checkPermission(uid, REPLICATES_PUT.name, app) | ||
if (code) { | ||
return res.status(code).send() | ||
} | ||
|
||
// check params | ||
const target_appid = body?.target_appid | ||
if (!target_appid) { | ||
res.status(422).send("invalid target_appid") | ||
} | ||
const types = ["all", "type"] | ||
const functions = body?.functions | ||
const fun_type = body?.functions?.type | ||
if (functions && !types.includes(fun_type)) { | ||
res.status(422).send(" invalid functions of type") | ||
} | ||
const policies = body?.policies | ||
const pol_type = body?.policies?.type | ||
if (policies && !types.includes(pol_type)) { | ||
res.status(422).send(" invalid policies of type") | ||
} | ||
|
||
// check target app | ||
const target_app = await getApplicationByAppid(target_appid) | ||
if (!target_app) { | ||
return res.status(422).send("invalid target_appid") | ||
} | ||
|
||
// push replicates | ||
const accessor = DatabaseAgent.sys_accessor | ||
const session = accessor.conn.startSession() | ||
try { | ||
await session.withTransaction(async () => { | ||
if (functions && 'all' === fun_type) { | ||
await publishFunctions(app) | ||
} | ||
if (functions && 'all' == pol_type) { | ||
await publishAccessPolicies(app) | ||
} | ||
if (functions && 'part' === fun_type) { | ||
for (const fuc of functions.items) { | ||
await publishOneFunction(app, fuc.id) | ||
} | ||
} | ||
if (policies && 'part' === pol_type) { | ||
for (const pol of policies.items) { | ||
await publishOneFunction(app, pol.id) | ||
} | ||
} | ||
}) | ||
} finally { | ||
await session.endSession() | ||
} | ||
return res.send("pushed") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters