todos-contra-el-fuego-web/imports/api/Subscriptions/methods.js
vjrj 637a749e8d meteor3: REST API async migration COMPLETE — smoke green on Meteor 3.1 + Mongo 7
All 12 Flutter REST endpoints byte-identical to the 1.6.1.1 baseline, running on
Meteor 3.1 against dockerized MongoDB 7. The 3.x jump works end-to-end.

- Rest.js: every endpoint action -> async; all Mongo calls -> *Async
  (findOneAsync/countAsync/fetchAsync/upsertAsync/removeAsync).
- Helpers to async: countRealFires (accepts array|cursor, forEachAsync->for-of),
  firesUnion (fetchAsync), fireFromHash/findOrCreateFire (await, dropped Fibers
  Promise.await), subscriptionsInsert/subscriptionsRemove, upsertFalsePositive,
  falsePositives.insert, subscriptions.update method, countFiresInRegions.
- getFires: countAsync() on a $near cursor throws on the mongodb 6 driver
  ($near not allowed in the aggregation countDocuments uses) -> fetch once and
  use array length for total.
- Route order: json-routes 3.0 matches in registration order, so
  mobile/subscriptions/all/:token/:mobileToken is now registered BEFORE
  :token/:mobileToken/:subsId (else GET .../all/x/y hit the delete-only route).
2026-07-14 06:21:35 +02:00

88 lines
2.4 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import Subscriptions from './Subscriptions';
import rateLimit from '../../modules/rate-limit';
function geo(doc) {
return {
type: 'Point',
coordinates: [doc.location.lon, doc.location.lat]
};
}
export async function subscriptionsInsert(doc, userId, type) {
check(doc, {
_id: Match.Maybe(Meteor.Collection.ObjectID),
location: Match.ObjectIncluding({ lat: Number, lon: Number }),
distance: Number
});
const newDoc = {
owner: userId,
type,
geo: geo(doc),
...doc
};
// console.log(newDoc);
const already = await Subscriptions.findOneAsync(newDoc);
if (already) {
throw new Meteor.Error('on-already-subscribed', 'The user is already subscribed to this area');
}
try {
return await Subscriptions.insertAsync(newDoc);
} catch (exception) {
// console.error(exception);
throw new Meteor.Error('500', exception);
}
}
export async function subscriptionsRemove(subscriptionId) {
check(subscriptionId, Meteor.Collection.ObjectID);
try {
return await Subscriptions.removeAsync(subscriptionId);
} catch (exception) {
console.error(exception);
throw new Meteor.Error('500', exception);
}
}
Meteor.methods({
'subscriptions.insert': function subscriptionInsertMethod(doc) {
check(doc, {
location: Match.ObjectIncluding({ lat: Number, lon: Number }),
distance: Number
});
return subscriptionsInsert(doc, this.userId, 'web');
},
'subscriptions.update': async function subscriptionsUpdate(doc) {
check(doc, {
_id: Meteor.Collection.ObjectID,
location: Match.ObjectIncluding({ lat: Number, lon: Number }),
distance: Number
});
try {
const dup = doc;
const subscriptionId = doc._id;
dup.geo = geo(doc);
await Subscriptions.updateAsync(subscriptionId, { $set: dup });
return subscriptionId; // Return _id so we can redirect to subscription after update.
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
'subscriptions.remove': function subscriptionsRemoveMethod(subscriptionId) {
check(subscriptionId, Meteor.Collection.ObjectID);
return subscriptionsRemove(subscriptionId);
}
});
rateLimit({
methods: [
'subscriptions.insert',
'subscriptions.update',
'subscriptions.remove'
],
limit: 5,
timeRange: 1000
});