Skip to content

Commit

Permalink
Merge branch 'master' into feature/send-suctected-commits
Browse files Browse the repository at this point in the history
  • Loading branch information
talyguryn authored Sep 15, 2021
2 parents befbb85 + 663a27a commit 1b5b5b7
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/resolvers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Validator = require('../utils/validator');
const UserInProject = require('../models/userInProject');
const EventsFactory = require('../models/eventsFactory');
const ProjectToWorkspace = require('../models/projectToWorkspace');
const { dateFromObjectId } = require('../utils/dates');
const ProjectModel = require('../models/project').default;

const EVENTS_GROUP_HASH_INDEX_NAME = 'groupHashUnique';
Expand Down Expand Up @@ -220,6 +221,17 @@ module.exports = {
},
},
Project: {
/**
* Returns project creation date
*
* @param {ProjectDBScheme} project - result of parent resolver
*
* @returns {Date}
*/
creationDate(project) {
return dateFromObjectId(project._id);
},

/**
* Find project's event
*
Expand Down
14 changes: 14 additions & 0 deletions src/resolvers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Validator from '../utils/validator';
import { SenderWorkerTaskType } from '../types/userNotifications';
import { TaskPriorities, emailNotification } from '../utils/emailNotifications';
import isE2E from '../utils/isE2E';
import { dateFromObjectId } from '../utils/dates';
import { UserDBScheme } from 'hawk.types';

/**
* See all types and fields here {@see ../typeDefs/user.graphql}
Expand Down Expand Up @@ -241,4 +243,16 @@ export default {
return true;
},
},
User: {
/**
* Returns user registration date
*
* @param {UserDBScheme} user - result of parent resolver
*
* @returns {Date}
*/
registrationDate(user: UserDBScheme): Date {
return dateFromObjectId(user._id);
},
},
};
12 changes: 12 additions & 0 deletions src/resolvers/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { emailNotification, TaskPriorities } from '../utils/emailNotifications';
import { SenderWorkerTaskType } from '../types/userNotifications';
import ProjectToWorkspace from '../models/projectToWorkspace';
import { Validator } from '../utils/validator';
import { dateFromObjectId } from '../utils/dates';

const { ApolloError, UserInputError, ForbiddenError } = require('apollo-server-express');
const crypto = require('crypto');
Expand Down Expand Up @@ -386,6 +387,17 @@ module.exports = {
workspace: () => ({}),
},
Workspace: {
/**
* Returns workspace creation date
*
* @param {WorkspaceDBScheme} workspace - result of parent resolver
*
* @returns {Date}
*/
creationDate(workspace) {
return dateFromObjectId(workspace._id);
},

/**
* Returns workspace invite hash
* If workspace has not hash this resolver generates it
Expand Down
5 changes: 5 additions & 0 deletions src/typeDefs/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ type Project {
"""
description: String
"""
Date of creating project
"""
creationDate: DateTime!
"""
Project domain
"""
Expand Down
5 changes: 5 additions & 0 deletions src/typeDefs/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default gql`
"""
name: String
"""
Date of registration
"""
registrationDate: DateTime!
"""
User's image
"""
Expand Down
5 changes: 5 additions & 0 deletions src/typeDefs/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export default gql`
"""
description: String
"""
Date of creating workspace
"""
creationDate: DateTime!
"""
Workspace logo image
"""
Expand Down
13 changes: 12 additions & 1 deletion src/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ObjectId } from 'mongodb';

/**
* Return timestamp for UTC midnight of the passed date
*
Expand Down Expand Up @@ -66,4 +68,13 @@ export function getMidnightWithTimezoneOffset(utcOccurrenceTime: number, utcMidn
const localMidnight = getUTCMidnight(localDate);

return localMidnight / milliseconds;
}
}

/**
* Returns date parsed from object id
*
* @param objectId - id of entity for getting date
*/
export function dateFromObjectId(objectId: ObjectId): Date {
return objectId.getTimestamp();
}
22 changes: 22 additions & 0 deletions test/utils/dateFromObjectId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ObjectId } from 'mongodb';
import { dateFromObjectId } from '../../src/utils/dates';

describe('dateFromObjectId', () => {
it('should return correct Date object from Object ID', () => {
/**
* Arrange
*/
const objectId = new ObjectId('612102b24b7a2b00231131b6');
const expectedDate = new Date('2021-08-21T13:42:10.000Z');

/**
* Act
*/
const parsedDate = dateFromObjectId(objectId);

/**
* Assert
*/
expect(parsedDate).toStrictEqual(expectedDate);
});
});

0 comments on commit 1b5b5b7

Please sign in to comment.