You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.7 KiB

  1. //
  2. // mdlWorkOrder.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 1/24/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Foundation
  10. import FluentKit
  11. final class mdlWorkOrder: Model, Content {
  12. static let schema = "workOrder"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "number") var number: String?
  15. @Field(key: "meterReading") var meterreading: Decimal?
  16. @Field(key: "specialInstructions") var specialinstructions: String?
  17. @Field(key: "scheduledStartDate") var scheduledstartdate: Date?
  18. @Field(key: "scheduledEndDate") var scheduledenddate: Date?
  19. @Field(key: "startDate") var startdate: Date?
  20. @Field(key: "endDate") var enddate: Date?
  21. @Field(key: "approvedDate") var approveddate: Date?
  22. @OptionalParent(key: "customerId") var customer: mdlCustomer?
  23. @OptionalParent(key: "equipmentId") var equipment: mdlEquipment?
  24. @OptionalParent(key: "statusId") var status: mdlStatus?
  25. @OptionalParent(key: "priorityId") var priority: mdlPriority?
  26. @OptionalParent(key: "technicianUserId") var technician: mdlUser?
  27. @OptionalParent(key: "reasonId") var reason: mdlReason?
  28. @OptionalParent(key: "incompleteReasonId") var incompletereason: mdlIncompleteReason?
  29. @OptionalParent(key: "fuelId") var fuel: mdlFuel?
  30. @OptionalParent(key: "workOrderFlowId") var workorderflow: mdlWorkOrderFlow?
  31. @Siblings(through: mdlWorkOrderAttachment.self, from: \.$workorder, to: \.$equipment) var attachments: [mdlEquipment]
  32. @Children(for: \.$workorder) var signatures: [mdlSignature]
  33. @Children(for: \.$workorder) var notes: [mdlNote]
  34. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  35. @Field(key: "createUserId") var createuserid: Int?
  36. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  37. @Field(key: "updateUserId") var updateuserid: Int?
  38. init() { }
  39. init(id: Int? = nil
  40. , number: String? = nil
  41. , meterreading: Decimal? = nil
  42. , specialinstructions: String? = nil
  43. , scheduledstartdate: Date? = nil
  44. , scheduledenddate: Date? = nil
  45. , startdate: Date? = nil
  46. , enddate: Date? = nil
  47. , approveddate: Date? = nil
  48. , customerid: Int? = nil
  49. , equipmentid: Int? = nil
  50. , statusid: Int? = nil
  51. , priorityid: Int? = nil
  52. , technicianuserid: Int? = nil
  53. , reasonid: Int? = nil
  54. , incompletereasonid: Int? = nil
  55. , fuelid: Int? = nil
  56. , workorderflowid: Int? = nil
  57. , createuserid: Int? = nil
  58. , updateuserid: Int? = nil
  59. ) {
  60. self.id = id
  61. self.number = number
  62. self.meterreading = meterreading
  63. self.specialinstructions = specialinstructions
  64. self.scheduledenddate = scheduledstartdate
  65. self.scheduledenddate = scheduledenddate
  66. self.startdate = startdate
  67. self.enddate = enddate
  68. self.approveddate = approveddate
  69. self.$customer.$id.value = customerid!
  70. self.$equipment.$id.value = equipmentid!
  71. self.$status.$id.value = statusid!
  72. self.$priority.$id.value = priorityid!
  73. self.$technician.$id.value = technicianuserid!
  74. self.$reason.$id.value = reasonid!
  75. self.$incompletereason.$id.value = incompletereasonid!
  76. self.$fuel.$id.value = fuelid!
  77. self.$workorderflow.$id.value = workorderflowid!
  78. self.createuserid = createuserid ?? -1
  79. self.updateuserid = updateuserid ?? -1
  80. }
  81. }
  82. extension mdlWorkOrder{
  83. struct Create: AsyncMigration {
  84. func prepare(on database: Database) async throws {
  85. try await database.schema("workOrder")
  86. .field("id", .int, .identifier(auto: true))
  87. .field("number", .string)
  88. .field("meterReading", .float)
  89. .field("specialInstructions", .string)
  90. .field("scheduledStartDate", .datetime)
  91. .field("scheduledEndDate", .datetime)
  92. .field("startDate", .datetime)
  93. .field("endDate", .datetime)
  94. .field("approvedDate", .datetime)
  95. .field("customerId", .int)
  96. .field("equipmentId", .int)
  97. .field("statusId", .int)
  98. .field("priorityId", .int)
  99. .field("technicianUserId", .int)
  100. .field("reasonId", .int)
  101. .field("incompleteReasonId", .int)
  102. .field("fuelId", .int)
  103. .field("createDate", .datetime)
  104. .field("createUserId", .int)
  105. .field("updateDate", .datetime)
  106. .field("updateUserId", .int)
  107. .create()
  108. }
  109. func revert(on database: Database) async throws {
  110. try await database.schema("workOrder").delete()
  111. }
  112. }
  113. struct Mod1: AsyncMigration {
  114. func prepare(on database: Database) async throws {
  115. try await database.schema("workOrder")
  116. .field("workOrderFlowId", .int, .references("workOrderFlow", "id"))
  117. .update()
  118. }
  119. func revert(on database: Database) async throws {
  120. try await database.schema("workOrder")
  121. .deleteField("workOrderFlowId")
  122. .update()
  123. }
  124. }
  125. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlWorkOrder>{
  126. let query = mdlWorkOrder.query(on: req.db)
  127. .with(\.$customer)
  128. .with(\.$equipment)
  129. .with(\.$status)
  130. .with(\.$priority)
  131. .with(\.$technician)
  132. .with(\.$reason)
  133. .with(\.$incompletereason)
  134. .with(\.$fuel)
  135. .with(\.$workorderflow)
  136. .with(\.$attachments)
  137. .with(\.$signatures)
  138. .with(\.$notes)
  139. if let x = req.parameters.get("x"){
  140. let decryptedString = try CustomCrypto.DecryptString(input: x)
  141. let array = decryptedString.components(separatedBy: ":")
  142. ///This call is the standard get by id call
  143. if (array.count == 1){
  144. let id = Int(array[0])!
  145. return query.filter(\.$id == id)
  146. }
  147. ///Optional else if if there are multiple parameters in the call
  148. ///You must include an else for each call that has parameters
  149. ///Need to check the endpoint to get the right filter statements
  150. ///else if(x.count == 2){
  151. /// let username = x[0]
  152. /// let password = x[1]
  153. /// return query.filter(\.$username == username).filter(\.$password == password)
  154. ///}
  155. else{
  156. throw Abort(.badRequest)
  157. }
  158. }
  159. else{
  160. return query
  161. }
  162. }
  163. }