Skip to content
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
35 changes: 24 additions & 11 deletions @app/client/src/pages/update-registration/[updateToken]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@app/components"
import { useUpdateEventRegistrationPageQuery } from "@app/graphql"
import { filterObjectByKeys } from "@app/lib"
import { Col, Row } from "antd"
import { Alert, Col, Row } from "antd"
import { useRouter } from "next/router"

import type { NextPage } from "next"
Expand Down Expand Up @@ -55,16 +55,29 @@ const UpdateEventRegistrationPage: NextPage = () => {
{event?.name[lang]} - {quota?.title[lang]}
</Col>
</Row>
<EventRegistrationForm
formRedirect={{
pathname: "/event/[slug]",
query: { slug: event?.slug },
}}
initialValues={constructInitialValues(registration)}
questions={event?.eventQuestions?.nodes}
type="update"
updateToken={updateToken as string}
/>
{event && !event.signupOpen ? (
<Row>
<Col sm={{ offset: 6 }}>
<Alert
data-cy="update-registration-signup-closed"
message={t("updateRegistration.closed")}
type="info"
showIcon
/>
</Col>
</Row>
) : (
<EventRegistrationForm
formRedirect={{
pathname: "/event/[slug]",
query: { slug: event?.slug },
}}
initialValues={constructInitialValues(registration)}
questions={event?.eventQuestions?.nodes}
type="update"
updateToken={updateToken as string}
/>
)}
</SharedLayout>
)
}
Expand Down
1 change: 1 addition & 0 deletions @app/client/src/translations/en/register_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"title": "Register to event:",
"updateRegistration": {
"closed": "Registration for this event has closed, so your registration can no longer be changed. Please contact the event organizers.",
"title": "Update event registration information"
},
"you": "You!"
Expand Down
1 change: 1 addition & 0 deletions @app/client/src/translations/fi/register_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"title": "Ilmoittaudu tapahtumaan:",
"updateRegistration": {
"closed": "Ilmoittautuminen tähän tapahtumaan on päättynyt, joten ilmoittautumistasi ei voi enää muuttaa. Ota yhteyttä tapahtuman järjestäjiin.",
"title": "Päivitä rekisteröitymistiedot"
},
"you": "Sinä!"
Expand Down
1 change: 1 addition & 0 deletions @app/client/src/translations/se/register_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"title": "Registrera dig till event:",
"updateRegistration": {
"closed": "Registreringen till detta evenemang har stängt, så din registrering kan inte längre ändras. Kontakta evenemangets arrangörer.",
"title": "Uppdatera information om händelseregistrering"
},
"you": "du!"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* A registration is editable through its update token only while event signup
* is open, matching `delete_registration`. Admins edit registrations through
* the same function (after fetching the token with
* `admin_update_registration`), so they are exempt and can correct
* registrations after signup has closed.
*/

create or replace function app_public.update_registration(
"updateToken" text,
"firstName" text,
"lastName" text,
answers jsonb default null
)
returns app_public.registrations as $$
declare
v_registration_secret app_private.registration_secrets;
v_registration app_public.registrations;
v_event app_public.events;
v_required_question_ids uuid[];
begin
select * into v_registration_secret
from app_private.registration_secrets
where update_token = "updateToken";

if v_registration_secret is null then
raise exception 'Registration matching token was not found.' using errcode = 'NTFND';
end if;

select * into v_event from app_public.events where id = v_registration_secret.event_id;

if not app_public.current_user_is_admin()
and ((v_event is null) or not (select app_public.events_signup_open(v_event))) then
raise exception 'Updating a registration after event signup has closed is not allowed. Please contact the event organizers.' using errcode = 'DNIED';
end if;

-- If the registration doesn't provide answers to all of the required questions it is invalid.
-- This is double validated in the _200_registration_is_valid trigger.
v_required_question_ids := array(select id from app_public.event_questions where event_id = v_registration_secret.event_id and is_required = True);
perform app_public.validate_registration_answers(v_registration_secret.event_id, v_required_question_ids, answers);

update app_public.registrations
set first_name = "firstName", last_name = "lastName", answers = update_registration.answers
where id = v_registration_secret.registration_id
returning
* into v_registration;

