Skip to content

Commit

Permalink
feat: check for swu org proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
IanFonzie committed Dec 1, 2023
1 parent 836ee22 commit e92d6cb
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 22 deletions.
48 changes: 33 additions & 15 deletions src/back-end/lib/db/proposal/sprint-with-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,22 +624,40 @@ export const readOwnSWUProposals = tryDb<
);
});

export const readOneSWUProposalByOpportunityAndAuthor = tryDb<
[Id, Session],
Id | null
>(async (connection, opportunityId, session) => {
if (!session) {
return valid(null);
}
const result = (
await connection<{ id: Id }>("swuProposals")
.where({ opportunity: opportunityId, createdBy: session.user.id })
.select("id")
.first()
)?.id;
export const readOneSWUProposalWithIdsQuery = (
query: (
connection: Connection,
...ids: Id[]
) => Promise<Pick<RawSWUProposal, "id"> | undefined>
) =>
tryDb<[Session, ...Id[]], Id | null>(async (connection, session, ...ids) => {
if (!session) {
return valid(null);
}

return valid(result ? result : null);
});
const result = (await query(connection, session.user.id, ...ids))?.id;

return valid(result ? result : null);
});

export const readOneSWUProposalByOpportunityAndAuthor =
readOneSWUProposalWithIdsQuery(
async (connection, userId, opportunityId) =>
await connection<RawSWUProposal, { id: Id }>("swuProposals")
.where({ opportunity: opportunityId, createdBy: userId })
.select("id")
.first()
);

export const readOneSWUProposalByOpportunityAndOrgAuthor =
readOneSWUProposalWithIdsQuery(
async (connection, userId, opportunityId, organizationId) =>
await connection<RawSWUProposal>("swuProposals")
.where({ opportunity: opportunityId, organization: organizationId })
.andWhereNot({ createdBy: userId })
.select("id")
.first()
);

async function isSWUProposalAuthor(
connection: Connection,
Expand Down
65 changes: 58 additions & 7 deletions src/back-end/lib/resources/proposal/sprint-with-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,42 @@ const create: crud.Create<
}

// Check for existing proposal on this opportunity, authored by this user
const dbResult = await db.readOneSWUProposalByOpportunityAndAuthor(
connection,
opportunity,
request.session
);
if (isInvalid(dbResult)) {
if (organization) {
const dbResultOrgProposal =
await db.readOneSWUProposalByOpportunityAndOrgAuthor(
connection,
request.session,
opportunity,
organization
);
if (isInvalid(dbResultOrgProposal)) {
return invalid({
database: [db.ERROR_MESSAGE]
});
}
if (dbResultOrgProposal.value) {
return invalid({
existingOrganizationProposal: {
proposalId: dbResultOrgProposal.value,
errors: ["Please select a different organization."]
}
});
}
}

// Check for existing proposal on this opportunity, authored by this user
const dbResultProposal =
await db.readOneSWUProposalByOpportunityAndAuthor(
connection,
request.session,
opportunity
);
if (isInvalid(dbResultProposal)) {
return invalid({
database: [db.ERROR_MESSAGE]
});
}
if (dbResult.value) {
if (dbResultProposal.value) {
return invalid({
conflict: ["You already have a proposal for this opportunity."]
});
Expand Down Expand Up @@ -635,6 +660,32 @@ const update: crud.Update<
});
}

// Check for existing proposal on this opportunity, authored by this user
if (validatedOrganization.value) {
const dbResult =
await db.readOneSWUProposalByOpportunityAndOrgAuthor(
connection,
request.session,
swuOpportunity.id,
validatedOrganization.value.id
);
if (isInvalid(dbResult)) {
return invalid({
database: [db.ERROR_MESSAGE]
});
}
if (dbResult.value && dbResult.value !== request.params.id) {
return invalid({
proposal: adt("edit" as const, {
existingOrganizationProposal: {
proposalId: dbResult.value,
errors: ["Please select a different organization."]
}
})
});
}
}

// Attachments must be validated for both drafts and published opportunities
const validatedAttachments = await validateAttachments(
connection,
Expand Down

0 comments on commit e92d6cb

Please sign in to comment.