added method CreateAccount and update table YclientsAccounts

This commit is contained in:
pasha1coil 2025-10-06 15:15:57 +03:00
parent c172584b98
commit 425ba10ad4
6 changed files with 188 additions and 179 deletions

@ -1510,15 +1510,15 @@ FROM YclientsAccountUsers u
WHERE u.Deleted = false
ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3;
-- name: GetCompanyYclientsWithPagination :many
WITH user_data AS (
SELECT SalonID FROM YclientsAccounts WHERE YclientsAccounts.AccountID = $1 AND YclientsAccounts.Deleted = false
)
SELECT u.*, COUNT(*) OVER() as total_count
FROM YclientsCompany u
JOIN user_data a ON u.SalonID = a.SalonID
WHERE u.Deleted = false
ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3;
-- -- name: GetCompanyYclientsWithPagination :many
-- WITH user_data AS (
-- SELECT SalonID FROM YclientsAccounts WHERE YclientsAccounts.AccountID = $1 AND YclientsAccounts.Deleted = false
-- )
-- SELECT u.*, COUNT(*) OVER() as total_count
-- FROM YclientsCompany u
-- JOIN user_data a ON u.SalonID = a.SalonID
-- WHERE u.Deleted = false
-- ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3;
-- name: GetServicesYclientsWithPagination :many
WITH user_data AS (
@ -1551,3 +1551,6 @@ update YclientsTokens set Expiration=$1, Active = $2 where AccountID = $3 RETURN
-- name: GetYclientsUserToken :one
select * from YclientsTokens where AccountID = $1 and Expiration=false and Active = true;
-- name: CreateYclientsAccount :one
insert into YclientsAccounts (AccountID,SalonID,Title,Country,ShortDecription,Active) values ($1,$2,$3,$4,$5,$6) RETURNING *;

@ -13,7 +13,9 @@ CREATE TABLE IF NOT EXISTS YclientsAccounts (
ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
AccountID VARCHAR(30) NOT NULL DEFAULT '', -- ID аккаунта у нас
SalonID INT NOT NULL, -- ID компании
Name VARCHAR(512) NOT NULL DEFAULT '',
Title text NOT NULL DEFAULT '',
ShortDecription text NOT NULL DEFAULT '',
Active INT NOT NULL,
Country VARCHAR(50) NOT NULL DEFAULT '',
Deleted BOOLEAN NOT NULL DEFAULT FALSE,
CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
@ -29,17 +31,17 @@ CREATE TABLE IF NOT EXISTS YclientsAccountUsers (
CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS YclientsCompany (
ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
SalonID INT NOT NULL, -- ID компании
Title text NOT NULL,
ShortDecription text NOT NULL,
Active INT NOT NULL,
Country text NOT NULL,
GroupPriority INT NOT NULL,
Deleted BOOLEAN NOT NULL DEFAULT FALSE,
CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- CREATE TABLE IF NOT EXISTS YclientsCompany (
-- ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
-- SalonID INT NOT NULL, -- ID компании
-- Title text NOT NULL,
-- ShortDecription text NOT NULL,
-- Active INT NOT NULL,
-- Country text NOT NULL,
-- GroupPriority INT NOT NULL,
-- Deleted BOOLEAN NOT NULL DEFAULT FALSE,
-- CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
-- );
CREATE TABLE IF NOT EXISTS YclientsServices (
ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,

@ -403,7 +403,9 @@ type Yclientsaccount struct {
ID int64 `db:"id" json:"id"`
Accountid string `db:"accountid" json:"accountid"`
Salonid int32 `db:"salonid" json:"salonid"`
Name string `db:"name" json:"name"`
Title string `db:"title" json:"title"`
Shortdecription string `db:"shortdecription" json:"shortdecription"`
Active int32 `db:"active" json:"active"`
Country string `db:"country" json:"country"`
Deleted bool `db:"deleted" json:"deleted"`
Createdat time.Time `db:"createdat" json:"createdat"`
@ -419,18 +421,6 @@ type Yclientsaccountuser struct {
Createdat time.Time `db:"createdat" json:"createdat"`
}
type Yclientscompany struct {
ID int64 `db:"id" json:"id"`
Salonid int32 `db:"salonid" json:"salonid"`
Title string `db:"title" json:"title"`
Shortdecription string `db:"shortdecription" json:"shortdecription"`
Active int32 `db:"active" json:"active"`
Country string `db:"country" json:"country"`
Grouppriority int32 `db:"grouppriority" json:"grouppriority"`
Deleted bool `db:"deleted" json:"deleted"`
Createdat time.Time `db:"createdat" json:"createdat"`
}
type Yclientsservice struct {
ID int64 `db:"id" json:"id"`
Salonid int32 `db:"salonid" json:"salonid"`

@ -1264,6 +1264,43 @@ func (q *Queries) CreateWebHook(ctx context.Context, arg CreateWebHookParams) er
return err
}
const createYclientsAccount = `-- name: CreateYclientsAccount :one
insert into YclientsAccounts (AccountID,SalonID,Title,Country,ShortDecription,Active) values ($1,$2,$3,$4,$5,$6) RETURNING id, accountid, salonid, title, shortdecription, active, country, deleted, createdat
`
type CreateYclientsAccountParams struct {
Accountid string `db:"accountid" json:"accountid"`
Salonid int32 `db:"salonid" json:"salonid"`
Title string `db:"title" json:"title"`
Country string `db:"country" json:"country"`
Shortdecription string `db:"shortdecription" json:"shortdecription"`
Active int32 `db:"active" json:"active"`
}
func (q *Queries) CreateYclientsAccount(ctx context.Context, arg CreateYclientsAccountParams) (Yclientsaccount, error) {
row := q.db.QueryRowContext(ctx, createYclientsAccount,
arg.Accountid,
arg.Salonid,
arg.Title,
arg.Country,
arg.Shortdecription,
arg.Active,
)
var i Yclientsaccount
err := row.Scan(
&i.ID,
&i.Accountid,
&i.Salonid,
&i.Title,
&i.Shortdecription,
&i.Active,
&i.Country,
&i.Deleted,
&i.Createdat,
)
return i, err
}
const decrementManual = `-- name: DecrementManual :one
UPDATE privileges p SET amount = amount - 1 FROM account a
WHERE p.account_id = a.id AND a.user_id = $1 AND p.privilegeID = $2 AND p.amount > 0
@ -2383,70 +2420,6 @@ func (q *Queries) GetBitrixTokenById(ctx context.Context, accountid string) (Bit
return i, err
}
const getCompanyYclientsWithPagination = `-- name: GetCompanyYclientsWithPagination :many
WITH user_data AS (
SELECT SalonID FROM YclientsAccounts WHERE YclientsAccounts.AccountID = $1 AND YclientsAccounts.Deleted = false
)
SELECT u.id, u.salonid, u.title, u.shortdecription, u.active, u.country, u.grouppriority, u.deleted, u.createdat, COUNT(*) OVER() as total_count
FROM YclientsCompany u
JOIN user_data a ON u.SalonID = a.SalonID
WHERE u.Deleted = false
ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3
`
type GetCompanyYclientsWithPaginationParams struct {
Accountid string `db:"accountid" json:"accountid"`
Column2 interface{} `db:"column_2" json:"column_2"`
Limit int32 `db:"limit" json:"limit"`
}
type GetCompanyYclientsWithPaginationRow struct {
ID int64 `db:"id" json:"id"`
Salonid int32 `db:"salonid" json:"salonid"`
Title string `db:"title" json:"title"`
Shortdecription string `db:"shortdecription" json:"shortdecription"`
Active int32 `db:"active" json:"active"`
Country string `db:"country" json:"country"`
Grouppriority int32 `db:"grouppriority" json:"grouppriority"`
Deleted bool `db:"deleted" json:"deleted"`
Createdat time.Time `db:"createdat" json:"createdat"`
TotalCount int64 `db:"total_count" json:"total_count"`
}
func (q *Queries) GetCompanyYclientsWithPagination(ctx context.Context, arg GetCompanyYclientsWithPaginationParams) ([]GetCompanyYclientsWithPaginationRow, error) {
rows, err := q.db.QueryContext(ctx, getCompanyYclientsWithPagination, arg.Accountid, arg.Column2, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetCompanyYclientsWithPaginationRow
for rows.Next() {
var i GetCompanyYclientsWithPaginationRow
if err := rows.Scan(
&i.ID,
&i.Salonid,
&i.Title,
&i.Shortdecription,
&i.Active,
&i.Country,
&i.Grouppriority,
&i.Deleted,
&i.Createdat,
&i.TotalCount,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCurrentBitrixCompany = `-- name: GetCurrentBitrixCompany :one
SELECT id, accountid, bitrixid, deleted, createdat, subdomain FROM BitrixAccounts WHERE AccountID = $1 AND Deleted = false
`
@ -2488,7 +2461,7 @@ func (q *Queries) GetCurrentCompany(ctx context.Context, accountid string) (Acco
const getCurrentYclientsCompany = `-- name: GetCurrentYclientsCompany :one
SELECT id, accountid, salonid, name, country, deleted, createdat FROM YclientsAccounts WHERE AccountID = $1 AND Deleted = false
SELECT id, accountid, salonid, title, shortdecription, active, country, deleted, createdat FROM YclientsAccounts WHERE AccountID = $1 AND Deleted = false
`
// Yclients
@ -2499,7 +2472,9 @@ func (q *Queries) GetCurrentYclientsCompany(ctx context.Context, accountid strin
&i.ID,
&i.Accountid,
&i.Salonid,
&i.Name,
&i.Title,
&i.Shortdecription,
&i.Active,
&i.Country,
&i.Deleted,
&i.Createdat,
@ -3652,6 +3627,7 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn
}
const getServicesYclientsWithPagination = `-- name: GetServicesYclientsWithPagination :many
WITH user_data AS (
SELECT SalonID FROM YclientsAccounts WHERE YclientsAccounts.AccountID = $1 AND YclientsAccounts.Deleted = false
)
@ -3685,6 +3661,15 @@ type GetServicesYclientsWithPaginationRow struct {
TotalCount int64 `db:"total_count" json:"total_count"`
}
// -- name: GetCompanyYclientsWithPagination :many
// WITH user_data AS (
// SELECT SalonID FROM YclientsAccounts WHERE YclientsAccounts.AccountID = $1 AND YclientsAccounts.Deleted = false
// )
// SELECT u.*, COUNT(*) OVER() as total_count
// FROM YclientsCompany u
// JOIN user_data a ON u.SalonID = a.SalonID
// WHERE u.Deleted = false
// ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3;
func (q *Queries) GetServicesYclientsWithPagination(ctx context.Context, arg GetServicesYclientsWithPaginationParams) ([]GetServicesYclientsWithPaginationRow, error) {
rows, err := q.db.QueryContext(ctx, getServicesYclientsWithPagination, arg.Accountid, arg.Column2, arg.Limit)
if err != nil {

@ -15,7 +15,9 @@ type YclientsAccount struct {
ID int64 `json:"id"`
AccountID string `json:"accountID"` // ID аккаунта нас
SalonID int32 `json:"salon_id"` // ID "аккаунта который ГЛАВНЫЙ"
Name string `json:"name"`
Title string `json:"title"`
ShortDecription string `json:"shortDecription"`
Active int32 `json:"active"`
Country string `json:"country"`
Deleted bool `json:"deleted"`
CreatedAt time.Time `json:"createdAt"`
@ -37,22 +39,22 @@ type UserListYclientsResp struct {
Items []YclientsAccountUser `json:"items"`
}
type YclientsCompany struct {
ID int64 `json:"id"`
SalonID int32 `json:"salon_id"` // ID "аккаунта который ГЛАВНЫЙ"
Title string `json:"title"`
ShortDecription string `json:"shortDecription"`
Active int32 `json:"active"`
Country string `json:"country"`
GroupPriority int32 `json:"groupPriority"`
Deleted bool `json:"deleted"`
CreatedAt time.Time `json:"createdAt"`
}
//type YclientsCompany struct {
// ID int64 `json:"id"`
// SalonID int32 `json:"salon_id"` // ID "аккаунта который ГЛАВНЫЙ"
// Title string `json:"title"`
// ShortDecription string `json:"shortDecription"`
// Active int32 `json:"active"`
// Country string `json:"country"`
// GroupPriority int32 `json:"groupPriority"`
// Deleted bool `json:"deleted"`
// CreatedAt time.Time `json:"createdAt"`
//}
type CompanyListYclientsResp struct {
Count int64 `json:"count"`
Items []YclientsCompany `json:"items"`
}
//type CompanyListYclientsResp struct {
// Count int64 `json:"count"`
// Items []YclientsCompany `json:"items"`
//}
type YclientsServices struct {
ID int64 `json:"id"`

@ -106,22 +106,24 @@ func (r *YclientsRepository) GetCurrentAccount(ctx context.Context, accountID st
ID: row.ID,
AccountID: row.Accountid,
SalonID: row.Salonid,
Name: row.Name,
Title: row.Title,
Country: row.Country,
ShortDecription: row.Shortdecription,
Active: row.Active,
Deleted: row.Deleted,
CreatedAt: row.Createdat,
}
//_, err = r.queries.CheckExpiredYclientsToken(ctx, accountID)
//if err != nil {
// if err == sql.ErrNoRows {
// user.Stale = false
// return &user, nil
// }
// return nil, err
//}
_, 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
}
@ -166,47 +168,72 @@ func (r *YclientsRepository) GettingUserWithPagination(ctx context.Context, req
return &resp, 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,
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,
Active: account.Active,
})
if err != nil {
return nil, err
}
var users []model.YclientsCompany
var count int64
for _, row := range rows {
users = append(users, model.YclientsCompany{
return &model.YclientsAccount{
ID: row.ID,
AccountID: row.Accountid,
SalonID: row.Salonid,
Title: row.Title,
Country: row.Country,
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
}
// todo
func (r *YclientsRepository) UpdateCompany() {
}, 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) {