@admin-layout/timetracker-module-server 1.0.3-alpha.2 → 1.0.3-alpha.31

Sign up to get free protection for your applications and to get access to all the features.
package/lib/index.js CHANGED
@@ -1369,7 +1369,7 @@ exports.schema = schema;
1369
1369
 
1370
1370
  "use strict";
1371
1371
  __webpack_require__.r(__webpack_exports__);
1372
- /* harmony default export */ __webpack_exports__["default"] = ("type TimeRecord {\n id: String\n startTime: DateTime\n endTime: DateTime\n taskId: String\n taskName: String\n description: String\n tags: [String]\n isBillable: Boolean\n projectId: String\n clientId: String\n userId: String\n orgId: String\n timesheetId: String\n editable: Boolean\n isPause: Boolean\n isTimeout: Boolean\n pauseStatus: [PauseStatus]\n}\n\ntype PauseStatus {\n pausedTime: DateTime\n restartTime: DateTime\n}\n\ninput PauseStatus_Input {\n pausedTime: DateTime\n restartTime: DateTime\n}\n\ninput TimeRecordRequest {\n userId: String\n startTime: DateTime\n endTime: DateTime\n taskName: String\n description: String\n tags: [String]\n isBillable: Boolean\n projectId: String\n clientId: String\n timesheetId: String\n isPause: Boolean\n isTimeout: Boolean\n pauseStatus: [PauseStatus_Input]\n}\n\n\"\"\"\n Subscription event for timer\n\"\"\"\nenum TimeRecordPubSubEvents {\n TimeRecordCreated\n TimeRecordUpdated\n TimeRecordStopped\n TimeRecordDeleted\n TimeRecordConfigurationUpdate\n}\n\ntype SubscriptionTimeRecord {\n mutation: TimeRecordPubSubEvents\n timeRecord: TimeRecord\n}\n\nextend type Query {\n getTimeRecords(userId: String): [TimeRecord] @addAccountContext\n getDurationTimeRecords(startTime: DateTime, endTime: DateTime, userId: String): [TimeRecord] @addAccountContext\n getPlayingTimeRecord: TimeRecord @addAccountContext\n}\n\nextend type Mutation {\n createTimeRecord(request: TimeRecordRequest): String @addAccountContext\n updateTimeRecord(recordId: String!, request: TimeRecordRequest): Boolean @addAccountContext\n removeTimeRecord(recordId: String!): Boolean @addAccountContext\n removeDurationTimeRecords(startTime: DateTime, endTime: DateTime, projectId: String): Boolean @addAccountContext\n}\n\nextend type Subscription {\n SubscribeToTimeTracker(orgName: String, userId: String): SubscriptionTimeRecord\n}\n");
1372
+ /* harmony default export */ __webpack_exports__["default"] = ("#type TimeRecords {\n# orgId: String\n# userId: String\n# timeRecords: [TimeRecord]\n#}\n\ntype TimeRecord {\n id: ID\n startTime: DateTime\n endTime: DateTime\n taskId: String\n taskName: String\n description: String\n tags: [String]\n isBillable: Boolean\n projectId: String\n clientId: String\n userId: String\n orgId: String\n timesheetId: String\n editable: Boolean\n isPause: Boolean\n isTimeout: Boolean\n pauseStatus: [PauseStatus]\n}\n\ntype PauseStatus {\n pausedTime: DateTime\n restartTime: DateTime\n}\n\ninput PauseStatus_Input {\n pausedTime: DateTime\n restartTime: DateTime\n}\n\ninput TimeRecordRequest {\n userId: String\n startTime: DateTime\n endTime: DateTime\n taskName: String\n description: String\n tags: [String]\n isBillable: Boolean\n projectId: String\n clientId: String\n timesheetId: String\n isPause: Boolean\n isTimeout: Boolean\n pauseStatus: [PauseStatus_Input]\n}\n\n\"\"\"\n Subscription event for timer\n\"\"\"\nenum TimeRecordPubSubEvents {\n TimeRecordCreated\n TimeRecordUpdated\n TimeRecordStopped\n TimeRecordDeleted\n TimeRecordConfigurationUpdate\n}\n\ntype SubscriptionTimeRecord {\n mutation: TimeRecordPubSubEvents\n timeRecord: TimeRecord\n}\n\nextend type Query {\n getTimeRecords(userId: String): [TimeRecord] @addAccountContext\n getDurationTimeRecords(startTime: DateTime, endTime: DateTime, userId: String): [TimeRecord] @addAccountContext\n getPlayingTimeRecord: TimeRecord @addAccountContext\n}\n\nextend type Mutation {\n createTimeRecord(request: TimeRecordRequest): String @addAccountContext\n updateTimeRecord(recordId: String!, request: TimeRecordRequest): Boolean @addAccountContext\n removeTimeRecord(recordId: String!): Boolean @addAccountContext\n removeDurationTimeRecords(startTime: DateTime, endTime: DateTime, projectId: String): Boolean @addAccountContext\n}\n\nextend type Subscription {\n SubscribeToTimeTracker(orgName: String, userId: String): SubscriptionTimeRecord\n}\n");
1373
1373
 
