Skip to content

Commit

Permalink
write a script to import proposals from CSV (#3653)
Browse files Browse the repository at this point in the history
* ..

* updates

* updates

* ..

* .

* undo commented code
  • Loading branch information
mattcasey authored Feb 15, 2024
1 parent 96205a0 commit e87328a
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 37 deletions.
42 changes: 20 additions & 22 deletions components/[pageId]/DocumentPage/DocumentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function DocumentPageComponent({

const enableComments = !isSharedPage && !enableSuggestingMode && !isPageTemplate && !!pagePermissions?.comment;

const isStructuredProposal = Boolean(proposal && proposal.formId);
const isStructuredProposal = Boolean(proposal?.formId);
const isUnpublishedProposal = proposal?.status === 'draft' || page.type === 'proposal_template';

// create a key that updates when edit mode changes - default to 'editing' so we dont close sockets immediately
Expand Down Expand Up @@ -382,25 +382,23 @@ function DocumentPageComponent({
{creatingInlineReward && !readOnly && <NewInlineReward pageId={page.id} />}
</CardPropertiesWrapper>
{proposal && proposal.formId ? (
<Box mb={10}>
{page.type === 'proposal_template' ? (
<FormFieldsEditor
readOnly={(!isAdmin && (!user || !proposalAuthors.includes(user.id))) || !!proposal?.archived}
proposalId={proposal.id}
formFields={proposal.form?.formFields ?? []}
/>
) : (
<ProposalFormFieldsInput
pageId={page.id}
enableComments={proposal.permissions.comment}
proposalId={proposal.id}
formFields={proposal.form?.formFields ?? []}
readOnly={!proposal.permissions.edit}
threads={threads}
isDraft={proposal?.status === 'draft'}
/>
)}
</Box>
page.type === 'proposal_template' ? (
<FormFieldsEditor
readOnly={(!isAdmin && (!user || !proposalAuthors.includes(user.id))) || !!proposal?.archived}
proposalId={proposal.id}
formFields={proposal.form?.formFields ?? []}
/>
) : (
<ProposalFormFieldsInput
pageId={page.id}
enableComments={proposal.permissions.comment}
proposalId={proposal.id}
formFields={proposal.form?.formFields ?? []}
readOnly={!proposal.permissions.edit}
threads={threads}
isDraft={proposal?.status === 'draft'}
/>
)
) : (
<CharmEditor
placeholderText={
Expand Down Expand Up @@ -437,7 +435,7 @@ function DocumentPageComponent({
)}

{isStructuredProposal && proposal?.fields?.enableRewards && (
<>
<Box mb={10}>
<Box my={1}>
<Typography variant='h5'>{getFeatureTitle('Rewards')}</Typography>
</Box>
Expand Down Expand Up @@ -478,7 +476,7 @@ function DocumentPageComponent({
});
}}
/>
</>
</Box>
)}

{(page.type === 'proposal' || page.type === 'card' || page.type === 'card_synced') && (
Expand Down
4 changes: 2 additions & 2 deletions components/proposals/ProposalPage/NewProposalPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ export function NewProposalPage({
/>
)}
{isStructured && formInputs.fields?.enableRewards && (
<>
<Box mb={10}>
<Box my={1}>
<Typography variant='h5'>{getFeatureTitle('Rewards')}</Typography>
</Box>
Expand Down Expand Up @@ -534,7 +534,7 @@ export function NewProposalPage({
});
}}
/>
</>
</Box>
)}
</Box>
</StyledContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function ProposalFormFieldsInput({
const proposalFormFieldAnswer = proposalFormFieldAnswers.find(
(_proposalFormFieldAnswer) => _proposalFormFieldAnswer.fieldId === formField.id
);
if (!proposalFormFieldAnswer) {
if (!proposalFormFieldAnswer && formField.type !== 'label') {
return null;
}
return {
Expand Down
1 change: 0 additions & 1 deletion lib/form/upsertProposalFormAnswers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export async function upsertProposalFormAnswers({ answers, formId, proposalId }:
const field = form.formFields.find((f) => f.id === a.fieldId);
const existingAnswer = existingAnswers.find((e) => e.fieldId === a.fieldId);
const answerId = existingAnswer?.id || v4();

return prisma.formFieldAnswer.upsert({
where: { id: answerId },
create: {
Expand Down
1 change: 0 additions & 1 deletion lib/proposal/form/getProposalFormAnswers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export async function getProposalFormAnswers({
});

const allAnswers = proposal.formAnswers;

if (!allAnswers) {
return [];
}
Expand Down
18 changes: 8 additions & 10 deletions pages/api/proposals/[id]/form/answers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InvalidInputError } from '@charmverse/core/errors';
import { isProposalAuthor } from '@charmverse/core/permissions';
import type { FormFieldAnswer } from '@charmverse/core/prisma-client';
import { prisma } from '@charmverse/core/prisma-client';
import type { NextApiRequest, NextApiResponse } from 'next';
Expand Down Expand Up @@ -58,22 +57,21 @@ async function upsertProposalFormAnswersHandler(
const proposalId = req.query.id as string;
const userId = req.session.user.id;

const proposal = await prisma.proposal.findUnique({ where: { id: proposalId }, include: { authors: true } });
const permissions = await permissionsApiClient.proposals.computeProposalPermissions({
resourceId: proposalId,
userId
});

if (!proposal) {
throw new InvalidInputError(`Proposal with id ${proposalId} does not exist`);
if (!permissions.edit) {
throw new ActionNotPermittedError(`You can't update this proposal.`);
}

const proposal = await prisma.proposal.findUniqueOrThrow({ where: { id: proposalId }, select: { formId: true } });

if (!proposal.formId) {
throw new InvalidInputError(`Proposal ${proposalId} does not have a form`);
}

const isAuthor = isProposalAuthor({ userId, proposal });

if (!isAuthor) {
throw new ActionNotPermittedError('Only authors can edit proposal form answers');
}

const { answers } = req.body as { answers: FieldAnswerInput[] };

await upsertProposalFormAnswers({ answers, formId: proposal.formId, proposalId });
Expand Down
Loading

0 comments on commit e87328a

Please sign in to comment.