-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore-service.ts
170 lines (147 loc) · 4.29 KB
/
store-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { AccessLog, Message, Secret, UserSettings } from '../models';
import { calculateTTL } from '../utils';
const { v4: uuidv4 } = require('uuid');
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
interface ICreateSecret {
workspaceId: string;
authorId: string;
channelId?: string;
title: any;
encrypted: any;
users: any;
expiry: number;
onetime: boolean;
conversation?: any;
}
async function createSecret({
workspaceId,
authorId,
channelId,
title,
encrypted,
users,
expiry,
onetime,
conversation,
}: ICreateSecret) {
try {
const uuid = uuidv4();
const expiryEpoch = calculateTTL(expiry);
const messageItem: Message = {
workspaceId,
uuid,
channelId,
title,
authorId,
users,
expiry: expiryEpoch,
onetime,
conversation,
createdAt: new Date().toISOString(),
};
const secretItem: Secret = {
uuid,
encrypted,
ttl: expiryEpoch,
};
await datastore
.save({
key: datastore.key(['Secret', uuid]),
data: secretItem,
});
await datastore
.save({
key: datastore.key(['Workspace', workspaceId, 'Message', uuid]),
data: messageItem,
});
return { uuid };
} catch (err) {
console.error(err);
return undefined;
}
}
export type SecretRetrievedType = {
secret: Secret | undefined;
message: Message | undefined;
createdAt: Date;
};
async function retrieveSecret(uuid: string, workspaceId: string): Promise<SecretRetrievedType> {
const secretKey = datastore.key(['Secret', uuid])
const [secret] = await datastore.get(secretKey);
const messageKey = datastore.key(['Workspace', workspaceId, 'Message', uuid])
const [message] = await datastore.get(messageKey);
const results: SecretRetrievedType = {
createdAt: new Date(message?.createdAt),
secret: (secret) || undefined,
message: (message) || undefined,
};
return results;
}
interface ICreateAuditTrail {
uuid: string;
userId: string;
valid?: boolean;
}
async function createAuditTrail({ uuid: secretUuid, userId, valid = true }: ICreateAuditTrail) {
const uuid = uuidv4();
const auditItem: AccessLog = {
uuid,
secretUuid,
userId,
valid,
createdAt: new Date().toISOString(),
};
await datastore
.save({
key: datastore.key('Access'),
data: auditItem,
});
return {};
}
async function retrieveAuditTrail(uuid: string) {
const query = await datastore.createQuery('Access')
.filter('secretUuid', '=', uuid).order('createdAt');
const [accessLogs] = await datastore.runQuery(query);
return accessLogs as AccessLog[];
}
async function deleteMessage(uuid: string) {
return await datastore.delete(datastore.key(['Secret', uuid]));
}
async function listAllSecrets(teamId: string, channelId: string) {
const query = datastore.createQuery('Message')
.filter('workspaceId', '=', teamId)
.filter('channelId', '=', channelId).order('createdAt');
const [messages] = await datastore.runQuery(query);
return messages as Message[];
}
async function saveUserSettings({ workspaceId, userId, defaultExpiry, defaultOneTime, defaultTitle }: UserSettings) {
const userSettingItem: UserSettings = {
workspaceId,
userId,
defaultExpiry,
defaultOneTime,
defaultTitle,
};
await datastore
.save({
key: datastore.key(['UserSettings', userId, 'Workspace', workspaceId]),
data: userSettingItem,
});
return {};
}
async function getUserSettings(workspaceId: string, userId: string) {
const settingsKey = datastore.key(['UserSettings', userId, 'Workspace', workspaceId])
const [settings] = await datastore.get(settingsKey);
return settings ? (settings as UserSettings) : undefined;
}
export {
createSecret,
retrieveSecret,
createAuditTrail,
retrieveAuditTrail,
deleteMessage,
listAllSecrets,
saveUserSettings,
getUserSettings,
};