1374
1374
  /***/ }),
1375
1375
 
@@ -1382,7 +1382,7 @@ __webpack_require__.r(__webpack_exports__);
1382
1382
 
1383
1383
  "use strict";
1384
1384
  __webpack_require__.r(__webpack_exports__);
1385
- /* harmony default export */ __webpack_exports__["default"] = ("# scalar DateTime\nenum TimesheetState {\n OPEN\n APPROVED_PENDING\n APPROVED\n APPROVED_FINALIZED # --- which means no going back\n DENYED\n SUBMITTED\n DENYED_FINALIZED # --- which means no going back\n}\n\ntype Timesheet {\n id: ID\n userId: String\n startDate: DateTime\n endDate: DateTime\n state: TimesheetState\n submittedOn: DateTime\n approvedOn: DateTime\n approvedBy: String\n updatedBy: String # ---> userId\n updatedOn: DateTime\n}\n\ninput TimesheetCreateRequest {\n userId: String\n startDate: DateTime\n endDate: DateTime\n state: TimesheetState\n submittedOn: DateTime\n approvedOn: DateTime\n approvedBy: String\n updatedBy: String # ---> userId\n updatedOn: DateTime\n}\n\ntype TimesheetResponse {\n id: ID\n startDate: DateTime\n endDate: DateTime\n state: TimesheetState\n submittedOn: DateTime\n approvedOn: DateTime\n approvedBy: String\n updatedBy: String # ---> userId\n updatedOn: DateTime\n userId: String\n orgId: String\n totalDuration: Int\n}\n\nextend type Query {\n getTimesheets(userId: String, withTotalHours: Boolean): [TimesheetResponse] @addAccountContext\n getDurationTimesheets(start: DateTime, end: DateTime): [TimesheetResponse] @addAccountContext\n}\n\nextend type Mutation {\n createTimesheet(request: TimesheetCreateRequest): Boolean @addAccountContext\n updateTimesheet(sheetId: String, request: TimesheetCreateRequest): Boolean @addAccountContext\n removeTimesheet(sheetId: String): Boolean @addAccountContext\n updateTimesheetStatus(sheetId: String, state: TimesheetState): Boolean @addAccountContext\n}\n");
1385
+ /* harmony default export */ __webpack_exports__["default"] = ("# scalar DateTime\nenum TimesheetState {\n OPEN\n APPROVED_PENDING\n APPROVED\n APPROVED_FINALIZED # --- which means no going back\n DENYED\n SUBMITTED\n DENYED_FINALIZED # --- which means no going back\n}\n\ntype Timesheets {\n orgId: String\n userId: String\n timesheets: [Timesheet]\n}\n\ntype Timesheet {\n id: ID\n userId: String\n startDate: DateTime\n endDate: DateTime\n state: TimesheetState\n submittedOn: DateTime\n approvedOn: DateTime\n approvedBy: String\n updatedBy: String # ---> userId\n updatedOn: DateTime\n timeRecordsId: [String]\n}\n\ninput TimesheetCreateRequest {\n userId: String\n startDate: DateTime\n endDate: DateTime\n state: TimesheetState\n submittedOn: DateTime\n approvedOn: DateTime\n approvedBy: String\n updatedBy: String # ---> userId\n updatedOn: DateTime\n timeRecordsId: [String]\n}\n\ntype TimesheetResponse {\n id: ID\n startDate: DateTime\n endDate: DateTime\n state: TimesheetState\n submittedOn: DateTime\n approvedOn: DateTime\n approvedBy: String\n updatedBy: String # ---> userId\n updatedOn: DateTime\n userId: String\n orgId: String\n totalDuration: Int\n}\n\nextend type Query {\n getTimesheets(userId: String, withTotalHours: Boolean): [TimesheetResponse] @addAccountContext\n getDurationTimesheets(start: DateTime, end: DateTime): [TimesheetResponse] @addAccountContext\n}\n\nextend type Mutation {\n createTimesheet(request: TimesheetCreateRequest): Boolean @addAccountContext\n updateTimesheet(sheetId: String, request: TimesheetCreateRequest): Boolean @addAccountContext\n removeTimesheet(sheetId: String): Boolean @addAccountContext\n updateTimesheetStatus(sheetId: String, state: TimesheetState): Boolean @addAccountContext\n}\n");
1386
1386
 
