From c894945b5112dcfa31e95a7a9f5b280487bc182e Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Wed, 2 Feb 2022 19:37:09 -0800 Subject: [PATCH] update/contact-home-phone --- controllers/updateController.go | 63 ++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/controllers/updateController.go b/controllers/updateController.go index e9201ed..9c40054 100644 --- a/controllers/updateController.go +++ b/controllers/updateController.go @@ -859,7 +859,68 @@ func UpdateContactAddress(c *fiber.Ctx) error { }) } -func UpdateContactPhone(c *fiber.Ctx) error { +func UpdateContactHomePhone(c *fiber.Ctx) error { + var data map[string]string + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + + if err := c.BodyParser(&data); err != nil { + cancel() + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + + // Ensure Authenticated admin sent request + if !AuthAdmin(c) { + cancel() + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "success": false, + "message": "Unauthorized: only an admin can perform this action", + }) + } + + // Check id of contact and new priority number is included + if data["_id"] == "" || data["newnumber"] == "" { + cancel() + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "success": false, + "message": "missing required fields", + }) + } + + update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) + update := bson.M{ + "$set": bson.M{ + "homephone": data["newnumber"], + "updated_at": update_time, + }, + } + + result, updateErr := contactCollection.UpdateOne( + ctx, + bson.M{"_id": data["_id"]}, + update, + ) + if updateErr != nil { + cancel() + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "contact could not be updated", + "error": updateErr, + }) + } + defer cancel() + + return c.Status(fiber.StatusOK).JSON(fiber.Map{ + "success": true, + "message": "successfully updated contact", + "result": result, + }) +} + +func UpdateContactWorkPhone(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented",