Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(messaging-api): fixed sending message when already imported #963

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/messaging-api/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ POSTGRES_PORT=5432
POSTGRES_DB_NAME=messaging
POSTGRES_DB_NAME_SHARED=shared
SCHEDULER_BACKEND_URL=http://localhost:8005
WEBHOOK_URL_BASE=http://host.docker.internal:8002
WEBHOOK_URL_BASE=http://localhost:8002
SYNCHRONOUS_USER_IMPORT=true
PROFILE_BACKEND_URL=http://localhost:8003
LOGTO_JWK_ENDPOINT=http://localhost:3301/oidc/jwks
Expand Down
30 changes: 30 additions & 0 deletions apps/messaging-api/openapi-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2580,6 +2580,11 @@ paths:
true, it means that the organisation already
asked the permissions to the user
type: boolean
welcomed:
default: false
description: If true, it means that a message to welcome the user has already
been sent
type: boolean
required:
- publicIdentityId
- firstName
Expand Down Expand Up @@ -2945,6 +2950,11 @@ paths:
true, it means that the organisation already asked
the permissions to the user
type: boolean
welcomed:
default: false
description: If true, it means that a message to welcome the user has already
been sent
type: boolean
required:
- publicIdentityId
- firstName
Expand Down Expand Up @@ -3338,6 +3348,11 @@ paths:
true, it means that the organisation already asked
the permissions to the user
type: boolean
welcomed:
default: false
description: If true, it means that a message to welcome the user has already
been sent
type: boolean
required:
- publicIdentityId
- firstName
Expand Down Expand Up @@ -4031,6 +4046,11 @@ paths:
true, it means that the organisation already
asked the permissions to the user
type: boolean
welcomed:
default: false
description: If true, it means that a message to welcome the user has already
been sent
type: boolean
required:
- importIndex
- phoneNumber
Expand Down Expand Up @@ -4465,6 +4485,11 @@ paths:
true, it means that the organisation already
asked the permissions to the user
type: boolean
welcomed:
default: false
description: If true, it means that a message to welcome the user has already
been sent
type: boolean
required:
- publicIdentityId
- firstName
Expand Down Expand Up @@ -4832,6 +4857,11 @@ paths:
true, it means that the organisation already asked
the permissions to the user
type: boolean
welcomed:
default: false
description: If true, it means that a message to welcome the user has already
been sent
type: boolean
required:
- publicIdentityId
- firstName
Expand Down
26 changes: 26 additions & 0 deletions apps/messaging-api/src/services/users/import/map-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ const processUser = async (params: {
});

