Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.
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
98 changes: 67 additions & 31 deletions src/draft-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import _orderBy from 'lodash.orderby'
import utilsService from './services/utils'
import OneBlinkAppsError from './services/errors/oneBlinkAppsError'
import { isOffline } from './offline-service'
import { getUsername, isLoggedIn } from './services/cognito'
import { getFormsKeyId } from './auth-service'
import { getUsername } from './services/cognito'
import { getFormsKeyId, getCurrentFormsAppUser } from './auth-service'
import { getFormSubmissionDrafts, uploadDraftData } from './services/api/drafts'
import {
getPendingQueueSubmissions,
Expand Down Expand Up @@ -41,6 +41,26 @@ interface LocalDraftsStorage {
syncedFormSubmissionDrafts: SubmissionTypes.FormSubmissionDraft[]
}

async function checkIfUsingPrivateDrafts(
formsAppId: number,
abortSignal?: AbortSignal,
): Promise<boolean> {
return getCurrentFormsAppUser(formsAppId, abortSignal)
.then((user) => !!user)
.catch((error) => {
if (error.status >= 400 && error.status < 500) {
return false
} else {
Sentry.captureException(error)
console.log(
'Could not determine if the current user has access to this forms app',
error,
)
return false
}
})
}

function generateLocalFormSubmissionDraftsFromDraftSubmissions(
draftSubmissions: DraftSubmission[],
pendingSubmissionsDraftIds: Set<string>,
Expand Down Expand Up @@ -214,7 +234,6 @@ function registerDraftsListener(
async function executeDraftsListeners(
localFormSubmissionDrafts: LocalFormSubmissionDraft[],
) {
console.log('Drafts have been updated', localFormSubmissionDrafts)
for (const draftsListener of draftsListeners) {
draftsListener(localFormSubmissionDrafts)
}
Expand Down Expand Up @@ -291,14 +310,18 @@ async function upsertDraft({
}

try {
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(
draftSubmission.formsAppId,
)
const formSubmissionDraftVersion = await saveDraftSubmission({
draftSubmission,
autoSaveKey,
onProgress,
skipUpload: !isUsingPrivateDrafts,
})

if (isLoggedIn()) {
const localDraftsStorage = await getLocalDrafts()
if (isUsingPrivateDrafts) {
const localDraftsStorage = await getLocalDraftsFromStorage()

if (formSubmissionDraftVersion) {
console.log('Draft was saved on server', formSubmissionDraftVersion)
Expand Down Expand Up @@ -349,7 +372,7 @@ async function upsertDraft({
await setAndBroadcastDrafts(localDraftsStorage)
} else {
let updated = false
const publicDraftsStorage = (await getPublicDrafts()).map(
const publicDraftsStorage = (await getPublicDraftsFromStorage()).map(
(publicDraftSubmission) => {
if (
publicDraftSubmission.formSubmissionDraftId ===
Expand Down Expand Up @@ -380,7 +403,7 @@ async function upsertDraft({
}
}

async function getLocalDrafts(): Promise<LocalDraftsStorage> {
async function getLocalDraftsFromStorage(): Promise<LocalDraftsStorage> {
const username = getUsername()
if (username) {
try {
Expand All @@ -404,7 +427,7 @@ async function getLocalDrafts(): Promise<LocalDraftsStorage> {
}
}

async function getPublicDrafts(): Promise<DraftSubmission[]> {
async function getPublicDraftsFromStorage(): Promise<DraftSubmission[]> {
try {
const publicDrafts = await utilsService.localForage.getItem<
DraftSubmission[]
Expand Down Expand Up @@ -432,17 +455,26 @@ async function getPublicDrafts(): Promise<DraftSubmission[]> {
* @returns
*/
async function getDrafts(): Promise<LocalFormSubmissionDraft[]> {
if (isLoggedIn()) {
const localDraftsStorage = await getLocalDrafts()
return await generateLocalFormSubmissionDraftsFromStorage(
localDraftsStorage,
)
} else {
const publicDraftsStorage = await getPublicDrafts()
return await generatePublicLocalFormSubmissionDraftsFromStorage(
publicDraftsStorage,
)
}
const localDraftsStorage = await getLocalDraftsFromStorage()
return await generateLocalFormSubmissionDraftsFromStorage(localDraftsStorage)
}

/**
* Get an array of Drafts that have been submitted publicly on this device.
*
* #### Example
*
* ```js
* const drafts = await draftService.getPublicDrafts()
* ```
*
* @returns
*/
async function getPublicDrafts(): Promise<LocalFormSubmissionDraft[]> {
const publicDraftsStorage = await getPublicDraftsFromStorage()
return await generatePublicLocalFormSubmissionDraftsFromStorage(
publicDraftsStorage,
)
}

async function tryGetFormSubmissionDrafts(
Expand Down Expand Up @@ -486,8 +518,9 @@ async function getDraftAndData(
formsAppId,
abortSignal,
)
if (isLoggedIn()) {
const localDraftsStorage = await getLocalDrafts()
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(formsAppId)
if (isUsingPrivateDrafts) {
const localDraftsStorage = await getLocalDraftsFromStorage()
if (formSubmissionDrafts) {
localDraftsStorage.syncedFormSubmissionDrafts = formSubmissionDrafts
await setAndBroadcastDrafts(localDraftsStorage)
Expand Down Expand Up @@ -531,8 +564,9 @@ async function deleteDraft(
): Promise<void> {
try {
await removeLocalDraftSubmission(formSubmissionDraftId)
if (isLoggedIn()) {
const localDraftsStorage = await getLocalDrafts()
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(formsAppId)
if (isUsingPrivateDrafts) {
const localDraftsStorage = await getLocalDraftsFromStorage()
const formSubmissionDraft =
localDraftsStorage.syncedFormSubmissionDrafts.find(
({ id }) => id === formSubmissionDraftId,
Expand Down Expand Up @@ -577,7 +611,7 @@ async function deleteDraft(

await setAndBroadcastDrafts(localDraftsStorage)
} else {
let publicDraftsStorage = await getPublicDrafts()
let publicDraftsStorage = await getPublicDraftsFromStorage()
const draftSubmission = publicDraftsStorage.find(
(draftSubmission) =>
draftSubmission.formSubmissionDraftId === formSubmissionDraftId,
Expand Down Expand Up @@ -678,10 +712,10 @@ async function syncDrafts({
}
_isSyncingDrafts = true

console.log('Start attempting to sync drafts.')
const isUsingPrivateDrafts = await checkIfUsingPrivateDrafts(formsAppId)

if (!isLoggedIn()) {
const publicDrafts = await getPublicDrafts()
if (!isUsingPrivateDrafts) {
const publicDrafts = await getPublicDraftsFromStorage()
const filteredPublicDrafts = []
// iterate through public draft records, and check if a draft submission exists for each record.
// If no draft submission exists for a record, the draft was likely submitted, so we must remove it
Expand All @@ -700,7 +734,7 @@ async function syncDrafts({
}

try {
let localDraftsStorage = await getLocalDrafts()
let localDraftsStorage = await getLocalDraftsFromStorage()
if (localDraftsStorage.deletedFormSubmissionDrafts.length) {
console.log(
'Removing local draft data for deleted drafts',
Expand All @@ -719,12 +753,12 @@ async function syncDrafts({
}

// Get local drafts again to ensure nothing has happened while processing
localDraftsStorage = await getLocalDrafts()
localDraftsStorage = await getLocalDraftsFromStorage()
localDraftsStorage.deletedFormSubmissionDrafts =
newDeletedFormSubmissionDrafts
}

const publicDraftsStorage = await getPublicDrafts()
const publicDraftsStorage = await getPublicDraftsFromStorage()
// if public drafts exist, add them to the current logged in users' unsynced drafts
// and remove them from local storage
if (publicDraftsStorage.length) {
Expand All @@ -747,13 +781,14 @@ async function syncDrafts({
draftSubmission,
autoSaveKey: undefined,
abortSignal,
skipUpload: !isUsingPrivateDrafts,
})
if (!formSubmissionDraftVersion) {
newUnsyncedDraftSubmissions.push(draftSubmission)
}
}
// Get local drafts again to ensure nothing has happened while processing
localDraftsStorage = await getLocalDrafts()
localDraftsStorage = await getLocalDraftsFromStorage()
localDraftsStorage.unsyncedDraftSubmissions = newUnsyncedDraftSubmissions
}

Expand Down Expand Up @@ -807,6 +842,7 @@ export {
upsertDraft,
getDraftAndData,
getDrafts,
getPublicDrafts,
deleteDraft,
syncDrafts,
getLatestFormSubmissionDraftVersion,
Expand Down
5 changes: 3 additions & 2 deletions src/services/draft-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { SubmissionTypes } from '@oneblink/types'
import Sentry from '../Sentry'
import { DraftSubmission, ProgressListener } from '../types/submissions'
import { deleteAutoSaveData } from '../auto-save-service'
import { isLoggedIn } from './cognito'

function getLocalDraftSubmissionKey(formSubmissionDraftId: string) {
return `DRAFT_SUBMISSION_${formSubmissionDraftId}`
Expand Down Expand Up @@ -47,11 +46,13 @@ export async function saveDraftSubmission({
autoSaveKey,
onProgress,
abortSignal,
skipUpload,
}: {
draftSubmission: DraftSubmission
autoSaveKey: string | undefined
onProgress?: ProgressListener
abortSignal?: AbortSignal
skipUpload?: boolean
}): Promise<SubmissionTypes.FormSubmissionDraftVersion | undefined> {
await setLocalDraftSubmission(draftSubmission)

Expand All @@ -60,7 +61,7 @@ export async function saveDraftSubmission({
}

try {
if (isLoggedIn()) {
if (!skipUpload) {
return await uploadDraftData(draftSubmission, onProgress, abortSignal)
}
} catch (error) {
Expand Down