329 lines
8.2 KiB
Go
329 lines
8.2 KiB
Go
package yclients
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"gitea.pena/SQuiz/common/dal/sqlcgen"
|
|
"gitea.pena/SQuiz/common/model"
|
|
)
|
|
|
|
type YclientsRepository struct {
|
|
queries *sqlcgen.Queries
|
|
pool *sql.DB
|
|
}
|
|
|
|
type Deps struct {
|
|
Queries *sqlcgen.Queries
|
|
Pool *sql.DB
|
|
}
|
|
|
|
func NewYclientsRepository(deps Deps) *YclientsRepository {
|
|
return &YclientsRepository{
|
|
queries: deps.Queries,
|
|
pool: deps.Pool,
|
|
}
|
|
}
|
|
|
|
// tokens
|
|
func (r *YclientsRepository) InsertYclientsTokens(ctx context.Context, accountID, accessToken string, salonID int32) (*model.YclientsToken, error) {
|
|
row, err := r.queries.InsertYclientsTokens(ctx, sqlcgen.InsertYclientsTokensParams{
|
|
Accountid: accountID,
|
|
Salonid: salonID,
|
|
Accesstoken: accessToken,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &model.YclientsToken{
|
|
AccountID: row.Accountid,
|
|
SalonID: row.Salonid,
|
|
AccessToken: row.Accesstoken,
|
|
Active: row.Active,
|
|
Expiration: row.Expiration,
|
|
CreatedAt: row.Createdat,
|
|
}, nil
|
|
}
|
|
|
|
func (r *YclientsRepository) UpdateYclientsTokensAccessToken(ctx context.Context, accountID string, salonID int32, yclientsAccessToken string) (*model.YclientsToken, error) {
|
|
row, err := r.queries.UpdateYclientsTokensAccessToken(ctx, sqlcgen.UpdateYclientsTokensAccessTokenParams{
|
|
Accountid: accountID,
|
|
Salonid: salonID,
|
|
Accesstoken: yclientsAccessToken,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &model.YclientsToken{
|
|
AccountID: row.Accountid,
|
|
SalonID: row.Salonid,
|
|
AccessToken: row.Accesstoken,
|
|
Active: row.Active,
|
|
Expiration: row.Expiration,
|
|
CreatedAt: row.Createdat,
|
|
}, nil
|
|
}
|
|
|
|
func (r *YclientsRepository) UpdateYclientsTokensExpiration(ctx context.Context, accountID string, active, expiration bool) error {
|
|
_, err := r.queries.UpdateYclientsTokensExpiration(ctx, sqlcgen.UpdateYclientsTokensExpirationParams{
|
|
Accountid: accountID,
|
|
Active: active,
|
|
Expiration: expiration,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *YclientsRepository) GetYclientsUserToken(ctx context.Context, accountID string) (*model.YclientsToken, error) {
|
|
row, err := r.queries.GetYclientsUserToken(ctx, accountID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &model.YclientsToken{
|
|
AccountID: row.Accountid,
|
|
SalonID: row.Salonid,
|
|
AccessToken: row.Accesstoken,
|
|
Active: row.Active,
|
|
Expiration: row.Expiration,
|
|
CreatedAt: row.Createdat,
|
|
}, err
|
|
}
|
|
|
|
// users
|
|
func (r *YclientsRepository) GetCurrentAccount(ctx context.Context, accountID string) (*model.YclientsAccount, error) {
|
|
row, err := r.queries.GetCurrentYclientsCompany(ctx, accountID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
user := model.YclientsAccount{
|
|
ID: row.ID,
|
|
AccountID: row.Accountid,
|
|
SalonID: row.Salonid,
|
|
Title: row.Title,
|
|
Country: row.Country,
|
|
ShortDecription: row.Shortdecription,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
}
|
|
|
|
_, err = r.GetYclientsUserToken(ctx, accountID)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
user.Stale = true
|
|
return &user, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
user.Stale = false
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
//func (r *YclientsRepository) SoftDeleteAccount(ctx context.Context, accountID string) error {
|
|
// err := r.queries.SoftDeleteYclientsAccount(ctx, accountID)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// return nil
|
|
//}
|
|
|
|
func (r *YclientsRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListYclientsResp, error) {
|
|
rows, err := r.queries.GetUsersYclientsWithPagination(ctx, sqlcgen.GetUsersYclientsWithPaginationParams{
|
|
Accountid: accountID,
|
|
Column2: req.Page,
|
|
Limit: req.Size,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var users []model.YclientsAccountUser
|
|
var count int64
|
|
for _, row := range rows {
|
|
users = append(users, model.YclientsAccountUser{
|
|
ID: row.ID,
|
|
SalonID: row.Salonid,
|
|
YclientsUserID: row.Yclientsuserid,
|
|
Name: row.Name,
|
|
Role: row.Role,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
})
|
|
count = row.TotalCount
|
|
}
|
|
|
|
resp := model.UserListYclientsResp{
|
|
Count: count,
|
|
Items: users,
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|
|
|
|
func (r *YclientsRepository) CreateAccount(ctx context.Context, account model.YclientsAccount) (*model.YclientsAccount, error) {
|
|
row, err := r.queries.CreateYclientsAccount(ctx, sqlcgen.CreateYclientsAccountParams{
|
|
Accountid: account.AccountID,
|
|
Salonid: account.SalonID,
|
|
Title: account.Title,
|
|
Country: account.Country,
|
|
Shortdecription: account.ShortDecription,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &model.YclientsAccount{
|
|
ID: row.ID,
|
|
AccountID: row.Accountid,
|
|
SalonID: row.Salonid,
|
|
Title: row.Title,
|
|
Country: row.Country,
|
|
ShortDecription: row.Shortdecription,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
}, nil
|
|
|
|
}
|
|
|
|
// company
|
|
//func (r *YclientsRepository) GettingCompanyWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.CompanyListYclientsResp, error) {
|
|
// rows, err := r.queries.GetCompanyYclientsWithPagination(ctx, sqlcgen.GetCompanyYclientsWithPaginationParams{
|
|
// Accountid: accountID,
|
|
// Column2: req.Page,
|
|
// Limit: req.Size,
|
|
// })
|
|
// if err != nil {
|
|
// return nil, err
|
|
// }
|
|
// var users []model.YclientsCompany
|
|
// var count int64
|
|
// for _, row := range rows {
|
|
// users = append(users, model.YclientsCompany{
|
|
// ID: row.ID,
|
|
// SalonID: row.Salonid,
|
|
// Title: row.Title,
|
|
// ShortDecription: row.Shortdecription,
|
|
// Active: row.Active,
|
|
// Country: row.Country,
|
|
// GroupPriority: row.Grouppriority,
|
|
// Deleted: row.Deleted,
|
|
// CreatedAt: row.Createdat,
|
|
// })
|
|
// count = row.TotalCount
|
|
// }
|
|
//
|
|
// resp := model.CompanyListYclientsResp{
|
|
// Count: count,
|
|
// Items: users,
|
|
// }
|
|
//
|
|
// return &resp, nil
|
|
//}
|
|
//
|
|
//func (r *YclientsRepository) UpdateCompany() {
|
|
//
|
|
//}
|
|
|
|
// services
|
|
|
|
func (r *YclientsRepository) GettingServicesWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.ServicesListYclientsResp, error) {
|
|
rows, err := r.queries.GetServicesYclientsWithPagination(ctx, sqlcgen.GetServicesYclientsWithPaginationParams{
|
|
Accountid: accountID,
|
|
Column2: req.Page,
|
|
Limit: req.Size,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var users []model.YclientsServices
|
|
var count int64
|
|
for _, row := range rows {
|
|
users = append(users, model.YclientsServices{
|
|
ID: row.ID,
|
|
SalonID: row.Salonid,
|
|
ServiceID: row.Serviceid,
|
|
Title: row.Title,
|
|
CategoryID: row.Categoryid,
|
|
PriceMin: row.Pricemin,
|
|
PriceMax: row.Pricemax,
|
|
Discount: row.Discount,
|
|
Comment: row.Comment,
|
|
Active: row.Active,
|
|
ApiID: row.Apiid,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
})
|
|
count = row.TotalCount
|
|
}
|
|
|
|
resp := model.ServicesListYclientsResp{
|
|
Count: count,
|
|
Items: users,
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|
|
|
|
// todo
|
|
func (r *YclientsRepository) UpdateServices() {
|
|
|
|
}
|
|
|
|
// timeslots
|
|
|
|
func (r *YclientsRepository) GettingTimeslotsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.TimeslotsListYclientsResp, error) {
|
|
rows, err := r.queries.GetTimeslotsYclientsWithPagination(ctx, sqlcgen.GetTimeslotsYclientsWithPaginationParams{
|
|
Accountid: accountID,
|
|
Column2: req.Page,
|
|
Limit: req.Size,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var users []model.Timeslots
|
|
var count int64
|
|
for _, row := range rows {
|
|
var weekdaysSettings []model.WeekdaySetting
|
|
err = json.Unmarshal(row.Weekdayssettings, &weekdaysSettings)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var datesSettings model.DateSetting
|
|
err = json.Unmarshal(row.Datessettings, &datesSettings)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
users = append(users, model.Timeslots{
|
|
ID: row.ID,
|
|
SalonID: row.Salonid,
|
|
IsEnabled: row.Isenabled,
|
|
WeekdaysSettings: weekdaysSettings,
|
|
DatesSettings: datesSettings,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
})
|
|
count = row.TotalCount
|
|
}
|
|
|
|
resp := model.TimeslotsListYclientsResp{
|
|
Count: count,
|
|
Items: users,
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|
|
|
|
// todo
|
|
func (r *YclientsRepository) UpdateTimeslots() {
|
|
|
|
}
|