mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-10-06 01:52:30 -04:00
65 lines
1.5 KiB
TypeScript
65 lines
1.5 KiB
TypeScript
import { Realm } from "@realm/react";
|
|
import type { ApiProjectObservation } from "api/types";
|
|
import * as uuid from "uuid";
|
|
|
|
class ProjectObservation extends Realm.Object {
|
|
_created_at?: Date;
|
|
|
|
_synced_at?: Date;
|
|
|
|
_updated_at?: Date;
|
|
|
|
needsSync( ) {
|
|
return !this._synced_at || this._synced_at <= this._updated_at;
|
|
}
|
|
|
|
wasSynced( ) {
|
|
return this._synced_at !== null;
|
|
}
|
|
|
|
static new( projectId: number ) {
|
|
return {
|
|
_created_at: new Date( ),
|
|
_updated_at: new Date( ),
|
|
uuid: uuid.v4( ).toLowerCase( ),
|
|
projectId,
|
|
};
|
|
}
|
|
|
|
static mapApiToRealm(
|
|
apiPo: ApiProjectObservation,
|
|
) {
|
|
const localPo = {
|
|
...apiPo,
|
|
projectId: apiPo.project_id ?? apiPo.project?.id,
|
|
_synced_at: new Date( ),
|
|
};
|
|
return localPo;
|
|
}
|
|
|
|
static schema = {
|
|
name: "ProjectObservation",
|
|
embedded: true,
|
|
properties: {
|
|
// datetime the PO was created on the device
|
|
_created_at: "date?",
|
|
// datetime the PO was last synced with the server
|
|
_synced_at: "date?",
|
|
// datetime the PO was updated on the device (i.e. edited locally)
|
|
_updated_at: "date?",
|
|
uuid: "string",
|
|
id: "int?",
|
|
projectId: "int",
|
|
// this creates an inverse relationship so POs
|
|
// automatically keep track of which Observation they are assigned to
|
|
assignee: {
|
|
type: "linkingObjects",
|
|
objectType: "Observation",
|
|
property: "projectObservations",
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
export default ProjectObservation;
|