import { Vessels } from '/imports/api/vessels/vessels.coffee'

import './publish/vessel.coffee'
import './publish/vessel.details.js'
import './publish/vessel.search.coffee'

const PersistenceManager = require('/imports/api/persistence/PersistenceManager.js');

Meteor.methods({
    'vessel.insert': ({}, vessel) => {
        // TODO: verify that user has permissions
        try {
            new PersistenceManager(Vessels).insert(vessel);
            return vessel._id;
        } catch(e) {
            throw new Meteor.Error(e);
        }
    },
    'vessel.update': (vesselId, key, value) => {
        // TODO: verify that user has permissions
        try {
            new PersistenceManager(Vessels).update(vesselId, key, value);
        } catch(e) {
            throw new Meteor.Error(e);
        }
    },
    'vessel.visit.insert': ({ projectId, entityId }, visit) => {
        // TODO: verify that user has permissions

        visit.projectId = projectId; // TODO: verify that is vessel project
        delete visit.userId

        const vessel = Vessels.findOne(entityId);
        let visits = vessel.visits;

        visits.push(visit);

        try {
            new PersistenceManager(Vessels).update(vessel._id, 'visits', visits); // TODO: add push method
            return vessel._id;
        } catch(e) {
            throw new Meteor.Error(e);
        }
    },
    'vessel.visit.getAvailableHarbors': ({ projectId }) => {
        // TODO: verify that user has permissions

        const project = Projects.findOne(projectId, {
            fields: {
                vesselModule: 1,
                harbors: 1
            }
        });

        if (project != null && project.vesselModule) {
            return project.harbors.map(({ _id, name}) => {
                return {
                    key: _id,
                    value: name
                }
            });
        }

        return [];
    }
})

@@ -1916,13 +1916,1684 @@
 %5B%5D;%0A
+    %7D,%0A    'vessel.visit.get': (%7B vesselId, visitId %7D) =%3E %7B%0A        const projectIds = GetGroupsForUser(Meteor.userId(), Permissions.member);%0A        let returnVisit = %7B%7D;%0A%0A        // TODO: only for admin%0A        return %5B%5D;%0A%0A        Projects.find(%7B%0A            _id: %7B%0A                $in: projectIds%0A            %7D%0A        %7D, %7B%0A            fields: %7B%0A                vesselModule: 1,%0A                harbors: 1%0A            %7D%0A        %7D)%0A        .fetch()%0A        .some((project) =%3E %7B%0A            if (project.vesselModule) %7B%0A                returnVisit = Vessels.find(vesselId, %7B%0A                    fields: %7B%0A                        visits: 1%0A                    %7D%0A                %7D)%0A                .fetch()%0A                .map((vessel) =%3E %7B%0A                    if (vessel.visits != null) %7B%0A                        return vessel.visits;%0A                    %7D else %7B%0A                        return %5B%5D;%0A                    %7D%0A                %7D)%0A                .reduce((acc, vessel) =%3E acc.concat(vessel.visits))%0A                .filter((visit) =%3E visit._id == visitId)%0A                .reduce((acc, visit) =%3E visit);%0A%0A                return true;%0A            %7D%0A        %7D);%0A%0A        return returnVisit;%0A    %7D,%0A    'vessel.visit.getLast': (%7B vesselId %7D) =%3E %7B%0A        return Projects.find(%7B%0A            _id: %7B $in: GetGroupsForUser(Meteor.userId(), Permissions.member) %7D%0A        %7D, %7B%0A            fields: %7B vesselModule: 1, harbors: 1 %7D%0A        %7D)%0A        .fetch()%0A        .filter((project) =%3E project.vesselModule)%0A        .reduce(() =%3E Vessels.findOne(vesselId, %7B fields: %7B visits: 1 %7D%7D).visits, %5B%5D)%0A        .sort((visitA, visitB) =%3E visitA.date - visitB.date)%0A        .pop();%0A
     %7D%0A%7D)
 %0A
@@ -3591,9 +3591,10 @@
     %7D%0A%7D)
+;
 %0A

add vessel.visit.get and getLast methods