fix: refs for db provider and few utils

This commit is contained in:
Lakhan Samani
2023-07-31 16:42:11 +05:30
parent 146561bacb
commit 15a4be5431
82 changed files with 394 additions and 533 deletions

View File

@@ -15,7 +15,7 @@ import (
)
// AddEmailTemplate to add EmailTemplate
func (p *provider) AddEmailTemplate(ctx context.Context, emailTemplate models.EmailTemplate) (*model.EmailTemplate, error) {
func (p *provider) AddEmailTemplate(ctx context.Context, emailTemplate *models.EmailTemplate) (*model.EmailTemplate, error) {
if emailTemplate.ID == "" {
emailTemplate.ID = uuid.New().String()
@@ -37,7 +37,7 @@ func (p *provider) AddEmailTemplate(ctx context.Context, emailTemplate models.Em
}
// UpdateEmailTemplate to update EmailTemplate
func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate models.EmailTemplate) (*model.EmailTemplate, error) {
func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate *models.EmailTemplate) (*model.EmailTemplate, error) {
bytes, err := json.Marshal(emailTemplate)
if err != nil {
return nil, err
@@ -67,7 +67,7 @@ func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate models
}
// ListEmailTemplates to list EmailTemplate
func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagination) (*model.EmailTemplates, error) {
func (p *provider) ListEmailTemplate(ctx context.Context, pagination *model.Pagination) (*model.EmailTemplates, error) {
emailTemplates := []*model.EmailTemplate{}
paginationClone := pagination
total, err := p.GetTotalDocs(ctx, models.Collections.EmailTemplate)
@@ -88,7 +88,7 @@ func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagin
}
for queryResult.Next() {
emailTemplate := models.EmailTemplate{}
var emailTemplate *models.EmailTemplate
err := queryResult.Row(&emailTemplate)
if err != nil {
log.Fatal(err)
@@ -102,15 +102,14 @@ func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagin
}
return &model.EmailTemplates{
Pagination: &paginationClone,
Pagination: paginationClone,
EmailTemplates: emailTemplates,
}, nil
}
// GetEmailTemplateByID to get EmailTemplate by id
func (p *provider) GetEmailTemplateByID(ctx context.Context, emailTemplateID string) (*model.EmailTemplate, error) {
emailTemplate := models.EmailTemplate{}
var emailTemplate *models.EmailTemplate
query := fmt.Sprintf(`SELECT _id, event_name, subject, design, template, created_at, updated_at FROM %s.%s WHERE _id = $1 LIMIT 1`, p.scopeName, models.Collections.EmailTemplate)
q, err := p.db.Query(query, &gocb.QueryOptions{
Context: ctx,
@@ -132,8 +131,7 @@ func (p *provider) GetEmailTemplateByID(ctx context.Context, emailTemplateID str
// GetEmailTemplateByEventName to get EmailTemplate by event_name
func (p *provider) GetEmailTemplateByEventName(ctx context.Context, eventName string) (*model.EmailTemplate, error) {
emailTemplate := models.EmailTemplate{}
var emailTemplate *models.EmailTemplate
query := fmt.Sprintf("SELECT _id, event_name, subject, design, template, created_at, updated_at FROM %s.%s WHERE event_name=$1 LIMIT 1", p.scopeName, models.Collections.EmailTemplate)
q, err := p.db.Query(query, &gocb.QueryOptions{
Context: ctx,

View File

@@ -11,7 +11,7 @@ import (
)
// AddEnv to save environment information in database
func (p *provider) AddEnv(ctx context.Context, env models.Env) (models.Env, error) {
func (p *provider) AddEnv(ctx context.Context, env *models.Env) (*models.Env, error) {
if env.ID == "" {
env.ID = uuid.New().String()
}
@@ -31,7 +31,7 @@ func (p *provider) AddEnv(ctx context.Context, env models.Env) (models.Env, erro
}
// UpdateEnv to update environment information in database
func (p *provider) UpdateEnv(ctx context.Context, env models.Env) (models.Env, error) {
func (p *provider) UpdateEnv(ctx context.Context, env *models.Env) (*models.Env, error) {
env.UpdatedAt = time.Now().Unix()
env.EncryptionKey = env.Hash
@@ -49,8 +49,8 @@ func (p *provider) UpdateEnv(ctx context.Context, env models.Env) (models.Env, e
}
// GetEnv to get environment information from database
func (p *provider) GetEnv(ctx context.Context) (models.Env, error) {
var env models.Env
func (p *provider) GetEnv(ctx context.Context) (*models.Env, error) {
var env *models.Env
query := fmt.Sprintf("SELECT _id, env, encryption_key, created_at, updated_at FROM %s.%s LIMIT 1", p.scopeName, models.Collections.Env)
q, err := p.db.Query(query, &gocb.QueryOptions{

View File

@@ -10,7 +10,7 @@ import (
)
// AddSession to save session information in database
func (p *provider) AddSession(ctx context.Context, session models.Session) error {
func (p *provider) AddSession(ctx context.Context, session *models.Session) error {
if session.ID == "" {
session.ID = uuid.New().String()
}

View File

@@ -15,7 +15,7 @@ import (
)
// AddUser to save user information in database
func (p *provider) AddUser(ctx context.Context, user models.User) (models.User, error) {
func (p *provider) AddUser(ctx context.Context, user *models.User) (*models.User, error) {
if user.ID == "" {
user.ID = uuid.New().String()
}
@@ -41,7 +41,7 @@ func (p *provider) AddUser(ctx context.Context, user models.User) (models.User,
}
// UpdateUser to update user information in database
func (p *provider) UpdateUser(ctx context.Context, user models.User) (models.User, error) {
func (p *provider) UpdateUser(ctx context.Context, user *models.User) (*models.User, error) {
user.UpdatedAt = time.Now().Unix()
unsertOpt := gocb.UpsertOptions{
Context: ctx,
@@ -54,7 +54,7 @@ func (p *provider) UpdateUser(ctx context.Context, user models.User) (models.Use
}
// DeleteUser to delete user information from database
func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
func (p *provider) DeleteUser(ctx context.Context, user *models.User) error {
removeOpt := gocb.RemoveOptions{
Context: ctx,
}
@@ -66,12 +66,10 @@ func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
}
// ListUsers to get list of users from database
func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (*model.Users, error) {
func (p *provider) ListUsers(ctx context.Context, pagination *model.Pagination) (*model.Users, error) {
users := []*model.User{}
paginationClone := pagination
userQuery := fmt.Sprintf("SELECT _id, email, email_verified_at, `password`, signup_methods, given_name, family_name, middle_name, nickname, birthdate, phone_number, phone_number_verified_at, picture, roles, revoked_timestamp, is_multi_factor_auth_enabled, created_at, updated_at FROM %s.%s ORDER BY id OFFSET $1 LIMIT $2", p.scopeName, models.Collections.User)
queryResult, err := p.db.Query(userQuery, &gocb.QueryOptions{
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
Context: ctx,
@@ -86,7 +84,7 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
}
paginationClone.Total = total
for queryResult.Next() {
var user models.User
var user *models.User
err := queryResult.Row(&user)
if err != nil {
log.Fatal(err)
@@ -97,21 +95,20 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
return nil, err
}
return &model.Users{
Pagination: &paginationClone,
Pagination: paginationClone,
Users: users,
}, nil
}
// GetUserByEmail to get user information from database using email address
func (p *provider) GetUserByEmail(ctx context.Context, email string) (models.User, error) {
user := models.User{}
func (p *provider) GetUserByEmail(ctx context.Context, email string) (*models.User, error) {
var user *models.User
query := fmt.Sprintf("SELECT _id, email, email_verified_at, `password`, signup_methods, given_name, family_name, middle_name, nickname, birthdate, phone_number, phone_number_verified_at, picture, roles, revoked_timestamp, is_multi_factor_auth_enabled, created_at, updated_at FROM %s.%s WHERE email = $1 LIMIT 1", p.scopeName, models.Collections.User)
q, err := p.db.Query(query, &gocb.QueryOptions{
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
Context: ctx,
PositionalParameters: []interface{}{email},
})
if err != nil {
return user, err
}
@@ -119,13 +116,12 @@ func (p *provider) GetUserByEmail(ctx context.Context, email string) (models.Use
if err != nil {
return user, err
}
return user, nil
}
// GetUserByID to get user information from database using user ID
func (p *provider) GetUserByID(ctx context.Context, id string) (models.User, error) {
user := models.User{}
func (p *provider) GetUserByID(ctx context.Context, id string) (*models.User, error) {
var user *models.User
query := fmt.Sprintf("SELECT _id, email, email_verified_at, `password`, signup_methods, given_name, family_name, middle_name, nickname, birthdate, phone_number, phone_number_verified_at, picture, roles, revoked_timestamp, is_multi_factor_auth_enabled, created_at, updated_at FROM %s.%s WHERE _id = $1 LIMIT 1", p.scopeName, models.Collections.User)
q, err := p.db.Query(query, &gocb.QueryOptions{
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
@@ -139,7 +135,6 @@ func (p *provider) GetUserByID(ctx context.Context, id string) (models.User, err
if err != nil {
return user, err
}
return user, nil
}
@@ -174,7 +169,6 @@ func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{},
return err
}
}
return nil
}
@@ -194,6 +188,5 @@ func (p *provider) GetUserByPhoneNumber(ctx context.Context, phoneNumber string)
if err != nil {
return user, err
}
return user, nil
}

View File

@@ -13,7 +13,7 @@ import (
)
// AddVerification to save verification request in database
func (p *provider) AddVerificationRequest(ctx context.Context, verificationRequest models.VerificationRequest) (models.VerificationRequest, error) {
func (p *provider) AddVerificationRequest(ctx context.Context, verificationRequest *models.VerificationRequest) (*models.VerificationRequest, error) {
if verificationRequest.ID == "" {
verificationRequest.ID = uuid.New().String()
}
@@ -33,8 +33,8 @@ func (p *provider) AddVerificationRequest(ctx context.Context, verificationReque
}
// GetVerificationRequestByToken to get verification request from database using token
func (p *provider) GetVerificationRequestByToken(ctx context.Context, token string) (models.VerificationRequest, error) {
verificationRequest := models.VerificationRequest{}
func (p *provider) GetVerificationRequestByToken(ctx context.Context, token string) (*models.VerificationRequest, error) {
var verificationRequest *models.VerificationRequest
params := make(map[string]interface{}, 1)
params["token"] = token
query := fmt.Sprintf("SELECT _id, token, identifier, expires_at, email, nonce, redirect_uri, created_at, updated_at FROM %s.%s WHERE token=$1 LIMIT 1", p.scopeName, models.Collections.VerificationRequest)
@@ -57,7 +57,7 @@ func (p *provider) GetVerificationRequestByToken(ctx context.Context, token stri
}
// GetVerificationRequestByEmail to get verification request by email from database
func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email string, identifier string) (models.VerificationRequest, error) {
func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email string, identifier string) (*models.VerificationRequest, error) {
query := fmt.Sprintf("SELECT _id, identifier, token, expires_at, email, nonce, redirect_uri, created_at, updated_at FROM %s.%s WHERE email=$1 AND identifier=$2 LIMIT 1", p.scopeName, models.Collections.VerificationRequest)
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
@@ -65,14 +65,11 @@ func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email stri
PositionalParameters: []interface{}{email, identifier},
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
})
verificationRequest := models.VerificationRequest{}
if err != nil {
return verificationRequest, err
return nil, err
}
var verificationRequest *models.VerificationRequest
err = queryResult.One(&verificationRequest)
if err != nil {
return verificationRequest, err
}
@@ -80,7 +77,7 @@ func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email stri
}
// ListVerificationRequests to get list of verification requests from database
func (p *provider) ListVerificationRequests(ctx context.Context, pagination model.Pagination) (*model.VerificationRequests, error) {
func (p *provider) ListVerificationRequests(ctx context.Context, pagination *model.Pagination) (*model.VerificationRequests, error) {
var verificationRequests []*model.VerificationRequest
paginationClone := pagination
total, err := p.GetTotalDocs(ctx, models.Collections.VerificationRequest)
@@ -98,7 +95,7 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination mode
return nil, err
}
for queryResult.Next() {
var verificationRequest models.VerificationRequest
var verificationRequest *models.VerificationRequest
err := queryResult.Row(&verificationRequest)
if err != nil {
log.Fatal(err)
@@ -111,12 +108,12 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination mode
}
return &model.VerificationRequests{
VerificationRequests: verificationRequests,
Pagination: &paginationClone,
Pagination: paginationClone,
}, nil
}
// DeleteVerificationRequest to delete verification request from database
func (p *provider) DeleteVerificationRequest(ctx context.Context, verificationRequest models.VerificationRequest) error {
func (p *provider) DeleteVerificationRequest(ctx context.Context, verificationRequest *models.VerificationRequest) error {
removeOpt := gocb.RemoveOptions{
Context: ctx,
}

View File

@@ -15,7 +15,7 @@ import (
)
// AddWebhook to add webhook
func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*model.Webhook, error) {
func (p *provider) AddWebhook(ctx context.Context, webhook *models.Webhook) (*model.Webhook, error) {
if webhook.ID == "" {
webhook.ID = uuid.New().String()
}
@@ -35,7 +35,7 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
}
// UpdateWebhook to update webhook
func (p *provider) UpdateWebhook(ctx context.Context, webhook models.Webhook) (*model.Webhook, error) {
func (p *provider) UpdateWebhook(ctx context.Context, webhook *models.Webhook) (*model.Webhook, error) {
webhook.UpdatedAt = time.Now().Unix()
// Event is changed
if !strings.Contains(webhook.EventName, "-") {
@@ -68,7 +68,7 @@ func (p *provider) UpdateWebhook(ctx context.Context, webhook models.Webhook) (*
}
// ListWebhooks to list webhook
func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination) (*model.Webhooks, error) {
func (p *provider) ListWebhook(ctx context.Context, pagination *model.Pagination) (*model.Webhooks, error) {
webhooks := []*model.Webhook{}
paginationClone := pagination
params := make(map[string]interface{}, 1)
@@ -89,7 +89,7 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
return nil, err
}
for queryResult.Next() {
var webhook models.Webhook
var webhook *models.Webhook
err := queryResult.Row(&webhook)
if err != nil {
log.Fatal(err)
@@ -100,14 +100,14 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
return nil, err
}
return &model.Webhooks{
Pagination: &paginationClone,
Pagination: paginationClone,
Webhooks: webhooks,
}, nil
}
// GetWebhookByID to get webhook by id
func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model.Webhook, error) {
var webhook models.Webhook
var webhook *models.Webhook
params := make(map[string]interface{}, 1)
params["_id"] = webhookID
query := fmt.Sprintf(`SELECT _id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s.%s WHERE _id=$_id LIMIT 1`, p.scopeName, models.Collections.Webhook)
@@ -141,7 +141,7 @@ func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string)
}
webhooks := []*model.Webhook{}
for queryResult.Next() {
var webhook models.Webhook
var webhook *models.Webhook
err := queryResult.Row(&webhook)
if err != nil {
log.Fatal(err)

View File

@@ -13,7 +13,7 @@ import (
)
// AddWebhookLog to add webhook log
func (p *provider) AddWebhookLog(ctx context.Context, webhookLog models.WebhookLog) (*model.WebhookLog, error) {
func (p *provider) AddWebhookLog(ctx context.Context, webhookLog *models.WebhookLog) (*model.WebhookLog, error) {
if webhookLog.ID == "" {
webhookLog.ID = uuid.New().String()
}
@@ -34,7 +34,7 @@ func (p *provider) AddWebhookLog(ctx context.Context, webhookLog models.WebhookL
}
// ListWebhookLogs to list webhook logs
func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Pagination, webhookID string) (*model.WebhookLogs, error) {
func (p *provider) ListWebhookLogs(ctx context.Context, pagination *model.Pagination, webhookID string) (*model.WebhookLogs, error) {
var query string
var err error
@@ -66,7 +66,7 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Paginat
return nil, err
}
for queryResult.Next() {
var webhookLog models.WebhookLog
var webhookLog *models.WebhookLog
err := queryResult.Row(&webhookLog)
if err != nil {
log.Fatal(err)
@@ -79,7 +79,7 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Paginat
}
return &model.WebhookLogs{
Pagination: &paginationClone,
Pagination: paginationClone,
WebhookLogs: webhookLogs,
}, nil
}