From 3049da7bcdb4d18f55859c28c9438205da3bc37a Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Sat, 18 Nov 2023 17:57:31 -0600 Subject: [PATCH] B: add method to find webhooks --- ui/db/pco.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ui/db/pco.go diff --git a/ui/db/pco.go b/ui/db/pco.go new file mode 100644 index 0000000..73bfd56 --- /dev/null +++ b/ui/db/pco.go @@ -0,0 +1,35 @@ +package db + +import ( + "context" + + "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config" + "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +//using userId and event string return PCO Subscriptions saved to the DB +func (db *DB) FindPcoSubscriptionForUser(userId primitive.ObjectID, eventName string) (*models.PcoSubscription, error) { + conf := config.Config() + + opts := options.FindOne() + res := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).FindOne(context.Background(), bson.M{"_id": userId, "obj_info.ent": models.PCO_SUBSCRIPTION_TYPE, "details.name": eventName}, opts) + + if res.Err() != nil { + if res.Err() == mongo.ErrNoDocuments { + return nil, nil + } + return nil, res.Err() + } + + subscription := &models.PcoSubscription{} + err := res.Decode(subscription) + if err != nil { + return nil, err + } + + return subscription, nil +}