2023-10-30 20:06:18 -04:00
|
|
|
package models
|
|
|
|
|
2023-11-01 22:40:50 -04:00
|
|
|
import (
|
2023-11-04 18:52:41 -04:00
|
|
|
"net/http"
|
2023-11-01 22:40:50 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2023-11-04 18:52:41 -04:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2023-11-01 22:40:50 -04:00
|
|
|
)
|
2023-10-30 20:06:18 -04:00
|
|
|
|
|
|
|
const VENDOR_ACCOUNT_TYPE = "vendor_account"
|
|
|
|
|
2023-11-02 19:23:07 -04:00
|
|
|
const (
|
|
|
|
YOUTUBE_VENDOR_NAME = "YouTube"
|
|
|
|
PCO_VENDOR_NAME = "PCO"
|
|
|
|
)
|
|
|
|
|
2023-10-30 20:06:18 -04:00
|
|
|
|
|
|
|
type VendorAccount struct {
|
2023-11-01 22:40:50 -04:00
|
|
|
*CommonFields `bson:"obj_info"`
|
2023-11-03 01:01:33 -04:00
|
|
|
Id primitive.ObjectID `bson:"_id,omitempty"`
|
2023-11-01 22:40:50 -04:00
|
|
|
UserId primitive.ObjectID `bson:"user_id,omitempty"`
|
|
|
|
Secret string `bson:"secret,omitempty"`
|
|
|
|
OauthCredentials *OauthCredential `bson:"ouath_credentials,omitempty"`
|
2023-11-02 19:23:07 -04:00
|
|
|
Name string `bson:"name"`
|
2023-11-01 22:40:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (va *VendorAccount) MongoId() primitive.ObjectID {
|
2023-11-03 01:01:33 -04:00
|
|
|
if va.Id.IsZero() {
|
2023-11-01 22:40:50 -04:00
|
|
|
now := time.Now()
|
2023-11-03 01:01:33 -04:00
|
|
|
va.Id = primitive.NewObjectIDFromTimestamp(now)
|
2023-11-01 22:40:50 -04:00
|
|
|
}
|
|
|
|
|
2023-11-03 01:01:33 -04:00
|
|
|
return va.Id
|
2023-11-01 22:40:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (va *VendorAccount) UpdateObjectInfo() {
|
|
|
|
now := time.Now()
|
|
|
|
if va.CommonFields == nil {
|
|
|
|
va.CommonFields = new(CommonFields)
|
|
|
|
va.EntityType = VENDOR_ACCOUNT_TYPE
|
|
|
|
va.CreatedAt = now
|
|
|
|
}
|
|
|
|
va.UpdatedAt = now
|
2023-10-30 20:06:18 -04:00
|
|
|
}
|
2023-11-04 18:52:41 -04:00
|
|
|
|
|
|
|
func (va *VendorAccount) MakeRequest(req *http.Request, db *mongo.Client) error {
|
|
|
|
if va.OauthCredentials.ExpiresAt.Before(time.Now()) {
|
|
|
|
va.OauthCredentials.RefreshAccessToken(va.Name)
|
|
|
|
}
|
|
|
|
}
|