|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/codescalers/cloud4students/internal" |
| 9 | + "github.com/codescalers/cloud4students/middlewares" |
| 10 | + "github.com/codescalers/cloud4students/models" |
| 11 | + "github.com/pkg/errors" |
| 12 | +) |
| 13 | + |
| 14 | +func (a *App) logInvoicePayment(userID, currency string, invoiceTotal float64, paymentDetails models.PaymentDetails) error { |
| 15 | + event := models.AuditEvent{ |
| 16 | + UserID: userID, |
| 17 | + Action: "pay_invoice", |
| 18 | + Role: "User", |
| 19 | + Timestamp: time.Now(), |
| 20 | + Metadata: fmt.Sprintf( |
| 21 | + "Invoice %v with value: %v %v is paid using: {balance: %v, vouchers: %v, card:%v}", |
| 22 | + paymentDetails.InvoiceID, invoiceTotal, currency, |
| 23 | + paymentDetails.Balance, paymentDetails.VoucherBalance, paymentDetails.Card, |
| 24 | + ), |
| 25 | + } |
| 26 | + |
| 27 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 28 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 29 | + } |
| 30 | + |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +func (a *App) logInvoicePDFUpdate(req *http.Request, invoiceID int) error { |
| 35 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 36 | + |
| 37 | + event := models.AuditEvent{ |
| 38 | + UserID: userID, |
| 39 | + Action: "update_invoice", |
| 40 | + Role: "User", |
| 41 | + Timestamp: time.Now(), |
| 42 | + Metadata: fmt.Sprintf("Invoice %v pdf data is updated", invoiceID), |
| 43 | + } |
| 44 | + |
| 45 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 46 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 47 | + } |
| 48 | + |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (a *App) logInvoiceDownload(req *http.Request, invoiceID int) error { |
| 53 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 54 | + |
| 55 | + event := models.AuditEvent{ |
| 56 | + UserID: userID, |
| 57 | + Action: "download_invoice", |
| 58 | + Role: "User", |
| 59 | + Timestamp: time.Now(), |
| 60 | + Metadata: fmt.Sprintf("Invoice %v is downloaded", invoiceID), |
| 61 | + } |
| 62 | + |
| 63 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 64 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (a *App) logEmailSent(req *http.Request, targetUserID, subject string) error { |
| 71 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 72 | + |
| 73 | + event := models.AuditEvent{ |
| 74 | + UserID: userID, |
| 75 | + Action: "send_email", |
| 76 | + Role: "Admin", |
| 77 | + Timestamp: time.Now(), |
| 78 | + Metadata: fmt.Sprintf("An email is sent to: %v, with subject: %v", targetUserID, subject), |
| 79 | + } |
| 80 | + |
| 81 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 82 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (a *App) logAnnouncementCreate(req *http.Request, subject string) error { |
| 89 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 90 | + |
| 91 | + event := models.AuditEvent{ |
| 92 | + UserID: userID, |
| 93 | + Action: "create_announcement", |
| 94 | + Role: "Admin", |
| 95 | + Timestamp: time.Now(), |
| 96 | + Metadata: fmt.Sprintf("An announcement is created with subject: %v", subject), |
| 97 | + } |
| 98 | + |
| 99 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 100 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (a *App) logAdminSet(req *http.Request, adminID string, admin bool) error { |
| 107 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 108 | + |
| 109 | + metaData := fmt.Sprintf("A new admin %v is added", adminID) |
| 110 | + if !admin { |
| 111 | + metaData = fmt.Sprintf("An admin %v is removed", adminID) |
| 112 | + } |
| 113 | + |
| 114 | + event := models.AuditEvent{ |
| 115 | + UserID: userID, |
| 116 | + Action: "set_admin", |
| 117 | + Role: "Admin", |
| 118 | + Timestamp: time.Now(), |
| 119 | + Metadata: metaData, |
| 120 | + } |
| 121 | + |
| 122 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 123 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func (a *App) logNextLaunchUpdate(req *http.Request, on bool) error { |
| 130 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 131 | + |
| 132 | + event := models.AuditEvent{ |
| 133 | + UserID: userID, |
| 134 | + Action: "update_next_launch", |
| 135 | + Role: "Admin", |
| 136 | + Timestamp: time.Now(), |
| 137 | + Metadata: fmt.Sprintf("Next launch value is updated to: %v", on), |
| 138 | + } |
| 139 | + |
| 140 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 141 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +func (a *App) logMaintenanceUpdate(req *http.Request, on bool) error { |
| 148 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 149 | + |
| 150 | + event := models.AuditEvent{ |
| 151 | + UserID: userID, |
| 152 | + Action: "update_maintenance", |
| 153 | + Role: "Admin", |
| 154 | + Timestamp: time.Now(), |
| 155 | + Metadata: fmt.Sprintf("Maintenance value is updated to: %v", on), |
| 156 | + } |
| 157 | + |
| 158 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 159 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func (a *App) logAllDeploymentsDelete(req *http.Request) error { |
| 166 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 167 | + |
| 168 | + event := models.AuditEvent{ |
| 169 | + UserID: userID, |
| 170 | + Action: "delete_all_deployments", |
| 171 | + Role: "Admin", |
| 172 | + Timestamp: time.Now(), |
| 173 | + Metadata: "All virtual machines are deleted", |
| 174 | + } |
| 175 | + |
| 176 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 177 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 178 | + } |
| 179 | + |
| 180 | + return nil |
| 181 | +} |
| 182 | + |
| 183 | +func (a *App) logVMsPriceUpdate(req *http.Request, prices internal.Prices) error { |
| 184 | + userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string) |
| 185 | + |
| 186 | + event := models.AuditEvent{ |
| 187 | + UserID: userID, |
| 188 | + Action: "update_vms_prices", |
| 189 | + Role: "Admin", |
| 190 | + Timestamp: time.Now(), |
| 191 | + Metadata: fmt.Sprintf( |
| 192 | + "Virtual machines prices are updated {small: %v, medium: %v, large: %v, public IPs: %v}", |
| 193 | + prices.SmallVM, prices.MediumVM, prices.LargeVM, prices.PublicIP, |
| 194 | + ), |
| 195 | + } |
| 196 | + |
| 197 | + if err := a.db.CreateAuditEvent(&event); err != nil { |
| 198 | + return errors.Wrapf(err, "Failed to log audit event: %s", event.Action) |
| 199 | + } |
| 200 | + |
| 201 | + return nil |
| 202 | +} |
0 commit comments