1387
1387
  /***/ }),
1388
1388
 
@@ -1469,13 +1469,12 @@ let TimeRecordService = class TimeRecordService {
1469
1469
  }
1470
1470
  async createTimeRecord(userId, orgId, request) {
1471
1471
  const data = await this.timeRecordRepository.createTimeRecord(userId, orgId, request);
1472
- const timeRecord = data === null || data === void 0 ? void 0 : data.timeRecords[0];
1473
- if (timeRecord) {
1472
+ if (data) {
1474
1473
  const record = {
1475
1474
  orgName: data.orgId,
1476
- userId: timeRecord.userId,
1475
+ userId: data.userId,
1477
1476
  mutation: "TimeRecordCreated" /* TimeRecordCreated */,
1478
- timeRecord,
1477
+ timeRecord: data,
1479
1478
  };
1480
1479
  this.pubsub.publish("TimeRecordCreated" /* TimeRecordCreated */, { SubscribeToTimeTracker: record });
1481
1480
  }
@@ -1483,13 +1482,12 @@ let TimeRecordService = class TimeRecordService {
1483
1482
  }
1484
1483
  async updateTimeRecord(userId, orgId, recordId, request) {
1485
1484
  const data = await this.timeRecordRepository.updateTimeRecord(userId, orgId, recordId, request);
1486
- const timeRecord = data === null || data === void 0 ? void 0 : data.timeRecords[0];
1487
- if (timeRecord) {
1485
+ if (data) {
1488
1486
  const record = {
1489
1487
  orgName: data.orgId,
1490
- userId: timeRecord.userId,
1488
+ userId: data.userId,
1491
1489
  mutation: "TimeRecordUpdated" /* TimeRecordUpdated */,
1492
- timeRecord,
1490
+ timeRecord: data,
1493
1491
  };
1494
1492
  this.pubsub.publish("TimeRecordUpdated" /* TimeRecordUpdated */, { SubscribeToTimeTracker: record });
1495
1493
  }
@@ -1501,7 +1499,7 @@ let TimeRecordService = class TimeRecordService {
1501
1499
  orgName: data.orgId,
1502
1500
  userId: data.userId,
1503
1501
  mutation: "TimeRecordDeleted" /* TimeRecordDeleted */,
1504
- timeRecord: data === null || data === void 0 ? void 0 : data.timeRecords[0],
1502
+ timeRecord: data,
1505
1503
  };
1506
1504
  this.pubsub.publish("TimeRecordDeleted" /* TimeRecordDeleted */, { SubscribeToTimeTracker: record });
1507
1505
  return true;
@@ -1573,7 +1571,7 @@ let TimesheetService = class TimesheetService {
1573
1571
  const timeRecords = await this.timeRecordRepository.getTimeRecords(orgId, userId);
1574
1572
  return timesheets.map((timesheet) => {
1575
1573
  const sheetTotalDuration = timeRecords
1576
- .filter((tr) => tr.userId === timesheet.userId && tr.startTime > timesheet.startDate && tr.endTime < timesheet.endDate)
1574
+ .filter((tr) => { var _a; return (_a = timesheet.timeRecordsId) === null || _a === void 0 ? void 0 : _a.includes(tr._id.toString()); })
1577
1575
  .reduce((duration, tr) => duration + Math.floor((moment(tr.endTime).valueOf() - moment(tr.startTime).valueOf()) / 1000), 0);
1578
1576
  return {
1579
1577
  id: timesheet.id,
@@ -1699,6 +1697,103 @@ TimesheetService = __decorate([
1699
1697
  exports.TimesheetService = TimesheetService;
1700
1698
 
1701
1699
 
1700
+ /***/ }),
1701
+
1702
+ /***/ "./src/store/models/timerecords-model.ts":
1703
+ /*!***********************************************!*\
1704
+ !*** ./src/store/models/timerecords-model.ts ***!
1705
+ \***********************************************/
1706
+ /*! no static exports found */
1707
+ /***/ (function(module, exports, __webpack_require__) {
1708
+
1709
+ "use strict";
1710
+
1711
+ Object.defineProperty(exports, "__esModule", { value: true });
1712
+ exports.TimeRecordsModelFunc = void 0;
1713
+ const mongoose_1 = __webpack_require__(/*! mongoose */ "mongoose");
1714
+ const PauseStatusSchema = new mongoose_1.Schema({
1715
+ pausedTime: { type: Date },
1716
+ restartTime: { type: Date },
1717
+ });
1718
+ // ===> TimeRecord
1719
+ const TimeRecordSchema = new mongoose_1.Schema({
1720
+ orgId: { type: String, index: true },
1721
+ userId: { type: String, index: true },
1722
+ startTime: { type: Date },
1723
+ endTime: { type: Date, default: null },
1724
+ taskName: { type: String },
1725
+ description: { type: String },
1726
+ taskId: { type: String },
1727
+ tags: { type: [String] },
1728
+ isBillable: { type: Boolean },
1729
+ projectId: { type: String },
1730
+ timesheetId: { type: String },
1731
+ isPause: { type: Boolean, default: false },
1732
+ isTimeout: { type: Boolean, default: false },
1733
+ pauseStatus: { type: [PauseStatusSchema] }
1734
+ });
1735
+ TimeRecordSchema.index({ endTime: 1, startTime: 1, userId: 1 });
1736
+ TimeRecordSchema.virtual('id').get(function () {
1737
+ return this._id.toHexString();
1738
+ });
1739
+ TimeRecordSchema.set('toJSON', {
1740
+ virtuals: true,
1741
+ });
1742
+ TimeRecordSchema.set('toObject', { virtuals: true });
1743
+ exports.TimeRecordsModelFunc = (db) => db.model('timerecords', TimeRecordSchema);
1744
+
1745
+
1746
+ /***/ }),
1747
+
1748
+ /***/ "./src/store/models/timesheets-model.ts":
1749
+ /*!**********************************************!*\
1750
+ !*** ./src/store/models/timesheets-model.ts ***!
1751
+ \**********************************************/
1752
+ /*! no static exports found */
1753
+ /***/ (function(module, exports, __webpack_require__) {
1754
+
1755
+ "use strict";
1756
+
1757
+ Object.defineProperty(exports, "__esModule", { value: true });
1758
+ exports.TimesheetsModelFunc = void 0;
1759
+ const mongoose_1 = __webpack_require__(/*! mongoose */ "mongoose");
1760
+ var TimesheetState;
1761
+ (function (TimesheetState) {
1762
+ TimesheetState[TimesheetState["OPEN"] = 0] = "OPEN";
1763
+ TimesheetState[TimesheetState["APPROVED_PENDING"] = 1] = "APPROVED_PENDING";
1764
+ TimesheetState[TimesheetState["APPROVED"] = 2] = "APPROVED";
1765
+ TimesheetState[TimesheetState["APPROVED_FINALIZED"] = 3] = "APPROVED_FINALIZED";
1766
+ TimesheetState[TimesheetState["DENYED"] = 4] = "DENYED";
1767
+ TimesheetState[TimesheetState["SUBMITTED"] = 5] = "SUBMITTED";
1768
+ TimesheetState[TimesheetState["DENYED_FINALIZED"] = 6] = "DENYED_FINALIZED";
1769
+ })(TimesheetState || (TimesheetState = {}));
1770
+ // ===> Timesheet
1771
+ const TimesheetSchema = new mongoose_1.Schema({
1772
+ userId: { type: String, index: true },
1773
+ startDate: { type: Date },
1774
+ endDate: { type: Date },
1775
+ state: { type: TimesheetState },
1776
+ submittedOn: { type: Date },
1777
+ approvedOn: { type: Date },
1778
+ approvedBy: { type: String },
1779
+ updatedBy: { type: String },
1780
+ updatedOn: { type: Date },
1781
+ timeRecordsId: { type: [String] },
1782
+ });
1783
+ const TimesheetsSchema = new mongoose_1.Schema({
1784
+ orgId: { type: String, index: true },
1785
+ timesheets: [TimesheetSchema],
1786
+ });
1787
+ TimesheetsSchema.virtual('id').get(function () {
1788
+ return this._id.toHexString();
1789
+ });
1790
+ TimesheetsSchema.set('toJSON', {
1791
+ virtuals: true,
1792
+ });
1793
+ TimesheetsSchema.set('toObject', { virtuals: true });
1794
+ exports.TimesheetsModelFunc = (db) => db.model('timesheets', TimesheetsSchema);
1795
+
1796
+
1702
1797
  /***/ }),
1703
1798
 
1704
1799
  /***/ "./src/store/models/timetracker-model.ts":
@@ -1764,6 +1859,7 @@ const TimesheetSchema = new mongoose_1.Schema({
1764
1859
  approvedBy: { type: String },
1765
1860
  updatedBy: { type: String },
1766
1861
  updatedOn: { type: Date },
1862
+ timeRecordsId: { type: [String] },
1767
1863
  });
1768
1864
  const TimeTrackerSchema = new mongoose_1.Schema({
1769
1865
  orgId: { type: String, index: true },
@@ -1835,82 +1931,60 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1835
1931
  Object.defineProperty(exports, "__esModule", { value: true });
1836
1932
  exports.TimeRecordRepository = void 0;
1837
1933
  const inversify_1 = __webpack_require__(/*! inversify */ "inversify");
1838
- const _ = __webpack_require__(/*! lodash */ "lodash");
1839
- const timetracker_model_1 = __webpack_require__(/*! ../models/timetracker-model */ "./src/store/models/timetracker-model.ts");
1934
+ const timerecords_model_1 = __webpack_require__(/*! ../models/timerecords-model */ "./src/store/models/timerecords-model.ts");
1840
1935
  let TimeRecordRepository = class TimeRecordRepository {
1841
1936
  constructor(db, logger) {
1842
1937
  this.logger = logger.child({ className: 'TimeRecordRepository' });
1843
- this.timeTrackerModel = timetracker_model_1.TimeTrackerModelFunc(db);
1938
+ this.timeRecordsModel = timerecords_model_1.TimeRecordsModelFunc(db);
1844
1939
  }
1845
1940
  async getTimeRecords(orgId, userId, from, until) {
1846
1941
  if (!userId) {
1847
1942
  userId = /.*/;
1848
1943
  }
1849
1944
  let match = {
1850
- 'timeRecords.userId': userId,
1945
+ 'userId': userId,
1851
1946
  };
1852
1947
  if (from) {
1853
- match = Object.assign(Object.assign({}, match), { 'timeRecords.startTime': { $gte: from } });
1948
+ match = Object.assign(Object.assign({}, match), { 'startTime': { $gte: from } });
1854
1949
  }
1855
1950
  if (until) {
1856
- match = Object.assign(Object.assign({}, match), { 'timeRecords.endTime': { $lte: until } });
1951
+ match = Object.assign(Object.assign({}, match), { 'endTime': { $lte: until } });
1857
1952
  }
1858
- const result = await this.timeTrackerModel.aggregate([
1953
+ const result = await this.timeRecordsModel.aggregate([
1859
1954
  {
1860
1955
  $match: {
1861
1956
  orgId,
1862
1957
  },
1863
1958
  },
1864
- {
1865
- $unwind: '$timeRecords',
1866
- },
1867
1959
  {
1868
1960
  $match: Object.assign({}, match),
1869
1961
  },
1870
- {
1871
- $group: {
1872
- _id: '$orgId',
1873
- timeRecords: {
1874
- $push: '$timeRecords',
1875
- },
1876
- },
1877
- },
1878
- {
1879
- $project: {
1880
- orgId: 1,
1881
- timeRecords: 1,
1882
- },
1883
- },
1884
1962
  ]);
1885
1963
  if (result.length === 0) {
1886
1964
  return [];
1887
1965
  }
1888
- return result[0].timeRecords;
1966
+ return result;
1889
1967
  }
1890
1968
  async getOrganizationTimeRecords(orgId) {
1891
- const trackDoc = await this.timeTrackerModel.findOne({ orgId });
1892
- if (trackDoc && trackDoc.timeRecords) {
1893
- return trackDoc.timeRecords;
1969
+ const trackDoc = await this.timeRecordsModel.find({ orgId });
1970
+ if (trackDoc) {
1971
+ return trackDoc;
1894
1972
  }
1895
1973
  return [];
1896
1974
  }
1897
1975
  async getPlayingTimeRecord(userId, orgId) {
1898
- const trackDoc = await this.timeTrackerModel.findOne({ orgId });
1976
+ const trackDoc = await this.timeRecordsModel.find({ orgId });
1899
1977
  if (trackDoc) {
1900
1978
  let res;
1901
- if (trackDoc.timeRecords)
1902
- res = trackDoc.timeRecords.find((tr) => tr.userId === userId && tr.endTime === null);
1979
+ if (trackDoc)
1980
+ res = trackDoc.find((tr) => tr.userId === userId && tr.endTime === null);
1903
1981
  return res;
1904
1982
  }
1905
1983
  return null;
1906
1984
  }
1907
1985
  async createTimeRecord(userId, orgId, request) {
1908
1986
  try {
1909
- const response = await this.timeTrackerModel.findOneAndUpdate({ orgId }, { orgId, $push: { timeRecords: request } }, {
1910
- upsert: true,
1911
- new: true,
1912
- projection: { userId, orgId, timeRecords: { $elemMatch: { startTime: request.startTime } } },
1913
- });
1987
+ const response = await this.timeRecordsModel.create(Object.assign(Object.assign({}, request), { orgId, userId }));
1914
1988
  return response.toObject();
1915
1989
  }
1916
1990
  catch (err) {
@@ -1921,21 +1995,12 @@ let TimeRecordRepository = class TimeRecordRepository {
1921
1995
  try {
1922
1996
  if (recordId === null || recordId === undefined)
1923
1997
  throw new Error('TimeRecord id not specified!');
1924
- let setRequest = {};
1925
- _.forIn(request, (value, key) => {
1926
- setRequest = Object.assign(Object.assign({}, setRequest), { [`timeRecords.$.${key}`]: value });
1927
- });
1928
- const response = await this.timeTrackerModel.findOneAndUpdate({ orgId, timeRecords: { $elemMatch: { _id: recordId } } }, {
1929
- $set: Object.assign({}, setRequest),
1998
+ const response = await this.timeRecordsModel.findOneAndUpdate({ orgId, _id: recordId }, {
1999
+ $set: Object.assign({}, request),
1930
2000
  }, {
1931
2001
  new: true,
1932
- projection: {
1933
- userId,
1934
- orgId,
1935
- timeRecords: { $elemMatch: { _id: recordId } },
1936
- },
1937
2002
  });
1938
- return response.toObject();
2003
+ return response === null || response === void 0 ? void 0 : response.toObject();
1939
2004
  }
1940
2005
  catch (err) {
1941
2006
  throw new Error(err.message);
@@ -1943,15 +2008,11 @@ let TimeRecordRepository = class TimeRecordRepository {
1943
2008
  }
1944
2009
  async removeTimeRecord(userId, orgId, recordId) {
1945
2010
  try {
1946
- const result = await this.timeTrackerModel.update({
1947
- orgId,
1948
- }, {
1949
- $pull: { timeRecords: { _id: recordId } },
1950
- });
1951
- if (result.nModified !== 1) {
2011
+ const result = await this.timeRecordsModel.remove({ userId, orgId, _id: recordId });
2012
+ if (result.n !== 1) {
1952
2013
  throw new Error('TimeRecord is not modified');
1953
2014
  }
1954
- return { userId, orgId, timeRecords: [{ _id: recordId }] };
2015
+ return { userId, orgId, _id: recordId };
1955
2016
  }
1956
2017
  catch (err) {
1957
2018
  throw new Error(err.message);
@@ -1959,14 +2020,12 @@ let TimeRecordRepository = class TimeRecordRepository {
1959
2020
  }
1960
2021
  async removeDurationTimeRecords(userId, orgId, startTime, endTime, projectId) {
1961
2022
  try {
1962
- const trackerDoc = await this.timeTrackerModel.find({ orgId });
2023
+ const trackerDoc = await this.timeRecordsModel.find({ orgId });
1963
2024
  if (trackerDoc && trackerDoc.length > 0) {
1964
- const timeRecords = trackerDoc[0].timeRecords.filter((tr) => tr.startTime < startTime || tr.startTime > endTime || tr.projectId !== projectId);
1965
- await this.timeTrackerModel.update({
1966
- orgId,
1967
- }, {
1968
- timeRecords,
1969
- });
2025
+ const timeRecords = trackerDoc.filter((tr) => tr.startTime < startTime || tr.startTime > endTime || tr.projectId !== projectId);
2026
+ for (const record of timeRecords) {
2027
+ await this.timeRecordsModel.remove({ _id: record._id });
2028
+ }
1970
2029
  return true;
1971
2030
  }
1972
2031
  return false;
@@ -1977,15 +2036,15 @@ let TimeRecordRepository = class TimeRecordRepository {
1977
2036
  }
1978
2037
  async approveTimeRecords(orgId, sheetId, startDate, endDate) {
1979
2038
  try {
1980
- await this.timeTrackerModel.updateMany({
2039
+ await this.timeRecordsModel.updateMany({
1981
2040
  orgId,
1982
- }, { $set: { 'timeRecords.$[el].timesheetId': sheetId } }, {
2041
+ }, { $set: { 'timesheetId': sheetId } }, {
1983
2042
  multi: true,
1984
2043
  arrayFilters: [
1985
2044
  {
1986
- 'el.startTime': { $gte: startDate },
1987
- 'el.endTime': { $lte: endDate },
1988
- 'el.timesheetId': null,
2045
+ 'startTime': { $gte: startDate },
2046
+ 'endTime': { $lte: endDate },
2047
+ 'timesheetId': null,
1989
2048
  },
1990
2049
  ],
1991
2050
  });
@@ -1997,13 +2056,13 @@ let TimeRecordRepository = class TimeRecordRepository {
1997
2056
  }
1998
2057
  async disapproveTimeRecords(orgId, sheetId) {
1999
2058
  try {
2000
- await this.timeTrackerModel.updateMany({
2059
+ await this.timeRecordsModel.updateMany({
2001
2060
  orgId,
2002
- }, { $set: { 'timeRecords.$[el].timesheetId': null } }, {
2061
+ }, { $set: { 'timesheetId': null } }, {
2003
2062
  multi: true,
2004
2063
  arrayFilters: [
2005
2064
  {
2006
- 'el.timesheetId': sheetId,
2065
+ 'timesheetId': sheetId,
2007
2066
  },
2008
2067
  ],
2009
2068
  });
@@ -2045,16 +2104,17 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
2045
2104
  Object.defineProperty(exports, "__esModule", { value: true });
2046
2105
  exports.TimesheetRepository = void 0;
2047
2106
  const inversify_1 = __webpack_require__(/*! inversify */ "inversify");
2107
+ const _ = __webpack_require__(/*! lodash */ "lodash");
2048
2108
  const core_1 = __webpack_require__(/*! @common-stack/core */ "@common-stack/core");
2049
- const timetracker_model_1 = __webpack_require__(/*! ../models/timetracker-model */ "./src/store/models/timetracker-model.ts");
2109
+ const timesheets_model_1 = __webpack_require__(/*! ../models/timesheets-model */ "./src/store/models/timesheets-model.ts");
2050
2110
  let TimesheetRepository = class TimesheetRepository {
2051
2111
  constructor(db, logger, broker) {
2052
2112
  this.broker = broker;
2053
2113
  this.logger = logger.child({ className: 'TimesheetRepository' });
2054
- this.timeTrackerModel = timetracker_model_1.TimeTrackerModelFunc(db);
2114
+ this.timesheetsModel = timesheets_model_1.TimesheetsModelFunc(db);
2055
2115
  }
2056
2116
  async getOrganizationTimesheets(orgId) {
2057
- const trackDoc = await this.timeTrackerModel.findOne({ orgId });
2117
+ const trackDoc = await this.timesheetsModel.findOne({ orgId });
2058
2118
  if (trackDoc && trackDoc.timesheets) {
2059
2119
  return trackDoc.timesheets;
2060
2120
  }
@@ -2066,7 +2126,7 @@ let TimesheetRepository = class TimesheetRepository {
2066
2126
  }
2067
2127
  async createTimesheet(userId, orgId, request) {
2068
2128
  try {
2069
- const response = await this.timeTrackerModel.update({ orgId }, { $push: { timesheets: request } }, { upsert: true });
2129
+ const response = await this.timesheetsModel.update({ orgId }, { $push: { timesheets: request } }, { upsert: true });
2070
2130
  return true;
2071
2131
  }
2072
2132
  catch (err) {
@@ -2075,7 +2135,11 @@ let TimesheetRepository = class TimesheetRepository {
2075
2135
  }
2076
2136
  async updateTimesheet(orgId, sheetId, request) {
2077
2137
  try {
2078
- const response = await this.timeTrackerModel.update({ orgId, timesheets: { $elemMatch: { _id: sheetId } } }, { $set: { 'timesheets.$': request } });
2138
+ let setRequest = {};
2139
+ _.forIn(request, (value, key) => {
2140
+ setRequest = Object.assign(Object.assign({}, setRequest), { [`timesheets.$.${key}`]: value });
2141
+ });
2142
+ const response = await this.timesheetsModel.update({ orgId, timesheets: { $elemMatch: { _id: sheetId } } }, { $set: Object.assign({}, setRequest) });
2079
2143
  return response;
2080
2144
  }
2081
2145
  catch (err) {
@@ -2084,7 +2148,7 @@ let TimesheetRepository = class TimesheetRepository {
2084
2148
  }
2085
2149
  async updateTimesheetStatus(orgId, sheetId, state) {
2086
2150
  try {
2087
- const response = await this.timeTrackerModel.update({
2151
+ const response = await this.timesheetsModel.update({
2088
2152
  orgId,
2089
2153
  $elemMatch: { 'timesheets.id': sheetId },
2090
2154
  }, {
@@ -2100,7 +2164,7 @@ let TimesheetRepository = class TimesheetRepository {
2100
2164
  }
2101
2165
  async removeTimesheet(userId, orgId, sheetId) {
2102
2166
  try {
2103
- await this.timeTrackerModel.update({
2167
+ await this.timesheetsModel.update({
2104
2168
  orgId,
2105
2169
  }, {
2106
2170
  $pull: { timesheets: { _id: sheetId } },