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

IMN 538 - Fix user info fetch during contract creation #543

Merged
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 packages/agreement-process/src/model/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export function agreementMissingUserInfo(userId: string): ApiError<ErrorCodes> {
}

export function agreementSelfcareIdNotFound(
tenantId: string
tenantId: TenantId
): ApiError<ErrorCodes> {
return new ApiError({
detail: `Selfcare id not found for tenant ${tenantId}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
generateId,
genericError,
tenantAttributeType,
unsafeBrandId,
} from "pagopa-interop-models";
import {
UserResponse,
Expand All @@ -34,6 +35,7 @@ import {
import { match } from "ts-pattern";
import {
agreementMissingUserInfo,
agreementSelfcareIdNotFound,
agreementStampNotFound,
userNotFound,
} from "../model/domain/errors.js";
Expand Down Expand Up @@ -127,16 +129,29 @@ const getAttributeInvolved = async (
};

const getSubmissionInfo = async (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MalpenZibo Does this apply only to this getSubmissionInfo case or also to the getActivationInfo below?
If it applies also to that case, maybe you could move this logic from here into the retrieveUser, which is called by both functions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be applied just to submission info.
The submission can be performed only by the consumer, and we can safely take the details from it.

The activation can be done both by consumer and by producer, but in both cases the getActivationInfo is called by the user who also activated the agreement. This is why we can take the info starting from the context (of the jwt)

selfcareId: SelfcareId,
consumer: Tenant,
seed: UpdateAgreementSeed
): Promise<[string, Date]> => {
const submission = seed.stamps.submission;
if (!submission) {
throw agreementStampNotFound("submission");
}
const user: UserResponse = await retrieveUser(selfcareId, submission.who);
if (user.name && user.surname && user.taxCode) {
return [`${user.name} ${user.surname} (${user.taxCode})`, submission.when];

if (!consumer.selfcareId) {
throw agreementSelfcareIdNotFound(consumer.id);
}

const consumerSelfcareId: SelfcareId = unsafeBrandId(consumer.selfcareId);

const consumerUser: UserResponse = await retrieveUser(
consumerSelfcareId,
unsafeBrandId(submission.who)
);
if (consumerUser.name && consumerUser.surname && consumerUser.taxCode) {
return [
`${consumerUser.name} ${consumerUser.surname} (${consumerUser.taxCode})`,
submission.when,
];
}

throw agreementMissingUserInfo(submission.who);
Expand Down Expand Up @@ -247,7 +262,7 @@ const getPdfPayload = async (
consumer.externalId.value
);
const [submitter, submissionTimestamp] = await getSubmissionInfo(
selfcareId,
consumer,
seed
);
const [activator, activationTimestamp] = await getActivationInfo(
Expand Down
3 changes: 3 additions & 0 deletions packages/selfcare-v2-client/src/model/selfcareModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ import { schemas } from "./generated/api.js";

const UserResponse = schemas.UserResponse.strip();
export type UserResponse = z.infer<typeof UserResponse>;

const InstitutionResponse = schemas.InstitutionResponse.strip();
export type InstitutionResponse = z.infer<typeof InstitutionResponse>;
Loading