From 0cad17af44d29f4c957eb9645d180ec32b8dc205 Mon Sep 17 00:00:00 2001 From: Jamie Whitehouse Date: Tue, 24 Oct 2023 14:21:45 +0200 Subject: [PATCH 1/2] AI-3942: add record id arg to addRecords --- R/records.R | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/R/records.R b/R/records.R index 09aa5f1..dffb3b4 100644 --- a/R/records.R +++ b/R/records.R @@ -23,6 +23,7 @@ recordExists <- function(formId, recordId) { #' @param formId the id of the form to which the record should be added #' @param parentRecordId the id of this record's parent record, if the form is a subform #' @param fieldValues a named list of fields to change. +#' @param recordId the id of the new record when a custom id is desired. The given id must be in cuid-compatible format. #' @export #' @family record functions #' @examples @@ -73,12 +74,16 @@ recordExists <- function(formId, recordId) { #' )) #' #' } -addRecord <- function(formId, parentRecordId = NA_character_, fieldValues) { +addRecord <- function(formId, parentRecordId = NA_character_, fieldValues, recordId = NA_character_) { stopifnot(is.character(formId)) stopifnot(is.character(parentRecordId)) stopifnot(is.list(fieldValues)) - - recordId <- cuid() + stopifnot(is.character(recordId)) + + if (identical(recordId, NA_character_)) { + # generate a record id if not provided + recordId <- cuid() + } changes <- list( list( formId = formId, From fe5874e73e7c3ce3aa3ef53a4cc9ab093f5f2720 Mon Sep 17 00:00:00 2001 From: Jamie Whitehouse Date: Fri, 27 Oct 2023 15:01:10 +0200 Subject: [PATCH 2/2] AI-3942: Check record id exists in form when adding a record with a provided id --- R/records.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/records.R b/R/records.R index dffb3b4..5ce58a2 100644 --- a/R/records.R +++ b/R/records.R @@ -83,6 +83,11 @@ addRecord <- function(formId, parentRecordId = NA_character_, fieldValues, recor if (identical(recordId, NA_character_)) { # generate a record id if not provided recordId <- cuid() + } else { + # check provided record id does not exist before continuing + if (recordExists(formId, recordId)) { + stop(sprintf("Record %s in form %s already exists.", recordId, formId)) + } } changes <- list( list(