567 lines
15 KiB
Go
567 lines
15 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
|
|
}
|
|
|
|
func (r *YclientsRepository) GetAllYclientsUserToken(ctx context.Context) ([]model.YclientsToken, error) {
|
|
rows, err := r.queries.GetAllYclientsUserToken(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var tokens []model.YclientsToken
|
|
for _, row := range rows {
|
|
tokens = append(tokens, model.YclientsToken{
|
|
AccountID: row.Accountid,
|
|
SalonID: row.Salonid,
|
|
AccessToken: row.Accesstoken,
|
|
Active: row.Active,
|
|
Expiration: row.Expiration,
|
|
})
|
|
}
|
|
return tokens, nil
|
|
}
|
|
|
|
// 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,
|
|
YclientsID: row.Yclientsid,
|
|
Name: row.Name,
|
|
IDPosition: row.Idposition,
|
|
TitlePosition: row.Titleposition,
|
|
Fired: row.Fired,
|
|
Status: row.Status,
|
|
Hidden: row.Hidden,
|
|
YclientsUserID: row.Yclientsuserid,
|
|
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
|
|
|
|
}
|
|
|
|
func (r *YclientsRepository) GetUserUsersByID(ctx context.Context, salonID int32) ([]model.YclientsAccountUser, error) {
|
|
rows, err := r.queries.GetUsersByIDYclients(ctx, salonID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var users []model.YclientsAccountUser
|
|
for _, row := range rows {
|
|
users = append(users, model.YclientsAccountUser{
|
|
ID: row.ID,
|
|
SalonID: row.Salonid,
|
|
YclientsID: row.Yclientsid,
|
|
Name: row.Name,
|
|
IDPosition: row.Idposition,
|
|
TitlePosition: row.Titleposition,
|
|
Fired: row.Fired,
|
|
Status: row.Status,
|
|
Hidden: row.Hidden,
|
|
YclientsUserID: row.Yclientsuserid,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
})
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
func (r *YclientsRepository) UpdateAccountUser(ctx context.Context, user model.YclientsAccountUser) error {
|
|
err := r.queries.UpdateYclientsAccountUser(ctx, sqlcgen.UpdateYclientsAccountUserParams{
|
|
Salonid: user.SalonID,
|
|
Yclientsid: user.YclientsID,
|
|
Name: user.Name,
|
|
Specialization: user.Specialization,
|
|
Idposition: user.IDPosition,
|
|
Titleposition: user.TitlePosition,
|
|
Fired: user.Fired,
|
|
Status: user.Status,
|
|
Hidden: user.Hidden,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *YclientsRepository) AddAccountUser(ctx context.Context, user model.YclientsAccountUser) error {
|
|
err := r.queries.AddYclientsAccountUser(ctx, sqlcgen.AddYclientsAccountUserParams{
|
|
Salonid: user.SalonID,
|
|
Yclientsid: user.YclientsID,
|
|
Name: user.Name,
|
|
Specialization: user.Specialization,
|
|
Idposition: user.IDPosition,
|
|
Titleposition: user.TitlePosition,
|
|
Fired: user.Fired,
|
|
Status: user.Status,
|
|
Hidden: user.Hidden,
|
|
Yclientsuserid: user.YclientsUserID,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *YclientsRepository) DeleteUsers(ctx context.Context, ids []int64) error {
|
|
err := r.queries.DeleteYclientsUsers(ctx, ids)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return 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 services []model.YclientsServices
|
|
var count int64
|
|
for _, row := range rows {
|
|
var staff []model.YclientsServiceStaff
|
|
err = json.Unmarshal(row.Staff, &staff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
services = append(services, model.YclientsServices{
|
|
ID: row.ID,
|
|
SalonID: row.Salonid,
|
|
ServiceID: row.Serviceid,
|
|
SalonServiceID: row.Salonserviceid,
|
|
Title: row.Title,
|
|
CategoryID: row.Categoryid,
|
|
PriceMin: row.Pricemin,
|
|
PriceMax: row.Pricemax,
|
|
Discount: row.Discount,
|
|
Comment: row.Comment,
|
|
Active: row.Active,
|
|
ApiID: row.Apiid,
|
|
Staff: staff,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
})
|
|
count = row.TotalCount
|
|
}
|
|
|
|
resp := model.ServicesListYclientsResp{
|
|
Count: count,
|
|
Items: services,
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|
|
|
|
func (r *YclientsRepository) GetAccountServicesByID(ctx context.Context, salonID int32) ([]model.YclientsServices, error) {
|
|
rows, err := r.queries.GetServicesByIDYclients(ctx, salonID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var services []model.YclientsServices
|
|
for _, row := range rows {
|
|
var staff []model.YclientsServiceStaff
|
|
err = json.Unmarshal(row.Staff, &staff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
services = append(services, model.YclientsServices{
|
|
ID: row.ID,
|
|
SalonID: row.Salonid,
|
|
ServiceID: row.Serviceid,
|
|
SalonServiceID: row.Salonserviceid,
|
|
Title: row.Title,
|
|
CategoryID: row.Categoryid,
|
|
PriceMin: row.Pricemin,
|
|
PriceMax: row.Pricemax,
|
|
Discount: row.Discount,
|
|
Comment: row.Comment,
|
|
Active: row.Active,
|
|
ApiID: row.Apiid,
|
|
Staff: staff,
|
|
Deleted: row.Deleted,
|
|
CreatedAt: row.Createdat,
|
|
})
|
|
}
|
|
return services, nil
|
|
}
|
|
|
|
func (r *YclientsRepository) UpdateAccountServices(ctx context.Context, service model.YclientsServices) error {
|
|
staff, err := json.Marshal(service.Staff)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = r.queries.UpdateYclientsAccountServices(ctx, sqlcgen.UpdateYclientsAccountServicesParams{
|
|
Salonid: service.SalonID,
|
|
Serviceid: service.ServiceID,
|
|
Salonserviceid: service.SalonServiceID,
|
|
Title: service.Title,
|
|
Categoryid: service.CategoryID,
|
|
Pricemin: service.PriceMin,
|
|
Pricemax: service.PriceMax,
|
|
Discount: service.Discount,
|
|
Comment: service.Comment,
|
|
Active: service.Active,
|
|
Apiid: service.ApiID,
|
|
Staff: staff,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *YclientsRepository) AddAccountService(ctx context.Context, service model.YclientsServices) error {
|
|
staff, err := json.Marshal(service.Staff)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = r.queries.AddYclientsAccountService(ctx, sqlcgen.AddYclientsAccountServiceParams{
|
|
Salonid: service.SalonID,
|
|
Serviceid: service.ServiceID,
|
|
Salonserviceid: service.SalonServiceID,
|
|
Title: service.Title,
|
|
Categoryid: service.CategoryID,
|
|
Pricemin: service.PriceMin,
|
|
Pricemax: service.PriceMax,
|
|
Discount: service.Discount,
|
|
Comment: service.Comment,
|
|
Active: service.Active,
|
|
Apiid: service.ApiID,
|
|
Staff: staff,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *YclientsRepository) DeleteServices(ctx context.Context, ids []int64) error {
|
|
err := r.queries.DeleteYclientsServices(ctx, ids)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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 timeslots []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
|
|
}
|
|
|
|
timeslots = append(timeslots, 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: timeslots,
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|
|
|
|
func (r *YclientsRepository) UpdateAccountTimeslots(ctx context.Context, salonID int32, timeslots model.Timeslots) error {
|
|
weekdaysSettings, err := json.Marshal(timeslots.WeekdaysSettings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
datesSettings, err := json.Marshal(timeslots.DatesSettings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = r.queries.UpdateYclientsAccountTimeslots(ctx, sqlcgen.UpdateYclientsAccountTimeslotsParams{
|
|
Salonid: salonID,
|
|
Isenabled: timeslots.IsEnabled,
|
|
Weekdayssettings: weekdaysSettings,
|
|
Datessettings: datesSettings,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *YclientsRepository) AddAccountTimeslots(ctx context.Context, salonID int32, timeslots model.Timeslots) error {
|
|
weekdaysSettings, err := json.Marshal(timeslots.WeekdaysSettings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
datesSettings, err := json.Marshal(timeslots.DatesSettings)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = r.queries.AddYclientsAccountTimeslots(ctx, sqlcgen.AddYclientsAccountTimeslotsParams{
|
|
Salonid: salonID,
|
|
Isenabled: timeslots.IsEnabled,
|
|
Weekdayssettings: weekdaysSettings,
|
|
Datessettings: datesSettings,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *YclientsRepository) DeleteTimeslots(ctx context.Context, ids []int64) error {
|
|
err := r.queries.DeleteYclientsTimeslots(ctx, ids)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|