2023-11-18 18:57:31 -05:00
|
|
|
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"
|
2023-11-18 21:15:15 -05:00
|
|
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
|
2023-11-18 18:57:31 -05:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-11-18 21:16:44 -05:00
|
|
|
// using userId and event string return PCO Subscriptions saved to the DB
|
2023-11-18 18:57:31 -05:00
|
|
|
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
|
|
|
|
}
|
2023-11-18 21:15:15 -05:00
|
|
|
|
2023-11-18 21:16:44 -05:00
|
|
|
// Okay so learned something here. Interfaces are determined implemented for the type a method is related to.
|
|
|
|
// This function is not implemented for DB it is implemented for *DB and that is important
|
|
|
|
func (db *DB) SaveSubscriptionsForUser(userId primitive.ObjectID, subscriptions ...webhooks.Subscription) error {
|
2023-11-18 21:15:15 -05:00
|
|
|
mods := make([]*models.PcoSubscription, 0, len(subscriptions))
|
|
|
|
for _, sub := range subscriptions {
|
|
|
|
mods = append(mods, &models.PcoSubscription{
|
2023-11-18 21:16:44 -05:00
|
|
|
UserId: userId,
|
|
|
|
Details: &sub,
|
2023-11-18 21:15:15 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return saveModels(db, mods...)
|
|
|
|
}
|