Skip to content

Commit fb737f4

Browse files
authored
chore: Create or get users (#67)
1 parent 54ddd68 commit fb737f4

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

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

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

40-
'v3.users.sign',
40+
'v3.users.createOrGet',
4141

4242
],
4343
aliases: {
@@ -81,7 +81,7 @@ export const internalRoute = () => ({
8181
'GET v1/usage/get': 'v1.usage.public.get',
8282

8383
// Users
84-
'POST v3/users/create': 'v3.users.sign',
84+
'POST v3/users/create-or-get': 'v3.users.createOrGet',
8585

8686
},
8787
cors: {

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

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ module.exports = {
917917
},
918918
},
919919

920-
sign: {
920+
createOrGet: {
921921
params: {
922922
email: {
923923
type: 'email',
@@ -926,14 +926,16 @@ module.exports = {
926926
async handler(ctx: any) {
927927
try {
928928
const { email } = ctx.params;
929+
const _buildableId = ctx?.meta?.buildable?._id;
930+
const emailWithOrganizationId = `${_buildableId}-${email}`;
929931

930932
// Create the username from the email
931-
const username = ctx.params.email.split('@')[0];
933+
const username = emailWithOrganizationId.split('@')[0];
932934

933935
// Creat the emails
934936
const emails = [
935937
{
936-
email,
938+
email: emailWithOrganizationId,
937939
primary: true,
938940
verified: true,
939941
},
@@ -951,17 +953,51 @@ module.exports = {
951953

952954
let _user;
953955

956+
const user = (await ctx.broker.call('v3.users.find', {
957+
query: { buildableId: _buildableId },
958+
pageSize: 1,
959+
}))[0];
960+
961+
if (user?.providers?.["pica-api"]) {
962+
throw new MoleculerError(
963+
'This user does not have access to create a new user',
964+
400,
965+
'programmatic-user-error',
966+
{}
967+
);
968+
}
969+
970+
const existingUser = (await ctx.broker.call('v3.users.find', {
971+
query: { email: emailWithOrganizationId },
972+
pageSize: 1,
973+
}))[0];
974+
975+
if (existingUser) {
976+
977+
const { pointers, _id } = existingUser;
978+
979+
return {
980+
_id,
981+
email,
982+
secrets: {
983+
live: `sk_live${pointers?.[1]}`,
984+
sandbox: `sk_test${pointers?.[0]}`,
985+
}
986+
}
987+
988+
}
989+
954990
_user = await this.createOrUpdateUser({
955991
ctx,
956992
provider: 'pica-api',
957993
emails,
958-
email,
994+
email: emailWithOrganizationId,
959995
username,
960996
firstName,
961997
lastName,
962998
avatar,
963999
profileLink,
964-
organizationId: ctx?.meta?.buildable?._id,
1000+
organizationId: _buildableId,
9651001
});
9661002
const { pointers, _id, buildableId } = _user;
9671003

@@ -973,15 +1009,27 @@ module.exports = {
9731009
});
9741010

9751011
if (!settings || settings.length === 0) {
976-
throw new Error('No settings found for this buildable ID');
1012+
throw new MoleculerError(
1013+
'No settings configuration found for the organization',
1014+
500,
1015+
'settings-not-found'
1016+
);
9771017
}
9781018

9791019
if (!buildableId) {
980-
throw new Error('buildableId is required to create settings record');
1020+
throw new MoleculerError(
1021+
'buildableId is required to create settings record',
1022+
500,
1023+
'missing-buildable-id'
1024+
);
9811025
}
9821026

9831027
if (!_id) {
984-
throw new Error('user id is required to create settings record');
1028+
throw new MoleculerError(
1029+
'_id is required to create settings record',
1030+
500,
1031+
'missing-user-id'
1032+
);
9851033
}
9861034

9871035
const firstSettings: LinkSettings = settings[0];
@@ -1022,6 +1070,9 @@ module.exports = {
10221070
}
10231071
catch (error) {
10241072
console.error(error);
1073+
if (error instanceof MoleculerError) {
1074+
throw error;
1075+
}
10251076
throw new AuthGenericError();
10261077
}
10271078
}

0 commit comments

Comments
 (0)