This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Importing of Registration (#314)
* added frontend for importing * add route * don't check if guest status is restricted * add import route to index * added csv import * correctly redirect to registrations edit * run rubocop * run eslint
- Loading branch information
1 parent
7d7d76f
commit b387c91
Showing
11 changed files
with
137 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/src/api/schema.d.ts | ||
/src/api/schema.d.ts | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getJWT } from '../../auth/get_jwt' | ||
|
||
export default async function importRegistration(body: { | ||
competitionId: string | ||
file: File | ||
}): Promise<{ status: string }> { | ||
const formData = new FormData() | ||
formData.append('csv_data', body.file) | ||
const response = await fetch( | ||
`${process.env.API_URL}/${body.competitionId}/import`, | ||
{ | ||
method: 'POST', | ||
body: formData, | ||
headers: { | ||
Authorization: await getJWT(), | ||
}, | ||
} | ||
) | ||
return response.json() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useMutation } from '@tanstack/react-query' | ||
import React, { useContext, useState } from 'react' | ||
import { useNavigate } from 'react-router-dom' | ||
import { Button, Input, Segment } from 'semantic-ui-react' | ||
import { CompetitionContext } from '../../api/helper/context/competition_context' | ||
import { PermissionsContext } from '../../api/helper/context/permission_context' | ||
import importRegistration from '../../api/registration/post/import_registration' | ||
import { BASE_ROUTE } from '../../routes' | ||
import PermissionMessage from '../../ui/messages/permissionMessage' | ||
|
||
export default function Import() { | ||
const [file, setFile] = useState() | ||
const navigate = useNavigate() | ||
const { competitionInfo } = useContext(CompetitionContext) | ||
const { canAdminCompetition } = useContext(PermissionsContext) | ||
const { mutate: importMutation, isLoading: isMutating } = useMutation({ | ||
mutationFn: importRegistration, | ||
onSuccess: () => | ||
navigate(`${BASE_ROUTE}/${competitionInfo.id}/registrations/edit`), | ||
}) | ||
return !canAdminCompetition ? ( | ||
<PermissionMessage> | ||
You are not allowed to import registrations. | ||
</PermissionMessage> | ||
) : ( | ||
<Segment> | ||
<Input | ||
type="file" | ||
accept="text/csv" | ||
onChange={(event) => setFile(event.target.files[0])} | ||
/> | ||
<Button | ||
disabled={!file || isMutating} | ||
onClick={() => | ||
importMutation({ competitionId: competitionInfo.id, file }) | ||
} | ||
> | ||
Upload CSV | ||
</Button> | ||
</Segment> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
user_id,guests,competing.event_ids,competing.registration_status,competing.registered_on,competing.comment,competing.admin_comment | ||
15073,0,333;444,pending,2023-11-03T15:47:24.764Z,test,test2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module CsvImport | ||
HEADERS = %w[user_id guests competing.event_ids competing.registration_status competing.registered_on competing.comment competing.admin_comment].freeze | ||
def self.valid?(csv) | ||
data = CSV.parse(csv) | ||
headers = data.first | ||
headers.present? && headers.all? { |h| h.in?(HEADERS) } | ||
end | ||
|
||
def self.parse_row_to_registration(csv_hash, competition_id) | ||
{ | ||
attendee_id: "#{competition_id}-#{csv_hash["user_id"]}", | ||
user_id: csv_hash['user_id'], | ||
competition_id: competition_id, | ||
lanes: [LaneFactory.competing_lane(csv_hash['competing.event_ids'].split(';'), csv_hash['competing.comment'], csv_hash['guests'], csv_hash['competing.admin_comment'], csv_hash['competing.registration_status'])], | ||
isCompeting: csv_hash['competing.registration_status'] == 'accepted', | ||
} | ||
end | ||
end |