-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathConvertGpsPointsTo2DArray.ts
More file actions
64 lines (53 loc) · 2.67 KB
/
Copy pathConvertGpsPointsTo2DArray.ts
File metadata and controls
64 lines (53 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import is2dArray from '@libs/is2dArray';
import Log from '@libs/Log';
import ONYXKEYS from '@src/ONYXKEYS';
import type {GpsDraftDetails} from '@src/types/onyx';
import type {GPSPoint, GPSPointAddress} from '@src/types/onyx/GpsDraftDetails';
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
type OldGpsDraftDetails = Omit<GpsDraftDetails, 'gpsPoints'> & {
gpsPoints: GPSPoint[] | GPSPoint[][];
startAddress?: GPSPointAddress;
endAddress?: GPSPointAddress;
};
export default function (): Promise<void> {
return new Promise<void>((resolve) => {
const connection = Onyx.connectWithoutView({
key: ONYXKEYS.GPS_DRAFT_DETAILS,
callback: (gpsDraftDetails: OnyxEntry<OldGpsDraftDetails>) => {
Onyx.disconnect(connection);
// gpsPoints may still be in the old 1D format from before the schema change
const gpsPoints = gpsDraftDetails?.gpsPoints;
// If gpsPoints is already in the correct format or is empty, skip the migration
if (!gpsPoints || is2dArray<GPSPoint>(gpsPoints)) {
Log.info('[Migrate Onyx] Skipped ConvertGpsPointsTo2DArray — already correct format or empty');
return resolve();
}
let newGpsPoints: GPSPoint[] = [...gpsPoints];
// gpsDraftDetails is not migrated, we need to check and move startAddress and endAddress values
// to the first and last points of the gpsPoints array
if (gpsDraftDetails?.startAddress?.value || gpsDraftDetails?.endAddress?.value) {
newGpsPoints = newGpsPoints.map((point, index) => {
let address: GPSPointAddress | undefined;
if (index === 0) {
address = gpsDraftDetails?.startAddress;
} else if (index === gpsPoints.length - 1) {
address = gpsDraftDetails?.endAddress;
}
return {
...point,
address,
};
});
}
const migratedPoints = [newGpsPoints];
// No need to add a new action just for this migration
// eslint-disable-next-line rulesdir/prefer-actions-set-data
Onyx.merge(ONYXKEYS.GPS_DRAFT_DETAILS, {gpsPoints: migratedPoints}).then(() => {
Log.info('[Migrate Onyx] Ran ConvertGpsPointsTo2DArray migration');
resolve();
});
},
});
});
}