-
-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sys): impl delete app api & page
- Loading branch information
Showing
8 changed files
with
137 additions
and
13 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
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,45 @@ | ||
import * as assert from "assert" | ||
import { Constants } from "../constants" | ||
import { DatabaseAgent } from "../lib/db-agent" | ||
|
||
/** | ||
* Recycle collector | ||
*/ | ||
export class RecycleCollector { | ||
collection: string | ||
|
||
constructor(collection: string) { | ||
assert.ok(collection, 'empty collection got') | ||
this.collection = collection | ||
} | ||
|
||
async insert(data: any) { | ||
const db = DatabaseAgent.sys_db | ||
const r = await db.collection(Constants.cn.recycles) | ||
.add({ | ||
collection: this.collection, | ||
data, | ||
created_at: Date.now() | ||
}) | ||
|
||
return r.id | ||
} | ||
|
||
async retrieve(_id: string) { | ||
const db = DatabaseAgent.sys_db | ||
const r = await db.collection(Constants.cn.recycles) | ||
.where({ _id: _id, collection: this.collection }) | ||
.getOne() | ||
|
||
return r.data | ||
} | ||
|
||
async remove(_id: string) { | ||
const db = DatabaseAgent.sys_db | ||
const r = await db.collection(Constants.cn.recycles) | ||
.where({ _id: _id, collection: this.collection }) | ||
.remove() | ||
|
||
return r.deleted | ||
} | ||
} |
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,53 @@ | ||
/* | ||
* @Author: Maslow<wangfugen@126.com> | ||
* @Date: 2021-08-30 15:22:34 | ||
* @LastEditTime: 2021-09-09 11:00:29 | ||
* @Description: | ||
*/ | ||
|
||
import * as assert from 'assert' | ||
import { Request, Response } from 'express' | ||
import { RecycleCollector } from '../../api/recycle' | ||
import { getApplicationByAppid } from '../../api/application' | ||
import { checkPermission } from '../../api/permission' | ||
import { Constants } from '../../constants' | ||
import { DatabaseAgent } from '../../lib/db-agent' | ||
|
||
const { APPLICATION_REMOVE } = Constants.permissions | ||
|
||
/** | ||
* The handler of removing application | ||
* @param req | ||
* @param res | ||
* @returns | ||
*/ | ||
export async function handleRemoveApplication(req: Request, res: Response) { | ||
const uid = req['auth']?.uid | ||
const appid = req.params.appid | ||
const app = await getApplicationByAppid(appid) | ||
if (!app) | ||
return res.status(422).send('invalid appid') | ||
|
||
if (app.status !== 'cleared' && app.status !== 'created') { | ||
return res.status(422).send('app status must be cleared or created') | ||
} | ||
|
||
// check permission | ||
const code = await checkPermission(uid, APPLICATION_REMOVE.name, app) | ||
if (code) { | ||
return res.status(code).send() | ||
} | ||
|
||
// save app to recycle collection | ||
const recycle = new RecycleCollector(Constants.cn.applications) | ||
const saved = await recycle.insert(app) | ||
assert.ok(saved, 'recycle insert got empty return value') | ||
|
||
// remove app | ||
const db = DatabaseAgent.sys_db | ||
const ret = await db.collection(Constants.cn.applications) | ||
.where({ appid }) | ||
.remove() | ||
|
||
return res.send({ data: ret }) | ||
} |
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