Skip to content

Commit a41c383

Browse files
authored
feat: create user programmatically using endpoint (#63)
1 parent 9542f87 commit a41c383

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

apps/event-system/services/api/routes/internal.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export const internalRoute = () => ({
3737
'v1.usage.public.create',
3838
'v1.usage.public.get',
3939

40+
'v3.users.sign',
41+
4042
],
4143
aliases: {
4244
'POST v3/users/get': 'v3.users.getUserFromToken',
@@ -78,6 +80,9 @@ export const internalRoute = () => ({
7880
'POST v1/usage/create': 'v1.usage.public.create',
7981
'GET v1/usage/get': 'v1.usage.public.get',
8082

83+
// Users
84+
'POST v3/users/create': 'v3.users.sign',
85+
8186
},
8287
cors: {
8388
origin: '*', //corsOk,

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

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ const RUST_INTERNAL_API_ENDPOINTS = {
9494
const generate_default_event_access_record = (
9595
env: string,
9696
buildableId: string,
97-
userId: string
97+
userId: string,
98+
organizationId?: string
9899
) => {
99100
return {
100101
name: `default-${env}-key`.toUpperCase(),
@@ -112,7 +113,7 @@ const generate_default_event_access_record = (
112113
buildableId,
113114
userId,
114115
projectId: buildableId,
115-
organizationId: buildableId,
116+
organizationId: organizationId || buildableId,
116117
clientId: buildableId,
117118
},
118119
};
@@ -911,6 +912,84 @@ module.exports = {
911912
}
912913
},
913914
},
915+
916+
sign: {
917+
params: {
918+
email: {
919+
type: 'email',
920+
},
921+
},
922+
async handler(ctx: any) {
923+
try {
924+
const { email } = ctx.params;
925+
926+
// Create the username from the email
927+
const username = ctx.params.email.split('@')[0];
928+
929+
// Creat the emails
930+
const emails = [
931+
{
932+
email,
933+
primary: true,
934+
verified: true,
935+
},
936+
];
937+
938+
// Let the firstName and lastName be the same as the email
939+
const firstName = ctx.params.email;
940+
const lastName = ctx.params.email;
941+
942+
// Let the avatar be empty
943+
const avatar = '';
944+
945+
// Let the profileLink be empty
946+
const profileLink = '';
947+
948+
let _user;
949+
950+
_user = await this.createOrUpdateUser({
951+
ctx,
952+
provider: 'programmatic',
953+
emails,
954+
email,
955+
username,
956+
firstName,
957+
lastName,
958+
avatar,
959+
profileLink,
960+
organizationId: ctx?.meta?.buildable?._id,
961+
});
962+
963+
const { _id, email: _email, userKey, firstName: _firstName, lastName: _lastName, state, pointers } = _user;
964+
965+
const buildableId = get(_user, 'client.buildableId');
966+
const containerId = get(_user, 'client.containers[0]._id');
967+
968+
const token = this.createToken({
969+
_id,
970+
email: _email,
971+
username,
972+
userKey,
973+
buildableId,
974+
containerId,
975+
firstName: _firstName,
976+
lastName: _lastName,
977+
pointers,
978+
});
979+
980+
return {
981+
token,
982+
state,
983+
};
984+
985+
}
986+
catch (error) {
987+
console.error(error);
988+
throw new AuthGenericError();
989+
}
990+
}
991+
},
992+
914993
mockOauth: {
915994
params: {
916995
user: {
@@ -1055,6 +1134,7 @@ module.exports = {
10551134
ctx,
10561135
provider,
10571136
user,
1137+
organizationId,
10581138
emails,
10591139
email,
10601140
username,
@@ -1066,6 +1146,7 @@ module.exports = {
10661146
const updateUser = async (_user: any) => {
10671147
const profile = get(_user, 'profile');
10681148
const accessList = get(_user, 'accessList');
1149+
const _organizationId = get(_user, 'organizationId') ? get(_user, 'organizationId') : organizationId;
10691150
const _firstName = get(_user, 'firstName')
10701151
? get(_user, 'firstName')
10711152
: firstName;
@@ -1106,6 +1187,7 @@ module.exports = {
11061187
firstName: _firstName,
11071188
lastName: _lastName,
11081189
accessList,
1190+
organizationId: _organizationId,
11091191
username: _user.username ? _user.username : username,
11101192
userKey: _user.userKey
11111193
? _user.userKey
@@ -1221,6 +1303,7 @@ module.exports = {
12211303
],
12221304
createdAt: Date.now(),
12231305
createdDate: new Date(),
1306+
organizationId,
12241307
});
12251308

12261309
const client = await ctx.broker.call('v1.clients.create', {
@@ -1252,7 +1335,8 @@ module.exports = {
12521335
data: generate_default_event_access_record(
12531336
'test',
12541337
client.buildableId,
1255-
_user._id
1338+
_user._id,
1339+
organizationId,
12561340
),
12571341
});
12581342

@@ -1266,7 +1350,8 @@ module.exports = {
12661350
data: generate_default_event_access_record(
12671351
'live',
12681352
client.buildableId,
1269-
_user._id
1353+
_user._id,
1354+
organizationId
12701355
),
12711356
});
12721357

0 commit comments

Comments
 (0)