if (userFromDb) {
userToStore.details = getDetailsToUpdate(userToStore, userFromDb);
return updateUser({
toUpdate: userToStore,
id: userFromDb.id,
Expand All @@ -199,6 +200,7 @@ const processUser = async (params: {
});

if (userFromDb) {
userToStore.details = getDetailsToUpdate(userToStore, userFromDb);
return updateUser({
toUpdate: userToStore,
id: userFromDb.id,
Expand All @@ -210,6 +212,29 @@ const processUser = async (params: {
return insertNewUser({ toInsert: userToStore, client });
};

const getDetailsToUpdate = (
toStore: Omit<User, "id">,
fromDb: User,
): UserDetails | undefined => {
if (!fromDb.details?.welcomed) {
return toStore.details;
}

if (!toStore.details) {
return {
welcomed: true,
publicIdentityId: null,
firstName: null,
lastName: null,
birthDate: null,
address: null,
collectedConsent: false,
};
}

return { ...toStore.details, welcomed: true };
};

const processOrganizationUserRelation = async (params: {
userId: string;
client: PoolClient;
Expand Down Expand Up @@ -501,6 +526,7 @@ const getUserIfMapped = async (params: {
userProfileId,
client,
errorCode: IMPORT_USERS_ERROR,
withDetails: true,
});
} catch (error) {
if (isLifeEventsError(error) && error.errorCode === 404) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ const prepareInvitations = (params: {
continue;
}

if (toInvite.organisationInvitationStatus === "accepted") {
const alreadyWelcomed = toInvite.details?.welcomed ?? false;
if (toInvite.userStatus === "active" && !alreadyWelcomed) {
// send invitation to say the have been onboarded
if (!toSend.welcome[language]) {
toSend.welcome[language] = {
Expand Down Expand Up @@ -473,20 +474,31 @@ const setImportedAsInvited = async (params: {
);
}
if (welcomed.length) {
let userIndex = 3;
let userIndex = 1;
const idsIndexes = welcomed.map(() => `$${userIndex++}`);
const sentAtIndex = `$${userIndex++}`;
const organisationIdIndex = `$${userIndex}`;
await params.client.query(
`
UPDATE organisation_user_configurations
SET invitation_sent_at = $1
WHERE organisation_id = $2 and user_id in (${idsIndexes.join(", ")});
SET invitation_sent_at = ${sentAtIndex}
WHERE organisation_id = ${organisationIdIndex} and user_id in (${idsIndexes.join(", ")});
`,
[
...welcomed,
new Date(new Date().toUTCString()).toISOString(),
params.toImportUsers.organisationId,
...welcomed,
],
);

await params.client.query(
`
UPDATE users
SET details['welcomed'] = 'true'
WHERE id in (${idsIndexes.join(", ")});
`,
welcomed,
);
}

await params.client.query("COMMIT");
Expand Down
6 changes: 6 additions & 0 deletions apps/messaging-api/src/services/users/shared-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const getUser = async (params: {
whereValues: string[];
errorCode: string;
logicalWhereOperator?: string;
withDetails?: boolean;
}): Promise<User> => {
let result: QueryResult<User>;
try {
const detailsQuery = params.withDetails ? 'details as "details",' : "";
const operator = params.logicalWhereOperator
? ` ${params.logicalWhereOperator} `
: " AND ";
Expand All @@ -28,6 +30,7 @@ const getUser = async (params: {
user_profile_id as "userProfileId",
importer_organisation_id as "importerOrganisationId",
user_status as "userStatus",
${detailsQuery}
correlation_quality as "correlationQuality"
FROM users where ${params.whereClauses.join(operator)} LIMIT 1
`,
Expand Down Expand Up @@ -64,12 +67,14 @@ export const getUserByUserProfileId = async (params: {
userProfileId: string;
client: PoolClient;
errorCode: string;
withDetails?: boolean;
}): Promise<User> =>
getUser({
client: params.client,
whereClauses: ["user_profile_id = $1"],
whereValues: [params.userProfileId],
errorCode: params.errorCode,
withDetails: params.withDetails,
});

export const getUserByContacts = async (params: {
Expand Down Expand Up @@ -98,6 +103,7 @@ export const getUserByContacts = async (params: {
whereValues: values,
errorCode: params.errorCode,
logicalWhereOperator: "OR",
withDetails: true,
});
};

Expand Down
7 changes: 7 additions & 0 deletions apps/messaging-api/src/types/usersSchemaDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export const UserDetailsSchema = Type.Object({
description:
"If false, an invitation to the user asking to accept to receive messages from the organisation will be sent. If true, it means that the organisation already asked the permissions to the user",
}),
welcomed: Type.Optional(
Type.Boolean({
default: false,
description:
"If true, it means that a message to welcome the user has already been sent",
}),
),
});
export type UserDetails = Static<typeof UserDetailsSchema>;

Expand Down
3 changes: 0 additions & 3 deletions packages/building-blocks-sdk/src/services/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ export class Messaging {
},
);

console.log(data?.data);

if (data?.data.type === "email") {
return { data: data.data };
}
Expand Down Expand Up @@ -524,7 +522,6 @@ export class Messaging {
{ params: { path: { eventId } } },
);

console.log(data?.data);
return { data: data?.data, error };
}

Expand Down