Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Created February 4, 2021 21:34
Show Gist options
  • Save rjcorwin/56f09e87999d6dcf1174d1156751d44d to your computer and use it in GitHub Desktop.
Save rjcorwin/56f09e87999d6dcf1174d1156751d44d to your computer and use it in GitHub Desktop.
async function cloneThisCase(toGen = 0) {
const db = await T.user.getUserDatabase()
let i = 0
while (i < toGen) {
const doc = JSON.parse(JSON.stringify(T.case.case))
const newCaseId = uuid()
doc._id = newCaseId
delete doc._rev
for (let event of doc.events) {
let newEventId = uuid()
event.id = newEventId
event.caseId = newCaseId
for (let eventForm of event.eventForms) {
let newEventFormId = uuid()
eventForm.id = newEventFormId
eventForm.caseId = newCaseId
eventForm.caseEventId = newEventId
if (eventForm.formResponseId) {
let formResponse = {}
try {
formResponse = await db.get(eventForm.formResponseId)
} catch (e) {
console.log('Warning, no form response to copy. Will be minimal data.')
}
const newFormResponseId = uuid()
eventForm.formResponseId = newFormResponseId
formResponse._id = newFormResponseId
delete formResponse._rev
formResponse.caseId = newCaseId
formResponse.caseEventId = newEventId
formResponse.eventFormId = newEventFormId
await db.put(formResponse)
}
}
}
await db.put(doc)
i++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment