Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk.api",
"version": "1.1.32",
"version": "1.1.33",
"main": "index.ts",
"license": "UNLICENSED",
"scripts": {
Expand Down Expand Up @@ -44,8 +44,6 @@
"@types/debug": "^4.1.5",
"@types/escape-html": "^1.0.0",
"@types/graphql-upload": "^8.0.11",
"@types/lodash.clonedeep": "^4.5.9",
"@types/lodash.mergewith": "^4.6.9",
"@types/jsonwebtoken": "^8.3.5",
"@types/lodash.clonedeep": "^4.5.9",
"@types/lodash.mergewith": "^4.6.9",
Expand Down
211 changes: 194 additions & 17 deletions src/models/eventsFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Factory = require('./modelFactory');
const mongo = require('../mongo');
const Event = require('../models/event');
const { ObjectID } = require('mongodb');
const { composeFullRepetitionEvent } = require('../utils/merge');

/**
* @typedef {Object} RecentEventSchema
Expand Down Expand Up @@ -287,6 +288,7 @@ class EventsFactory extends Factory {
if (result && result.events) {
result.events.forEach(event => {
event.projectId = this.projectId;
event.firstAppearanceTimestamp = event.timestamp;
});
}

Expand Down Expand Up @@ -391,23 +393,55 @@ class EventsFactory extends Factory {
/**
* Returns Event repetitions
*
* @param {string|ObjectID} eventId - Event's id
* @param {string|ObjectID} eventId - Event's id (may be repetition id)
* @param {Number} limit - count limitations
* @param {Number} skip - selection offset
* @param {Number} cursor - selection offset
Comment thread
e11sy marked this conversation as resolved.
Outdated
*
* @return {EventRepetitionSchema[]}
*
* @todo move to Repetitions(?) model
*/
async getEventRepetitions(eventId, limit = 10, skip = 0) {
async getEventRepetitions(eventId, limit = 10, cursor = null) {
limit = this.validateLimit(limit);
skip = this.validateSkip(skip);

cursor = cursor ? new ObjectID(cursor) : null;

const result = {
repetitions: [],
cursor: null,
};

/**
* Get original event
* @type {EventSchema}
*/
const eventOriginal = await this.findById(eventId);
let eventOriginal = await this.getCollection(this.TYPES.EVENTS)
.findOne({
_id: new ObjectID(eventId),
});

/**
* If event is not found, try to find it as repetition
*/
if (!eventOriginal) {
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
.findOne({
_id: new ObjectID(eventId),
});

if (!repetition) {
return result;
}

eventOriginal = await this.getCollection(this.TYPES.EVENTS)
.findOne({
groupHash: repetition.groupHash,
});
}

if (!eventOriginal) {
return result;
}

/**
* Collect repetitions
Expand All @@ -416,13 +450,28 @@ class EventsFactory extends Factory {
const repetitions = await this.getCollection(this.TYPES.REPETITIONS)
.find({
groupHash: eventOriginal.groupHash,
_id: cursor ? { $lte: cursor } : { $exists: true },
})
.sort({ _id: -1 })
.limit(limit)
.skip(skip)
.limit(limit + 1)
.toArray();

const isLastPortion = repetitions.length < limit && skip === 0;
if (repetitions.length === limit + 1) {
result.cursor = repetitions.pop()._id;
}

for (const repetition of repetitions) {
result.repetitions.push({
...eventOriginal,
_id: repetition._id,
payload: composeFullRepetitionEvent(eventOriginal, repetition).payload,
timestamp: repetition.timestamp,
firstAppearanceTimestamp: eventOriginal.timestamp,
projectId: this.projectId,
});
}

const isLastPortion = result.cursor === null;

/**
* For last portion:
Expand All @@ -434,16 +483,15 @@ class EventsFactory extends Factory {
* @type {EventRepetitionSchema}
*/
const firstRepetition = {
_id: eventOriginal._id,
payload: eventOriginal.payload,
groupHash: eventOriginal.groupHash,
timestamp: eventOriginal.timestamp,
...eventOriginal,
firstAppearanceTimestamp: eventOriginal.timestamp,
projectId: this.projectId,
};

repetitions.push(firstRepetition);
result.repetitions.push(firstRepetition);
}

return repetitions;
return result;
}

/**
Expand All @@ -455,10 +503,43 @@ class EventsFactory extends Factory {
* @todo move to Repetitions(?) model
*/
async getEventRepetition(repetitionId) {
return this.getCollection(this.TYPES.REPETITIONS)
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
.findOne({
_id: ObjectID(repetitionId),
});

if (!repetition) {
/**
* If repetition is not found, it can mean that client is trying to get original event
*/
const event = await this.findById(repetitionId);

if (!event) {
return null;
}

return {
...event,
firstAppearanceTimestamp: event.timestamp,
};
Comment thread
e11sy marked this conversation as resolved.
Outdated
}

const originalEvent = await this.getCollection(this.TYPES.EVENTS)
.findOne({
groupHash: repetition.groupHash,
});

if (!originalEvent) {
return null;
}

return {
...originalEvent,
_id: repetition._id,
payload: composeFullRepetitionEvent(originalEvent, repetition).payload,
timestamp: repetition.timestamp,
firstAppearanceTimestamp: originalEvent.timestamp,
};
}

/**
Expand All @@ -483,7 +564,33 @@ class EventsFactory extends Factory {
* @returns {Release|null}
*/
async getEventRelease(eventId) {
const eventOriginal = await this.findById(eventId);
let eventOriginal = await this.getCollection(this.TYPES.EVENTS)
.findOne({
_id: new ObjectID(eventId),
});

/**
* If event is not found, try to find it as repetition
*/
if (!eventOriginal) {
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
.findOne({
_id: new ObjectID(eventId),
});

if (!repetition) {
return null;
}

eventOriginal = await this.getCollection(this.TYPES.EVENTS)
.findOne({
groupHash: repetition.groupHash,
});

if (!eventOriginal) {
return null;
}
}
Comment thread
e11sy marked this conversation as resolved.
Outdated

const release = await mongo.databases.events.collection(this.TYPES.RELEASES).findOne({
release: eventOriginal.payload.release,
Expand All @@ -502,6 +609,34 @@ class EventsFactory extends Factory {
* @return {Promise<void>}
*/
async visitEvent(eventId, userId) {
let event = await this.getCollection(this.TYPES.EVENTS)
.findOne({
_id: new ObjectID(eventId),
});

/**
* If event is not found, try to find it as repetition
*/
if (!event) {
Comment thread
e11sy marked this conversation as resolved.
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
.findOne({
_id: new ObjectID(eventId),
});

if (!repetition) {
return null;
}

event = await this.getCollection(this.TYPES.EVENTS)
.findOne({
groupHash: repetition.groupHash,
});

if (!event) {
return null;
}
}

return this.getCollection(this.TYPES.EVENTS)
.updateOne(
{ _id: new ObjectID(eventId) },
Expand All @@ -521,7 +656,27 @@ class EventsFactory extends Factory {
const collection = this.getCollection(this.TYPES.EVENTS);
const query = { _id: new ObjectID(eventId) };

const event = await collection.findOne(query);
let event = await collection.findOne(query);

/**
* If event is not found, try to find it as repetition
*/
if (!event) {
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
.findOne({
_id: new ObjectID(eventId),
});

if (repetition) {
event = await this.getCollection(this.TYPES.EVENTS)
.findOne({
groupHash: repetition.groupHash,
});

query._id = new ObjectID(event._id);
}
}

const markKey = `marks.${mark}`;

let update;
Expand Down Expand Up @@ -572,6 +727,28 @@ class EventsFactory extends Factory {
async updateAssignee(eventId, assignee) {
const collection = this.getCollection(this.TYPES.EVENTS);
const query = { _id: new ObjectID(eventId) };

let event = await collection.findOne(query);

/**
* If event is not found, try to find it as repetition
Comment thread
e11sy marked this conversation as resolved.
Outdated
*/
if (!event) {
const repetition = await this.getCollection(this.TYPES.REPETITIONS)
.findOne({
_id: new ObjectID(eventId),
});

if (repetition) {
event = await this.getCollection(this.TYPES.EVENTS)
.findOne({
groupHash: repetition.groupHash,
});

query._id = new ObjectID(event._id);
}
}

const update = {
$set: { assignee: assignee },
};
Expand Down
23 changes: 3 additions & 20 deletions src/resolvers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ module.exports = {
},
},
Event: {
/**
* Returns Event with concrete repetition
*
* @param {string} eventId - id of Event of which repetition requested
* @param {string} projectId - projectId of Event of which repetition requested
* @param {string|null} [repetitionId] - if not specified, last repetition will returned
* @return {Promise<EventRepetitionSchema>}
*/
async repetition({ id: eventId, projectId }, { id: repetitionId }) {
const factory = new EventsFactory(projectId);

if (!repetitionId) {
return factory.getEventLastRepetition(eventId);
}

return factory.getEventRepetition(repetitionId);
},

/**
* Returns repetitions list of the event
Expand All @@ -43,14 +26,14 @@ module.exports = {
* @param {String} eventId
* @param {String} projectId
* @param {Number} limit
* @param {Number} skip
* @param {Number} cursor
*
* @return {EventRepetitionSchema[]}
*/
async repetitions({ _id: eventId, projectId }, { limit, skip }) {
async repetitions({ _id: eventId, projectId }, { limit, cursor }) {
const factory = new EventsFactory(projectId);

return factory.getEventRepetitions(eventId, limit, skip);
return factory.getEventRepetitions(eventId, limit, cursor);
},

/**
Expand Down
10 changes: 5 additions & 5 deletions src/resolvers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,17 @@ module.exports = {
*
* @returns {Event}
*/
async event(project, { id: eventId }) {
async event(project, { id: repetitionId }) {
const factory = new EventsFactory(project._id);
const event = await factory.findById(eventId);
const repetition = await factory.getEventRepetition(repetitionId);

if (!event) {
if (!repetition) {
return null;
}

event.projectId = project._id;
repetition.projectId = project._id;

return event;
return repetition;
},

/**
Expand Down
Loading
Loading