Skip to content

Commit ffe5da4

Browse files
committed
add change request team if it does not exists
1 parent a98c327 commit ffe5da4

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/api/core/ChangeRequestDB.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ChangeRequestDB {
4646
logger : Object
4747
endStatus:any = { Assigned: true, Error: true }
4848
availableStatus:any = { Assigned: true, Error: true, Progress: true }
49+
errorMessage:any = {'api:UnknownDatabase':true,'api:NoUniqueIdForOrganizationName':true}
4950

5051
constructor(req : Request) {
5152
this.request = req
@@ -67,6 +68,7 @@ class ChangeRequestDB {
6768
// maybe I can add a capability at database level
6869
// admin create the db
6970
// and add the capability
71+
7072
async getChangeRequests (changeId: String | false = false, as_list = true) {
7173
const params : DocParamsGet = { type: 'ChangeRequest', as_list: as_list}
7274
if (changeId) {
@@ -77,7 +79,8 @@ class ChangeRequestDB {
7779
return result
7880
} catch (err:any) {
7981
// if there is no change request we return an empty array
80-
if (typeof err.data === 'object' && err.data['api:error'] && err.data['api:error']['@type'] === 'api:UnknownDatabase') {
82+
if (typeof err.data === 'object' && err.data['api:error']
83+
&& this.errorMessage[err.data['api:error']['@type']]) {
8184
return []
8285
}
8386
throw err
@@ -87,8 +90,12 @@ class ChangeRequestDB {
8790
async createTeam(){
8891
try{
8992
await this.accessControl.createOrganization(CROrg)
90-
}catch(err){
91-
// the team already exists
93+
}catch(err:any){
94+
if (typeof err.data === 'object' && err.data['api:error']
95+
&& this.errorMessage[err.data['api:error']['@type']]) {
96+
return // the team already exists
97+
}
98+
throw err
9299
}
93100
}
94101

src/api/paths/api/changes/{org}/{db}/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import ChangeRequestDB from "../../../../../core/ChangeRequestDB";
66

77
export const GET: Operation = async(req:Request, res:Response) =>{
88
try{
9-
var dd = new Date()
10-
console.log("ChangeRequestDB",dd.toISOString())
9+
//var dd = new Date()
10+
//console.log("ChangeRequestDB",dd.toISOString())
1111
const changeR = new ChangeRequestDB(req)
1212
const result = await changeR.getChangeRequests()
13-
var dd1 = new Date()
14-
console.log("ChangeRequestDB",dd1.toISOString())
13+
//var dd1 = new Date()
14+
//console.log("ChangeRequestDB",dd1.toISOString())
1515
res.status(200).json(result);
1616
}catch(err:any){
1717
console.log(err.message)

src/app.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import bodyParser from "body-parser";
1111
import cors from "cors";
1212
import {addContextMiddle} from "./api/addContextMiddle"
1313
import { createProxyMiddleware } from 'http-proxy-middleware'
14-
14+
import TerminusClient , {WOQLClient,WOQL, AccessControl} from "@terminusdb/terminusdb-client"
1515
declare namespace Express {
1616
export interface Request {
1717
context?: object
@@ -66,7 +66,24 @@ app.use(
6666
);
6767

6868
app.use(addContextMiddle)
69-
app.listen(3035);
69+
app.listen(3035,function(){
70+
// when we start the server we check if the terminusCR team already exists
71+
// if it does not exists we'll create it
72+
const endpoint :string = process.env.SERVER_ENDPOINT || "http://127.0.0.1:6363"
73+
const key = process.env.USER_KEY || "root"
74+
const CROrg = process.env.CR_TEAM_NAME || "terminusCR"
75+
const user = process.env.USER_NAME || "admin"
76+
const accessControl = new AccessControl(endpoint, { key: key, user: user })
77+
78+
accessControl.createOrganization(CROrg).then(result=>{
79+
console.log("The Change Request team has been created")
80+
}).catch((err:any)=>{
81+
if (typeof err.data === 'object' && err.data['api:error']
82+
&& err.data['api:error']['@type'] === "api:NoUniqueIdForOrganizationName") {
83+
console.log("The Change Request team already exists")
84+
}
85+
})
86+
});
7087

7188
console.log("App running on port http://localhost:3035");
7289
console.log(

0 commit comments

Comments
 (0)