Skip to content

Commit 97eb1c9

Browse files
committed
added ADD_RECORDS_FROM_KOBO mutation
1 parent d63873a commit 97eb1c9

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

apps/back-office/src/app/components/synchronize-kobo-modal/graphql/mutations.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ export const EDIT_FORM_KOBO_PREFERENCES = gql`
1212
}
1313
}
1414
`;
15+
16+
/** For the form created from a Kobotoolbox form, import data submissions to create records. */
17+
export const ADD_RECORDS_FROM_KOBO = gql`
18+
mutation addRecordsFromKobo($form: ID!) {
19+
addRecordsFromKobo(form: $form)
20+
}
21+
`;

apps/back-office/src/app/components/synchronize-kobo-modal/synchronize-kobo-modal.component.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
<h3 class="font-semibold">{{ 'components.form.create.kobo.data.synchronize' | translate }}</h3>
44
</ng-container>
55
<ng-container ngProjectAs="content">
6+
<ui-spinner *ngIf="loading" size="medium"></ui-spinner>
7+
<div class="flex flex-col items-center my-2">
8+
<ui-button category="secondary" variant="primary" (click)="onSynchronize()">{{
9+
'components.form.create.kobo.data.start' | translate
10+
}}</ui-button>
11+
</div>
612
<ui-alert>
713
{{ 'components.form.create.kobo.data.fromFormWithVersion' | translate : { formName: data.form.name, versionId: data.deployedVersionId} }}
814
</ui-alert>
915
<!-- Loading indicator -->
10-
<ui-spinner *ngIf="loading" size="medium"></ui-spinner>
1116
<form [formGroup]="formGroup" class="mt-2">
1217
<ui-toggle formControlName="dataFromDeployedVersion" class="mb-5">
1318
<ng-container ngProjectAs="label">

apps/back-office/src/app/components/synchronize-kobo-modal/synchronize-kobo-modal.component.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TooltipModule,
1515
} from '@oort-front/ui';
1616
import {
17+
AddRecordsFromKoboMutationResponse,
1718
ApiConfiguration,
1819
EditFormMutationResponse,
1920
UnsubscribeComponent,
@@ -30,7 +31,10 @@ interface DialogData {
3031
apiConfiguration: ApiConfiguration;
3132
form: { id: string; name: string };
3233
}
33-
import { EDIT_FORM_KOBO_PREFERENCES } from './graphql/mutations';
34+
import {
35+
ADD_RECORDS_FROM_KOBO,
36+
EDIT_FORM_KOBO_PREFERENCES,
37+
} from './graphql/mutations';
3438

3539
/**
3640
* Synchronize kobo form data (modal, import data submissions from kobo)
@@ -131,6 +135,54 @@ export class SynchronizeKoboModalComponent extends UnsubscribeComponent {
131135
});
132136
}
133137

138+
/**
139+
* Synchronize data submissions from kobo
140+
*/
141+
public onSynchronize() {
142+
this.loading = true;
143+
this.apollo
144+
.mutate<AddRecordsFromKoboMutationResponse>({
145+
mutation: ADD_RECORDS_FROM_KOBO,
146+
variables: {
147+
form: this.data.form.id,
148+
},
149+
})
150+
.pipe(takeUntil(this.destroy$))
151+
.subscribe({
152+
next: ({ errors, data }) => {
153+
if (!errors) {
154+
if (data?.addRecordsFromKobo) {
155+
this.snackBar.openSnackBar(
156+
this.translate.instant('common.notifications.objectCreated', {
157+
value: this.translate.instant('common.record.few'),
158+
type: '',
159+
})
160+
);
161+
} else {
162+
this.snackBar.openSnackBar(
163+
this.translate.instant(
164+
'components.form.create.kobo.data.nothingToSynchronize'
165+
)
166+
);
167+
}
168+
} else {
169+
this.snackBar.openSnackBar(
170+
this.translate.instant('common.notifications.objectNotCreated', {
171+
type: this.translate.instant('common.record.few').toLowerCase(),
172+
error: errors ? errors[0].message : '',
173+
}),
174+
{ error: true }
175+
);
176+
}
177+
this.loading = false;
178+
},
179+
error: (err) => {
180+
this.loading = false;
181+
this.snackBar.openSnackBar(err.message, { error: true });
182+
},
183+
});
184+
}
185+
134186
/**
135187
* Create the form group
136188
*

libs/shared/src/i18n/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,10 @@
842842
"placeholder": "Enter a KoboToolbox form id",
843843
"data": {
844844
"synchronize": "Synchronize kobo data",
845+
"start": "Start synchronization",
845846
"fromFormWithVersion": "The form '{{formName}}' was created from the original Kobotoolbox form with the deploy version id {{versionId}}",
846847
"syncOnlyFromDeployedVersion": "Synchronize data submissions only from the same form deploy version",
848+
"nothingToSynchronize": "No data found for synchronization",
847849
"tooltip": {
848850
"syncOnlyFromDeployedVersion": "Only data submissions sent when the form was in the same version as when it was imported will be used to create records. Data submissions registered in earlier or later versions will be disregarded."
849851
}

libs/shared/src/i18n/fr.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,10 @@
853853
"placeholder": "Entrez l'identifiant du formulaire KoboToolbox",
854854
"data": {
855855
"synchronize": "Synchroniser les données Kobo",
856+
"start": "Démarrer la synchronisation",
856857
"fromFormWithVersion": "Le formulaire '{{formName}}' a été créé à partir du formulaire Kobotoolbox d'origine avec l'ID de version de déploiement {{versionId}}",
857858
"syncOnlyFromDeployedVersion": "Synchronisez les soumissions de données uniquement à partir de la même version de déploiement du formulaire",
859+
"nothingToSynchronize": "Aucune donnée trouvée pour la synchronisation",
858860
"tooltip": {
859861
"syncOnlyFromDeployedVersion": "Seules les soumissions de données envoyées lorsque le formulaire était dans la même version que lors de son importation seront utilisées pour créer des enregistrements. Les soumissions de données enregistrées dans des versions antérieures ou ultérieures seront ignorées."
860862
}

libs/shared/src/lib/models/record.model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface Record {
2626
modifiedBy?: User;
2727
canUpdate?: boolean;
2828
canDelete?: boolean;
29+
koboId?: string;
2930
validationErrors?: { question: string; errors: string[] }[];
3031
}
3132

@@ -88,3 +89,8 @@ export interface DeleteRecordsMutationResponse {
8889
export interface GenerateRecordsMutationResponse {
8990
generateRecords: Record[];
9091
}
92+
93+
/** Model for add records from kobo graphql mutation response */
94+
export interface AddRecordsFromKoboMutationResponse {
95+
addRecordsFromKobo: boolean;
96+
}

0 commit comments

Comments
 (0)