Skip to content

Commit b1abc41

Browse files
committed
refactor(ts): change db module to use TS and ESM
1 parent c3ffd78 commit b1abc41

File tree

4 files changed

+130
-78
lines changed

4 files changed

+130
-78
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
const toClass = function (obj, proto) {
1+
export const toClass = function (obj: any, proto: any) {
22
obj = JSON.parse(JSON.stringify(obj));
33
obj.__proto__ = proto;
44
return obj;
55
};
6-
7-
module.exports.toClass = toClass;

src/db/index.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/db/index.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const bcrypt = require('bcryptjs');
2+
const config = require('../config');
3+
let sink: any;
4+
if (config.getDatabase().type === 'mongo') {
5+
sink = require('./mongo');
6+
} else if (config.getDatabase().type === 'fs') {
7+
sink = require('./file');
8+
}
9+
10+
export const createUser = async (
11+
username: string,
12+
password: string,
13+
email: string,
14+
gitAccount: string,
15+
admin: boolean = false
16+
) => {
17+
console.log(
18+
`creating user
19+
user=${username},
20+
gitAccount=${gitAccount}
21+
email=${email},
22+
admin=${admin}`,
23+
);
24+
25+
const data = {
26+
username: username,
27+
password: await bcrypt.hash(password, 10),
28+
gitAccount: gitAccount,
29+
email: email,
30+
admin: admin,
31+
};
32+
33+
if (username === undefined || username === null || username === '') {
34+
const errorMessage = `username ${username} cannot be empty`;
35+
throw new Error(errorMessage);
36+
}
37+
38+
if (gitAccount === undefined || gitAccount === null || gitAccount === '') {
39+
const errorMessage = `GitAccount ${gitAccount} cannot be empty`;
40+
throw new Error(errorMessage);
41+
}
42+
43+
if (email === undefined || email === null || email === '') {
44+
const errorMessage = `Email ${email} cannot be empty`;
45+
throw new Error(errorMessage);
46+
}
47+
const existingUser = await sink.findUser(username);
48+
49+
if (existingUser) {
50+
const errorMessage = `user ${username} already exists`;
51+
throw new Error(errorMessage);
52+
}
53+
54+
await sink.createUser(data);
55+
};
56+
57+
export const {
58+
authorise,
59+
reject,
60+
cancel,
61+
getPushes,
62+
writeAudit,
63+
getPush,
64+
findUser,
65+
getUsers,
66+
deleteUser,
67+
updateUser,
68+
getRepos,
69+
getRepo,
70+
createRepo,
71+
addUserCanPush,
72+
addUserCanAuthorise,
73+
removeUserCanAuthorise,
74+
removeUserCanPush,
75+
deleteRepo,
76+
isUserPushAllowed,
77+
canUserApproveRejectPushRepo,
78+
canUserApproveRejectPush,
79+
canUserCancelPush,
80+
getSessionStore,
81+
} = sink;

src/db/types.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export type PushQuery = {
2+
error: boolean;
3+
blocked: boolean,
4+
allowPush: boolean,
5+
authorised: boolean
6+
};
7+
8+
export type UserRole = 'canPush' | 'canAuthorise';
9+
10+
export type Repo = {
11+
project: string;
12+
name: string;
13+
url: string;
14+
users: Record<UserRole, string[]>;
15+
_id: string;
16+
};
17+
18+
export type User = {
19+
_id: string;
20+
username: string;
21+
password: string | null; // null if oidcId is set
22+
gitAccount: string;
23+
email: string;
24+
admin: boolean;
25+
oidcId: string | null;
26+
};
27+
28+
export type Push = {
29+
id: string;
30+
allowPush: boolean;
31+
authorised: boolean;
32+
blocked: boolean;
33+
blockedMessage: string;
34+
branch: string;
35+
canceled: boolean;
36+
commitData: object;
37+
commitFrom: string;
38+
commitTo: string;
39+
error: boolean;
40+
method: string;
41+
project: string;
42+
rejected: boolean;
43+
repo: string;
44+
repoName: string;
45+
timepstamp: string;
46+
type: string;
47+
url: string;
48+
};

0 commit comments

Comments
 (0)