activity.service.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // src/harvest/harvest.service.ts
  2. import { Injectable } from '@nestjs/common';
  3. import { HarvestActivity } from './activity.schema';
  4. import { CreateActivityDto, UpdateActivityDto } from './activity.dto';
  5. import { ObjectId } from 'mongodb';
  6. import { MongoService } from 'src/services/mongo.service';
  7. @Injectable()
  8. export class ActivityService {
  9. private readonly collectionName = 'activities';
  10. constructor(private readonly mongo: MongoService) { }
  11. /* ------------------------------
  12. * Utility: Convert DTO to Schema
  13. * ------------------------------ */
  14. private dtoToHarvestActivity(dto: CreateActivityDto): HarvestActivity {
  15. return {
  16. name: dto.name,
  17. type: dto.type,
  18. resources: dto.resources.map((r) => ({
  19. ...r,
  20. id: r.id ? new ObjectId(r.id) : new ObjectId(),
  21. })),
  22. duration: dto.duration,
  23. outputs: dto.outputs.map((o) => ({
  24. ...o,
  25. id: o.id ? new ObjectId(o.id) : new ObjectId(),
  26. })),
  27. targets: dto.targets.map((t) => ({
  28. ...t,
  29. id: t.id ? new ObjectId(t.id) : new ObjectId(),
  30. })),
  31. dateStart: new Date(dto.dateStart),
  32. dateEnd: new Date(dto.dateEnd),
  33. };
  34. }
  35. /* ------------------------------
  36. * CRUD OPERATIONS
  37. * ------------------------------ */
  38. async create(dto: CreateActivityDto) {
  39. const activity = this.dtoToHarvestActivity(dto);
  40. return this.mongo.createActivity(activity);
  41. }
  42. async findAll(filter: Record<string, any> = {}) {
  43. return this.mongo.getAllActivities(filter);
  44. }
  45. async findById(id: string) {
  46. return this.mongo.getActivityById(id);
  47. }
  48. async update(id: string, dto: UpdateActivityDto) {
  49. // For partial updates, we only convert provided IDs
  50. const update: any = { ...dto };
  51. if (dto.duration) {
  52. update.duration = {
  53. value: {
  54. quantity: dto.duration.value?.quantity ?? 0,
  55. uom: dto.duration.value?.uom ?? 'hours',
  56. },
  57. };
  58. }
  59. if (dto.resources) {
  60. update.resources = dto.resources.map((r) => ({
  61. ...r,
  62. id: r.id ? new ObjectId(r.id) : new ObjectId(),
  63. }));
  64. }
  65. if (dto.outputs) {
  66. update.outputs = dto.outputs.map((o) => ({
  67. ...o,
  68. id: o.id ? new ObjectId(o.id) : new ObjectId(),
  69. }));
  70. }
  71. if (dto.targets) {
  72. update.targets = dto.targets.map((t) => ({
  73. ...t,
  74. id: t.id ? new ObjectId(t.id) : new ObjectId(),
  75. }));
  76. }
  77. if (dto.dateStart) update.dateStart = new Date(dto.dateStart);
  78. if (dto.dateEnd) update.dateEnd = new Date(dto.dateEnd);
  79. return this.mongo.updateActivity(id, update);
  80. }
  81. async delete(id: string) {
  82. return this.mongo.deleteActivity(id);
  83. }
  84. /* ------------------------------
  85. * Aggregations / Queries
  86. * ------------------------------ */
  87. async getTotalResourceHours() {
  88. return this.mongo.getTotalResourceHours();
  89. }
  90. async getTotalOutputWeight() {
  91. return this.mongo.getTotalOutputWeight();
  92. }
  93. async findByDateRange(start: Date, end: Date) {
  94. return this.mongo.getActivitiesByDateRange(start, end);
  95. }
  96. /* ------------------------------
  97. * Aggregations / Custom Queries
  98. * ------------------------------ */
  99. async getTotalWorkers(filter: { start?: Date; end?: Date; location?: string }) {
  100. return this.mongo.getTotalWorkers(filter);
  101. }
  102. async getTotalOutputs(filter: { start?: Date; end?: Date }) {
  103. return this.mongo.getTotalOutputs(filter);
  104. }
  105. }