@@ -16,6 +16,10 @@ import {
16
16
randomCode ,
17
17
getFirstAndLastNameFromName ,
18
18
} 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' ;
19
23
20
24
class AuthGenericError extends MoleculerError {
21
25
constructor ( ) {
@@ -949,7 +953,7 @@ module.exports = {
949
953
950
954
_user = await this . createOrUpdateUser ( {
951
955
ctx,
952
- provider : 'programmatic ' ,
956
+ provider : 'pica-api ' ,
953
957
emails,
954
958
email,
955
959
username,
@@ -959,29 +963,62 @@ module.exports = {
959
963
profileLink,
960
964
organizationId : ctx ?. meta ?. buildable ?. _id ,
961
965
} ) ;
966
+ const { pointers, _id, buildableId } = _user ;
962
967
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
+ } ) ;
964
974
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
+ }
967
978
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 ) ;
979
1005
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
980
1012
return {
981
- token,
982
- state,
1013
+ _id,
1014
+ email,
1015
+ secrets : {
1016
+ live : `sk_live${ pointers ?. [ 1 ] } ` ,
1017
+ sandbox : `sk_test${ pointers ?. [ 0 ] } ` ,
1018
+ }
983
1019
} ;
984
1020
1021
+
985
1022
}
986
1023
catch ( error ) {
987
1024
console . error ( error ) ;
@@ -1194,7 +1231,7 @@ module.exports = {
1194
1231
: `${
1195
1232
username || email . substring ( 0 , email . indexOf ( '@' ) )
1196
1233
} ${ randomCode ( 6 ) } `,
1197
- [ `providers.${ provider } ` ] : user ,
1234
+ [ `providers.${ provider } ` ] : provider === 'pica-api' ? true : user ,
1198
1235
connectedAccounts,
1199
1236
profile : {
1200
1237
//profile may be an array of onboarding questions answered
@@ -1275,7 +1312,7 @@ module.exports = {
1275
1312
username || email . substring ( 0 , email . indexOf ( '@' ) )
1276
1313
} ${ randomCode ( 6 ) } `,
1277
1314
providers : {
1278
- [ provider ] : user ,
1315
+ [ provider ] : provider === 'pica-api' ? true : user ,
1279
1316
} ,
1280
1317
connectedAccounts : [
1281
1318
// for frontend connected accounts page
0 commit comments