return v_registration;
end;
$$ language plpgsql volatile security definer set search_path to pg_catalog, public, pg_temp;
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UpdateRegistration admin can update registration after event signup has closed 1`] = `
{
"data": {
"updateRegistration": {
"registration": {
"answers": "[random-3]",
"createdAt": "[timestamp-1]",
"firstName": "Päivi",
"fullName": "Päivi Tetty",
"id": "[id-1]",
"lastName": "Tetty",
"position": 1,
"quota": {
"id": "[id-2]",
"position": 0,
"size": "[random-2]",
"title": "[random-1]",
},
"status": "IN_QUOTA",
"updatedAt": "[timestamp-2]",
},
},
},
}
`;

exports[`UpdateRegistration can update a registration with a valid updateToken 1`] = `
{
"data": {
Expand Down Expand Up @@ -37,7 +63,7 @@ exports[`UpdateRegistration can't update registration if registration token is n
"exception": {
"code": "NTFND",
"severity": "ERROR",
"where": "PL/pgSQL function app_public.update_registration(text,text,text,jsonb) line 12 at RAISE",
"where": "PL/pgSQL function app_public.update_registration(text,text,text,jsonb) line 13 at RAISE",
},
},
"locations": [
Expand Down Expand Up @@ -68,7 +94,7 @@ exports[`UpdateRegistration can't update registration if required answers are no
"severity": "ERROR",
"where": "PL/pgSQL function app_public.validate_registration_answers(uuid,uuid[],jsonb) line 50 at RAISE
SQL statement "SELECT app_public.validate_registration_answers(v_registration_secret.event_id, v_required_question_ids, answers)"
PL/pgSQL function app_public.update_registration(text,text,text,jsonb) line 18 at PERFORM",
PL/pgSQL function app_public.update_registration(text,text,text,jsonb) line 26 at PERFORM",
},
},
"locations": [
Expand All @@ -85,3 +111,32 @@ PL/pgSQL function app_public.update_registration(text,text,text,jsonb) line 18 a
],
}
`;

exports[`UpdateRegistration can't update registration once event signup has closed 1`] = `
{
"data": {
"updateRegistration": null,
},
"errors": [
{
"extensions": {
"exception": {
"code": "DNIED",
"severity": "ERROR",
"where": "PL/pgSQL function app_public.update_registration(text,text,text,jsonb) line 20 at RAISE",
},
},
"locations": [
{
"column": 3,
"line": 2,
},
],
"message": "Updating a registration after event signup has closed is not allowed. Please contact the event organizers.",
"path": [
"updateRegistration",
],
},
],
}
`;
105 changes: 105 additions & 0 deletions @app/server/__tests__/mutations/updateRegistration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UpdateEventRegistrationDocument } from "@app/graphql"
import dayjs from "dayjs"

import {
asRoot,
Expand All @@ -7,9 +8,27 @@ import {
removePropFromObject,
runGraphQLQuery,
setup,
sleep,
teardown,
} from "../helpers"

// Event times where signup is open for one second from now. NOW() runs in the
// database, so tests sleep past the window instead of faking timers.
function shortSignupTimes() {
const registrationStartTime = dayjs().toDate()
const registrationEndTime = dayjs(registrationStartTime)
.add(1, "second")
.toDate()
const eventStartTime = dayjs(registrationEndTime).add(7, "day").toDate()
const eventEndTime = dayjs(eventStartTime).add(7, "day").toDate()
return [
registrationStartTime,
registrationEndTime,
eventStartTime,
eventEndTime,
]
}

beforeEach(deleteTestData)
beforeAll(setup)
afterAll(teardown)
Expand Down Expand Up @@ -133,4 +152,90 @@ describe("UpdateRegistration", () => {
}
)
})

it("can't update registration once event signup has closed", async () => {
const { registrations, registrationSecrets } =
await createEventDataAndLogin({
eventOptions: { create: true, times: shortSignupTimes() },
})
const { update_token: updateToken } = registrationSecrets[0]
const answers = registrations[0].answers

await sleep(1000)

await runGraphQLQuery(
UpdateEventRegistrationDocument,

// GraphQL variables:
{
input: {
updateToken,
firstName: "Päivi",
lastName: "Tetty",
answers,
},
},

// Additional props to add to `req` (e.g. `user: {sessionId: '...'}`)
{},

// This function runs all your test assertions:
async (json) => {
expect(json.errors).toBeTruthy()

const message = json.errors![0].message
const code = json.errors![0].extensions.exception.code
expect(message).toEqual(
"Updating a registration after event signup has closed is not allowed. Please contact the event organizers."
)
expect(code).toEqual("DNIED")
}
)
})

it("admin can update registration after event signup has closed", async () => {
const { registrations, registrationSecrets, session } =
await createEventDataAndLogin({
userOptions: {
create: true,
amount: 1,
isVerified: true,
isAdmin: true,
},
eventOptions: { create: true, times: shortSignupTimes() },
})
const { update_token: updateToken } = registrationSecrets[0]
const answers = registrations[0].answers

await sleep(1000)

await runGraphQLQuery(
UpdateEventRegistrationDocument,

// GraphQL variables:
{
input: {
updateToken,
firstName: "Päivi",
lastName: "Tetty",
answers,
},
},

// Additional props to add to `req` (e.g. `user: {sessionId: '...'}`)
{
user: { sessionId: session.uuid },
},

// This function runs all your test assertions:
async (json) => {
expect(json.errors).toBeFalsy()

const { firstName, lastName } =
json.data!.updateRegistration.registration
expect(firstName).toEqual("Päivi")
expect(lastName).toEqual("Tetty")
}
)
})
})
Loading