diff --git a/imports/api/ActiveFiresUnion/ActiveFiresUnion.js b/imports/api/ActiveFiresUnion/ActiveFiresUnion.js new file mode 100644 index 0000000..2437e54 --- /dev/null +++ b/imports/api/ActiveFiresUnion/ActiveFiresUnion.js @@ -0,0 +1,71 @@ +/* eslint-disable consistent-return */ +/* eslint-disable import/no-absolute-path */ + +import { Mongo } from 'meteor/mongo'; +import SimpleSchema from 'simpl-schema'; +import LocationSchema from '/imports/api/Utility/LocationSchema.js'; +import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js'; + +const ActiveFiresUnion = new Mongo.Collection('activefiresunion', { idGeneration: 'MONGO' }); + +/* Sample: + * + * TODO + * + * */ + +// We have to store polygons and their centroids +// {"type":"Polygon","coordinates":[[[-5.8645,43.5524],[-5.8621,43.5524],[-5.8621,43.5546],[-5.8645,43.5546],[-5.8645,43.5524]]]} + +ActiveFiresUnion.allow({ + insert: () => false, + update: () => false, + remove: () => false +}); + +ActiveFiresUnion.deny({ + insert: () => true, + update: () => true, + remove: () => true +}); + +// https://www.npmjs.com/package/simpl-schema +const GeoJsonSchema = new SimpleSchema({ + type: { + type: String, + allowedValues: ['Polygon'] + }, + coordinates: { + type: Array + }, + 'coordinates.$': { + type: Array + }, + 'coordinates.$.$': { + type: Array + }, + 'coordinates.$.$.$': { + type: Number + } +}); + +const firesUnionSchema = { + centerid: LocationSchema, + + // https://docs.mongodb.com/manual/reference/geojson/ + // : { type: , coordinates: } + shape: GeoJsonSchema, + history: { type: Array, optional: true }, + 'history.$': GeoJsonSchema, + + when: Date, // First date + + createdAt: defaultCreatedAt, + updatedAt: defaultUpdateAt +}; + +ActiveFiresUnion.schema = new SimpleSchema(firesUnionSchema); + +ActiveFiresUnion.attachSchema(ActiveFiresUnion.schema); + +export default ActiveFiresUnion; diff --git a/imports/startup/server/migrations.js b/imports/startup/server/migrations.js index defc7c1..e3bfac9 100644 --- a/imports/startup/server/migrations.js +++ b/imports/startup/server/migrations.js @@ -10,6 +10,7 @@ import FalsePositives from '/imports/api/FalsePositives/FalsePositives'; import Industries from '/imports/api/Industries/Industries'; import IndustryRegistries from '/imports/api/Industries/IndustryRegistries'; import Notifications from '/imports/api/Notifications/Notifications'; +import ActiveFiresUnion from '/imports/api/ActiveFiresUnion/ActiveFiresUnion'; import { Mongo } from 'meteor/mongo'; Meteor.startup(() => { @@ -240,6 +241,18 @@ Meteor.startup(() => { } }); + Migrations.add({ + version: 18, + up: () => { + const raw = ActiveFiresUnion.rawCollection(); + raw.createIndex({ centerid: '2dsphere' }); + raw.createIndex({ shape: '2dsphere' }); + raw.createIndex({ when: 1 }); + raw.createIndex({ createdAt: 1 }); + raw.createIndex({ updatedAt: 1 }); + } + }); + // Set createdAt in users & subs Migrations.migrateTo('latest'); diff --git a/test/activeFiresUnion.test.js b/test/activeFiresUnion.test.js new file mode 100644 index 0000000..1a1a52e --- /dev/null +++ b/test/activeFiresUnion.test.js @@ -0,0 +1,29 @@ +/* eslint-env mocha */ +/* eslint-disable func-names, prefer-arrow-callback */ +/* eslint-disable import/no-absolute-path */ + +import { chai } from 'meteor/practicalmeteor:chai'; +import ActiveFiresUnion from '/imports/api/ActiveFiresUnion/ActiveFiresUnion'; + +const centerid = { type: 'Point', coordinates: [-5.905956029891968, 43.55727622097011] }; +const shape = { type: 'Polygon', coordinates: [[[-5.9084, 43.5558], [-5.906, 43.5558], [-5.906, 43.5566], [-5.9036, 43.5566], [-5.9036, 43.5583], [-5.9061, 43.5583], [-5.9061, 43.558], [-5.9084, 43.558], [-5.9084, 43.5558]]] }; +const fireUnion = { centerid, shape, when: Date('2018-05-02T16:11:04.617Z') }; + +describe('activeFireUnion insert', () => { + it('should insert fire union', () => { + // Remove previous test register + const alreadyInserted = ActiveFiresUnion.findOne({ centerid }); + if (alreadyInserted) { + ActiveFiresUnion.remove(alreadyInserted._id); + } + + const insertedId = ActiveFiresUnion.insert(fireUnion); + ActiveFiresUnion.schema.validate(fireUnion); + + const inserted = ActiveFiresUnion.findOne(insertedId); + delete inserted._id; + chai.expect(fireUnion).to.deep.equal(inserted); + ActiveFiresUnion.remove(insertedId); + chai.expect(ActiveFiresUnion.find({ _id: inserted }).count()).equal(0); + }); +});