Skip to content

Commit 4bd1bdd

Browse files
authored
feat: create user endpoint (#64)
1 parent a41c383 commit 4bd1bdd

File tree

2 files changed

+94
-23
lines changed

2 files changed

+94
-23
lines changed

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

+56-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import {
1616
randomCode,
1717
getFirstAndLastNameFromName,
1818
} from '@libs-private/utils/user';
19+
import { LinkSettings } from '@event-inc/types';
20+
import { generateSettingsRecord } from '@libs-private/service-logic/generators/settings/linkSettings';
21+
import { useGenericCRUDService } from '@libs-private/service-logic/services/genericCRUD';
22+
import { Services } from '@libs-private/data-models/types/services';
1923

2024
class AuthGenericError extends MoleculerError {
2125
constructor() {
@@ -949,7 +953,7 @@ module.exports = {
949953

950954
_user = await this.createOrUpdateUser({
951955
ctx,
952-
provider: 'programmatic',
956+
provider: 'pica-api',
953957
emails,
954958
email,
955959
username,
@@ -959,29 +963,62 @@ module.exports = {
959963
profileLink,
960964
organizationId: ctx?.meta?.buildable?._id,
961965
});
966+
const { pointers, _id, buildableId } = _user;
962967

963-
const { _id, email: _email, userKey, firstName: _firstName, lastName: _lastName, state, pointers } = _user;
968+
// Find the settings record from the settings service by the buildableId
969+
const settings = await ctx.broker.call('v1.settings.find', {
970+
query: {
971+
"ownership.buildableId": ctx?.meta?.buildable?._id,
972+
},
973+
});
964974

965-
const buildableId = get(_user, 'client.buildableId');
966-
const containerId = get(_user, 'client.containers[0]._id');
975+
if (!settings || settings.length === 0) {
976+
throw new Error('No settings found for this buildable ID');
977+
}
967978

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+
if (!buildableId) {
980+
throw new Error('buildableId is required to create settings record');
981+
}
982+
983+
if (!_id) {
984+
throw new Error('user id is required to create settings record');
985+
}
986+
987+
const firstSettings: LinkSettings = settings[0];
988+
989+
990+
const newSettings = generateSettingsRecord({
991+
ownership: {
992+
buildableId,
993+
organizationId: ctx?.meta?.buildable?._id,
994+
userId: _id,
995+
},
996+
platforms: firstSettings.connectedPlatforms,
997+
features: firstSettings.features,
998+
buildKitIntegrations: firstSettings.buildKitIntegrations,
999+
})
1000+
1001+
const { create } = useGenericCRUDService(ctx, Services.Settings, newSettings.ownership);
1002+
1003+
// Create new settings record with st prefix
1004+
const newSettingsRecord = await create('st', newSettings);
9791005

1006+
// If the newSettingsRecord is not created, throw an error
1007+
if (!newSettingsRecord.ok) {
1008+
throw new Error('Failed to create new settings record');
1009+
}
1010+
1011+
// Return the pointers
9801012
return {
981-
token,
982-
state,
1013+
_id,
1014+
email,
1015+
secrets: {
1016+
live: `sk_live${pointers?.[1]}`,
1017+
sandbox: `sk_test${pointers?.[0]}`,
1018+
}
9831019
};
9841020

1021+
9851022
}
9861023
catch (error) {
9871024
console.error(error);
@@ -1194,7 +1231,7 @@ module.exports = {
11941231
: `${
11951232
username || email.substring(0, email.indexOf('@'))
11961233
}${randomCode(6)}`,
1197-
[`providers.${provider}`]: user,
1234+
[`providers.${provider}`]: provider === 'pica-api' ? true : user,
11981235
connectedAccounts,
11991236
profile: {
12001237
//profile may be an array of onboarding questions answered
@@ -1275,7 +1312,7 @@ module.exports = {
12751312
username || email.substring(0, email.indexOf('@'))
12761313
}${randomCode(6)}`,
12771314
providers: {
1278-
[provider]: user,
1315+
[provider]: provider === 'pica-api' ? true : user,
12791316
},
12801317
connectedAccounts: [
12811318
// for frontend connected accounts page

libs-private/service-logic/services/settings/useSettingsService.ts

+38-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export const useSettingsService = (ctx: Context, ownership: Ownership) => {
4040
}
4141
);
4242

43+
const { updateMany } = useGenericCRUDService(
44+
ctx,
45+
SERVICE_NAME,
46+
ownership,
47+
{ DISABLE_ADDING_OWNERSHIP_CHECK: true }
48+
);
49+
4350
return {
4451
find,
4552
async get(props: FindRequest = {}): Promise<BResult<Settings, 'service'>> {
@@ -135,13 +142,40 @@ export const useSettingsService = (ctx: Context, ownership: Ownership) => {
135142
}
136143
}
137144

138-
await updateById(settingsRecord._id, {
145+
const updatedValues = {
139146
connectedPlatforms: settingsRecord.connectedPlatforms,
140-
updatedAt: new Date().getTime(),
141-
updatedDate: new Date().toISOString(),
142147
features: features?.length ? features : settingsRecord.features,
143148
buildKitIntegrations: settingsRecord.buildKitIntegrations || [],
144-
});
149+
updatedAt: new Date().getTime(),
150+
updatedDate: new Date().toISOString(),
151+
};
152+
153+
const result = await updateById(settingsRecord._id, updatedValues);
154+
155+
if (!result.ok) {
156+
console.error(`Failed to update settings record ${settingsRecord._id}`);
157+
}
158+
159+
if (result.ok) {
160+
// Update all settings records with the same organizationId but different buildableId in one operation
161+
const updateResult = await updateMany({
162+
query: {
163+
"ownership.organizationId": ownership.organizationId,
164+
"ownership.buildableId": { "$ne": ownership.buildableId }
165+
},
166+
update: {
167+
$set: {
168+
connectedPlatforms: updatedValues.connectedPlatforms,
169+
features: updatedValues.features,
170+
buildKitIntegrations: updatedValues.buildKitIntegrations,
171+
}
172+
}
173+
});
174+
175+
if (!updateResult.ok) {
176+
console.error('Failed to update multiple settings records');
177+
}
178+
}
145179

146180
return resultOk(true);
147181
}

0 commit comments

Comments
 (0)