• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

akvo / nmis-mobile / 5878601508

16 Aug 2023 12:13PM UTC coverage: 85.797% (-0.7%) from 86.52%
5878601508

Pull #143

github

ifirmawan
[#142] Push all images link to the sync endpoint
Pull Request #143: Feature/142 upload images and sync it

713 of 895 branches covered (79.66%)

Branch coverage included in aggregate %.

39 of 39 new or added lines in 5 files covered. (100.0%)

1510 of 1696 relevant lines covered (89.03%)

20.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.11
/app/src/lib/background-task.js
1
import { crudForms, crudSessions, crudDataPoints, crudUsers } from '../database/crud';
2
import api from './api';
3
import * as BackgroundFetch from 'expo-background-fetch';
4
import * as TaskManager from 'expo-task-manager';
5

6
const syncFormVersion = async ({
35✔
7
  showNotificationOnly = true,
×
8
  sendPushNotification = () => {},
×
9
}) => {
10
  try {
2✔
11
    // find last session
12
    const session = await crudSessions.selectLastSession();
2✔
13
    if (!session) {
2!
14
      return;
×
15
    }
16
    const authenticationCode = session.passcode;
2✔
17
    const data = new FormData();
2✔
18
    data.append('code', authenticationCode);
2✔
19
    api
2✔
20
      .post('/auth', data, { headers: { 'Content-Type': 'multipart/form-data' } })
21
      .then(async (res) => {
22
        const { data } = res;
2✔
23
        const promises = data.formsUrl.map(async (form) => {
2✔
24
          const formExist = await crudForms.selectFormByIdAndVersion({ ...form });
2✔
25
          if (formExist) {
2!
26
            return false;
×
27
          }
28
          if (showNotificationOnly) {
2✔
29
            console.info('[bgTask]New form:', form.id, form.version);
1✔
30
            return { id: form.id, version: form.version };
1✔
31
          }
32
          const formRes = await api.get(form.url);
1✔
33
          // update previous form latest value to 0
34
          await crudForms.updateForm({ ...form });
1✔
35
          console.info('[syncForm]Updated Forms...', form.id);
1✔
36
          const savedForm = await crudForms.addForm({ ...form, formJSON: formRes?.data });
1✔
37
          console.info('[syncForm]Saved Forms...', form.id);
1✔
38
          return savedForm;
1✔
39
        });
40
        Promise.all(promises).then(async (res) => {
2✔
41
          const exist = res.filter((x) => x);
2✔
42
          if (!exist.length || !showNotificationOnly) {
2✔
43
            return;
1✔
44
          }
45
          sendPushNotification();
1✔
46
        });
47
      });
48
  } catch (err) {
49
    console.error('[bgTask]sycnFormVersion failed:', err);
×
50
  }
51
};
52

53
const registerBackgroundTask = async (TASK_NAME, minimumInterval = 86400) => {
35✔
54
  try {
2✔
55
    await BackgroundFetch.registerTaskAsync(TASK_NAME, {
2✔
56
      minimumInterval: minimumInterval,
57
      stopOnTerminate: false, // android only,
58
      startOnBoot: true, // android only
59
    });
60
  } catch (err) {
61
    console.error('Task Register failed:', err);
×
62
  }
63
};
64

65
const unregisterBackgroundTask = async (TASK_NAME) => {
35✔
66
  try {
1✔
67
    await BackgroundFetch.unregisterTaskAsync(TASK_NAME);
1✔
68
  } catch (err) {
69
    console.error('Task Unregister failed:', err);
×
70
  }
71
};
72

73
const backgroundTaskStatus = async (TASK_NAME, minimumInterval = 86400) => {
35✔
74
  const status = await BackgroundFetch.getStatusAsync();
1✔
75
  const isRegistered = await TaskManager.isTaskRegisteredAsync(TASK_NAME);
1✔
76
  if (BackgroundFetch.BackgroundFetchStatus?.[status] === 'Available' && !isRegistered) {
1!
77
    await registerBackgroundTask(TASK_NAME, minimumInterval);
1✔
78
  }
79
  console.log(`[${TASK_NAME}] Status`, status, isRegistered, minimumInterval);
1✔
80
};
81

82
const syncFormSubmission = async (photos = []) => {
35✔
83
  try {
1✔
84
    console.info('[syncFormSubmision] SyncData started');
1✔
85
    // get token
86
    const session = await crudSessions.selectLastSession();
1✔
87
    // set token
88
    api.setToken(session.token);
1✔
89
    // get all datapoints to sync
90
    const data = await crudDataPoints.selectSubmissionToSync();
1✔
91
    console.info('[syncFormSubmision] data point to sync:', data.length);
1✔
92
    const syncProcess = data.map(async (d) => {
1✔
93
      // get user
94
      const user = await crudUsers.selectUserById({ id: d.user });
1✔
95
      const form = await crudForms.selectFormById({ id: d.form });
1✔
96
      const geo = d.geo ? d.geo.split('|')?.map((x) => parseFloat(x)) : [];
2!
97

98
      const answerValues = JSON.parse(d.json.replace(/''/g, "'"));
1✔
99
      photos?.forEach((pt) => {
1✔
100
        answerValues[pt?.id] = pt?.value;
×
101
      });
102
      const syncData = {
1✔
103
        formId: form.formId,
104
        name: d.name,
105
        duration: Math.round(d.duration),
106
        submittedAt: d.submittedAt,
107
        submitter: user.name,
108
        geo,
109
        answers: answerValues,
110
      };
111
      console.info('[syncFormSubmision] SyncData:', syncData);
1✔
112
      // sync data point
113
      const res = await api.post('/sync', syncData);
1✔
114
      console.info('[syncFormSubmision] post sync data point:', res.status, res.data);
1✔
115
      if (res.status === 200) {
1!
116
        // update data point
117
        await crudDataPoints.updateDataPoint({
1✔
118
          ...d,
119
          syncedAt: new Date().toISOString(),
120
        });
121
        console.info('[syncFormSubmision] updated data point syncedAt:', d.id);
1✔
122
      }
123
      return {
1✔
124
        datapoint: d.id,
125
        status: res.status,
126
      };
127
    });
128
    return Promise.all(syncProcess).then(async (res) => {
1✔
129
      return res;
1✔
130
    });
131
  } catch (err) {
132
    console.error('[syncFormSubmission] Error: ', err);
×
133
  }
134
};
135

136
const backgroundTaskHandler = () => {
35✔
137
  return {
35✔
138
    syncFormVersion,
139
    registerBackgroundTask,
140
    unregisterBackgroundTask,
141
    backgroundTaskStatus,
142
    syncFormSubmission,
143
  };
144
};
145

146
const backgroundTask = backgroundTaskHandler();
35✔
147
export default backgroundTask;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc