The staff member should have the permissions: 'host', 'debug', 'manage_tournament'.
The tournament hasn't concluded nor has it been deleted.
If TournamentForm.type is staff_registration, the tournament's staff regs. close date must be null or in the future.
If TournamentForm.type is staff_registration, set target to public (do not throw an error).
If TournamentForm.type is staff_registration, set anonymousResponses to false (do not throw an error).
The tournament can only have up to 5 forms of type general and 5 forms of type staff_registration (forms are soft deleted, so make sure soft deleted forms aren't included in the count).
Update form
Update the Form and/or TournamentForm record by Form.id.
Input:public, anonymousResponses, closeAt, title, description, thanksMessage, closedMessage, fields, target, tournamentId. (The last one must be defined if a tournament form is being updated).
Conditions
If the form is for a tournament, the staff member should have the permissions: 'host', 'debug', 'manage_tournament'.
If the form is for a tournament, the tournament must not have concluded nor has it been deleted.
If the form is for a tournament and TournamentForm.type is staff_registration, TournamentDates.staffRegsCloseAt must be null or in the future.
If the form is for a tournament and TournamentForm.type is staff_registration, target can't be set (instead of throwing an error, just set target to public).
If the form is public, public can't be set to false.
If the form is public, anonymousResponses can't be changed.
Use userFormFieldsChecks function to check if fields is valid.
If comparing the inputted fields and the already existing fields results in the discovery that a field has been deleted and if that field's ID is present in fieldsWithResponses, then soft delete by marking field's delete as true.
Delete form
Set the date of Form.deletedAt.
Input:formId, deletedAt, tournamentId. (The last one must be defined if a tournament form is being updated).
Conditions
The validation for deletedAt must be v.date([v.minValue(oldestDatePossible), v.maxValue(maxPossibleDate)]).
If the form is for a tournament, the tournament must have not concluded nor has it been deleted.
If the form is for a tournament and TournamentForm.type is staff_registration, TournamentDates.staffRegsCloseAt must be null or in the future.
Submit form response
Insert a record into the FormResponse table.
Input:fieldResponses, formId.
Conditions
A user can only submit one response to a form. (Create unique index).
Each key in fieldResponses must correspond to a field ID.
The value for submittedByUserId must be from session.userId.
Form.closeAt must be null or in the future.
If the form is for a tournament and TournamentForm.target is staff, the user must be a staff member (regardless of roles or permissions).
If the form is for a tournament and TournamentForm.target is players, the user must be a player in the tournament (regardless of tournament type).
If the form is for a tournament and TournamentForm.target is team_captains, the user must be a a team captain (applies to team and draft tournaments; in solo, there's no need to check for this).
If the form is for a tournament, the tournament must have not concluded nor has it been deleted.
If the form is for a tournament and TournamentForm.type is staff_registration, TournamentDates.staffRegsCloseAt must be null or in the future.
Update Form.fieldsWithResponses by putting all the field IDs in fieldResponses into an array and adding any new values to fieldsWithResponses (in other words, no duplicate IDs).
Why?
Admins must be able to create forms for public forms and tournament staff must be able to create forms for any tournament use they may see fit.
How?
For each procedure:
db/forms.ts
file. You can find more details about input validation there.lib/schemas.ts
for plenty of already written schemas for some of these procedures' inputs.Create form
Insert a record into the Form table.
Input:
anonymousResponses
,title
.Conditions
Create tournament form
Insert a record into the TournamentForm table.
Input:
anonymousResponses
,title
,type
,target
,tournamentId
.Conditions
TournamentForm.type
isstaff_registration
, the tournament's staff regs. close date must be null or in the future.TournamentForm.type
isstaff_registration
, settarget
topublic
(do not throw an error).TournamentForm.type
isstaff_registration
, setanonymousResponses
tofalse
(do not throw an error).general
and 5 forms of typestaff_registration
(forms are soft deleted, so make sure soft deleted forms aren't included in the count).Update form
Update the Form and/or TournamentForm record by Form.id.
Input:
public
,anonymousResponses
,closeAt
,title
,description
,thanksMessage
,closedMessage
,fields
,target
,tournamentId
. (The last one must be defined if a tournament form is being updated).Conditions
TournamentForm.type
isstaff_registration
,TournamentDates.staffRegsCloseAt
must be null or in the future.TournamentForm.type
isstaff_registration
,target
can't be set (instead of throwing an error, just settarget
topublic
).public
can't be set to false.anonymousResponses
can't be changed.userFormFieldsChecks
function to check iffields
is valid.fields
and the already existingfields
results in the discovery that a field has been deleted and if that field's ID is present infieldsWithResponses
, then soft delete by marking field'sdelete
as true.Delete form
Set the date of
Form.deletedAt
.Input:
formId
,deletedAt
,tournamentId
. (The last one must be defined if a tournament form is being updated).Conditions
deletedAt
must bev.date([v.minValue(oldestDatePossible), v.maxValue(maxPossibleDate)])
.TournamentForm.type
isstaff_registration
,TournamentDates.staffRegsCloseAt
must be null or in the future.Submit form response
Insert a record into the FormResponse table.
Input:
fieldResponses
,formId
.Conditions
fieldResponses
must correspond to a field ID.submittedByUserId
must be fromsession.userId
.Form.closeAt
must be null or in the future.TournamentForm.target
isstaff
, the user must be a staff member (regardless of roles or permissions).TournamentForm.target
isplayers
, the user must be a player in the tournament (regardless of tournament type).TournamentForm.target
isteam_captains
, the user must be a a team captain (applies to team and draft tournaments; in solo, there's no need to check for this).TournamentForm.type
isstaff_registration
,TournamentDates.staffRegsCloseAt
must be null or in the future.Form.fieldsWithResponses
by putting all the field IDs infieldResponses
into an array and adding any new values tofieldsWithResponses
(in other words, no duplicate IDs).