Skip to content

Commit 35a69f2

Browse files
committed
Cli update for users create and update
1 parent 10ab862 commit 35a69f2

File tree

1 file changed

+86
-3
lines changed

1 file changed

+86
-3
lines changed

apps/event-system/services/users/users.service.ts

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,13 @@ module.exports = {
304304
type: 'string',
305305
convert: true,
306306
},
307+
isTerminal: {
308+
type: 'boolean',
309+
optional: true,
310+
},
307311
},
308312
async handler(ctx: any) {
309-
const { type, code } = ctx.params;
313+
const { type, code, isTerminal = false } = ctx.params;
310314

311315
const map = {
312316
github: async (code: any) => {
@@ -591,7 +595,7 @@ module.exports = {
591595
lastName,
592596
avatar,
593597
profileLink,
594-
} = await fn(code);
598+
} = await fn(code, isTerminal);
595599

596600
if (!email) {
597601
console.debug(
@@ -643,7 +647,7 @@ module.exports = {
643647
const buildableId = get(_user, 'client.buildableId');
644648
const containerId = get(_user, 'client.containers[0]._id');
645649

646-
const token = this.createToken({
650+
const token = await this.createToken({
647651
_id,
648652
email,
649653
username,
@@ -655,6 +659,11 @@ module.exports = {
655659
pointers,
656660
});
657661

662+
if (isTerminal) {
663+
const { testKey, liveKey } = await this.createOrGetEventAccessKeys(token, pointers);
664+
return { testKey, liveKey };
665+
}
666+
658667
return {
659668
token,
660669
state,
@@ -1147,5 +1156,79 @@ module.exports = {
11471156

11481157
return token;
11491158
},
1159+
async createOrGetEventAccessKeys(token: string, pointers: string[]) {
1160+
1161+
// Let's list the event access records for the user
1162+
const listEventAccessRecords = await axios({
1163+
method: 'get',
1164+
url: RUST_INTERNAL_API_ENDPOINTS.GET_EVENT_ACCESS_RECORD,
1165+
headers: {
1166+
'Content-Type': 'application/json',
1167+
Authorization: `Bearer ${token}`,
1168+
'x-pica-show-all-environments': true,
1169+
'x-pica-secret': `sk_test${pointers[0]}`,
1170+
},
1171+
});
1172+
1173+
const filteredEventAccessRecords = listEventAccessRecords?.data?.rows?.filter(
1174+
(record: any) => !['DEFAULT-LIVE-KEY', 'DEFAULT-TEST-KEY'].includes(record.name)
1175+
);
1176+
1177+
// Get existing keys or create new ones
1178+
const testKey = await this.getOrCreateAccessKey({
1179+
existingRecords: filteredEventAccessRecords,
1180+
environment: 'test',
1181+
token,
1182+
pointer: pointers[0],
1183+
});
1184+
1185+
1186+
const liveKey = await this.getOrCreateAccessKey({
1187+
existingRecords: filteredEventAccessRecords,
1188+
environment: 'live',
1189+
token,
1190+
pointer: pointers[1],
1191+
});
1192+
1193+
1194+
return { testKey, liveKey };
1195+
},
1196+
1197+
async getOrCreateAccessKey({ existingRecords, environment, token, pointer }: {
1198+
existingRecords: any[],
1199+
environment: 'test' | 'live',
1200+
token: string,
1201+
pointer: string
1202+
}) {
1203+
let key = existingRecords?.find(record => record.environment === environment)?.accessKey;
1204+
1205+
if (!key) {
1206+
const response = await axios({
1207+
method: 'post',
1208+
url: RUST_INTERNAL_API_ENDPOINTS.GET_EVENT_ACCESS_RECORD,
1209+
headers: {
1210+
'Content-Type': 'application/json',
1211+
Authorization: `Bearer ${token}`,
1212+
'x-pica-secret': `sk_${environment}${pointer}`,
1213+
},
1214+
data: {
1215+
name: `${environment}-key`,
1216+
group: `${environment}-key`,
1217+
connectionType: "custom",
1218+
platform: "pica",
1219+
paths: {
1220+
id: null,
1221+
event: null,
1222+
payload: null,
1223+
secret: null,
1224+
signature: null,
1225+
},
1226+
}
1227+
});
1228+
key = response?.data?.accessKey;
1229+
}
1230+
1231+
return key;
1232+
}
11501233
},
11511234
};

0 commit comments

Comments
 (0)