Merge branch 'amoContact' into 'main'
Amo contact See merge request backend/quiz/common!31
This commit is contained in:
commit
cb6177cdeb
@ -1045,3 +1045,15 @@ WHERE AccountID = $1 AND Deleted = false;
|
|||||||
UPDATE privileges p SET amount = amount - 1 FROM account a
|
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
|
WHERE p.account_id = a.id AND a.user_id = $1 AND p.privilegeID = $2 AND p.amount > 0
|
||||||
RETURNING p.id, p.privilegeID, p.account_id, p.privilege_name, p.amount, p.created_at;
|
RETURNING p.id, p.privilegeID, p.account_id, p.privilege_name, p.amount, p.created_at;
|
||||||
|
|
||||||
|
-- name: GetExistingContactAmo :many
|
||||||
|
WITH getAmoID AS (
|
||||||
|
SELECT AmoID FROM amoContact WHERE amoContact.AccountID = $1 AND amoContact.Field IN ($2::text[])
|
||||||
|
) SELECT * FROM amoContact
|
||||||
|
WHERE amoContact.AccountID = $1 AND amoContact.AmoID IN (SELECT AmoID FROM getAmoID);
|
||||||
|
|
||||||
|
-- name: InsertContactAmo :one
|
||||||
|
INSERT INTO amoContact (AccountID, AmoID, Field) VALUES ($1, $2, $3) RETURNING AmoID;
|
||||||
|
|
||||||
|
-- name: UpdateAmoContact :exec
|
||||||
|
UPDATE amoContact SET Field = $1,AmoID=$3 WHERE ID = $2;
|
||||||
|
|||||||
1
dal/schema/000016_init.down.sql
Normal file
1
dal/schema/000016_init.down.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS amoContact;
|
||||||
6
dal/schema/000016_init.up.sql
Normal file
6
dal/schema/000016_init.up.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS amoContact (
|
||||||
|
ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY,
|
||||||
|
AccountID INT NOT NULL, -- ID "компании" в амо
|
||||||
|
AmoID INT NOT NULL, -- ID контакта в амо
|
||||||
|
Field text NOT NULL DEFAULT '' -- значение чего то email? phone? etc
|
||||||
|
)
|
||||||
@ -32,6 +32,13 @@ type Accountsamo struct {
|
|||||||
Driveurl string `db:"driveurl" json:"driveurl"`
|
Driveurl string `db:"driveurl" json:"driveurl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Amocontact struct {
|
||||||
|
ID int64 `db:"id" json:"id"`
|
||||||
|
Accountid int32 `db:"accountid" json:"accountid"`
|
||||||
|
Amoid int32 `db:"amoid" json:"amoid"`
|
||||||
|
Field string `db:"field" json:"field"`
|
||||||
|
}
|
||||||
|
|
||||||
type Amocrmstatus struct {
|
type Amocrmstatus struct {
|
||||||
ID int64 `db:"id" json:"id"`
|
ID int64 `db:"id" json:"id"`
|
||||||
Accountid int32 `db:"accountid" json:"accountid"`
|
Accountid int32 `db:"accountid" json:"accountid"`
|
||||||
|
|||||||
@ -1449,6 +1449,46 @@ func (q *Queries) GetCurrentCompany(ctx context.Context, accountid string) (Acco
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getExistingContactAmo = `-- name: GetExistingContactAmo :many
|
||||||
|
WITH getAmoID AS (
|
||||||
|
SELECT AmoID FROM amoContact WHERE amoContact.AccountID = $1 AND amoContact.Field IN ($2::text[])
|
||||||
|
) SELECT id, accountid, amoid, field FROM amoContact
|
||||||
|
WHERE amoContact.AccountID = $1 AND amoContact.AmoID IN (SELECT AmoID FROM getAmoID)
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetExistingContactAmoParams struct {
|
||||||
|
Accountid int32 `db:"accountid" json:"accountid"`
|
||||||
|
Column2 []string `db:"column_2" json:"column_2"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetExistingContactAmo(ctx context.Context, arg GetExistingContactAmoParams) ([]Amocontact, error) {
|
||||||
|
rows, err := q.db.QueryContext(ctx, getExistingContactAmo, arg.Accountid, pq.Array(arg.Column2))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []Amocontact
|
||||||
|
for rows.Next() {
|
||||||
|
var i Amocontact
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Accountid,
|
||||||
|
&i.Amoid,
|
||||||
|
&i.Field,
|
||||||
|
); 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 getExpiredCountPrivilege = `-- name: GetExpiredCountPrivilege :many
|
const getExpiredCountPrivilege = `-- name: GetExpiredCountPrivilege :many
|
||||||
SELECT p.id, p.privilegeID, p.privilege_name, p.amount, p.created_at, a.user_id
|
SELECT p.id, p.privilegeID, p.privilege_name, p.amount, p.created_at, a.user_id
|
||||||
FROM privileges p
|
FROM privileges p
|
||||||
@ -2893,6 +2933,23 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (A
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const insertContactAmo = `-- name: InsertContactAmo :one
|
||||||
|
INSERT INTO amoContact (AccountID, AmoID, Field) VALUES ($1, $2, $3) RETURNING AmoID
|
||||||
|
`
|
||||||
|
|
||||||
|
type InsertContactAmoParams struct {
|
||||||
|
Accountid int32 `db:"accountid" json:"accountid"`
|
||||||
|
Amoid int32 `db:"amoid" json:"amoid"`
|
||||||
|
Field string `db:"field" json:"field"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) InsertContactAmo(ctx context.Context, arg InsertContactAmoParams) (int32, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, insertContactAmo, arg.Accountid, arg.Amoid, arg.Field)
|
||||||
|
var amoid int32
|
||||||
|
err := row.Scan(&amoid)
|
||||||
|
return amoid, err
|
||||||
|
}
|
||||||
|
|
||||||
const insertPrivilege = `-- name: InsertPrivilege :exec
|
const insertPrivilege = `-- name: InsertPrivilege :exec
|
||||||
INSERT INTO privileges (privilegeID, account_id, privilege_name, amount, created_at) VALUES ($1, $2, $3, $4, $5)
|
INSERT INTO privileges (privilegeID, account_id, privilege_name, amount, created_at) VALUES ($1, $2, $3, $4, $5)
|
||||||
`
|
`
|
||||||
@ -3450,6 +3507,21 @@ func (q *Queries) UpdateAmoAccountUser(ctx context.Context, arg UpdateAmoAccount
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateAmoContact = `-- name: UpdateAmoContact :exec
|
||||||
|
UPDATE amoContact SET Field = $1,AmoID=$3 WHERE ID = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateAmoContactParams struct {
|
||||||
|
Field string `db:"field" json:"field"`
|
||||||
|
ID int64 `db:"id" json:"id"`
|
||||||
|
Amoid int32 `db:"amoid" json:"amoid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateAmoContact(ctx context.Context, arg UpdateAmoContactParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, updateAmoContact, arg.Field, arg.ID, arg.Amoid)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const updateFieldRules = `-- name: UpdateFieldRules :exec
|
const updateFieldRules = `-- name: UpdateFieldRules :exec
|
||||||
UPDATE rules SET FieldsRule = $1
|
UPDATE rules SET FieldsRule = $1
|
||||||
WHERE AccountID = (SELECT AmoID FROM accountsAmo WHERE accountsAmo.AccountID = $2 AND accountsAmo.Deleted = false) AND QuizID = $3 AND Deleted = false
|
WHERE AccountID = (SELECT AmoID FROM accountsAmo WHERE accountsAmo.AccountID = $2 AND accountsAmo.Deleted = false) AND QuizID = $3 AND Deleted = false
|
||||||
|
|||||||
@ -288,3 +288,11 @@ type AmoUsersTrueResults struct {
|
|||||||
QuizAccountID string
|
QuizAccountID string
|
||||||
DriveURL string
|
DriveURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// возможно стоит добавить enum? тип ContactQuizConfig уже есть
|
||||||
|
type ContactAmo struct {
|
||||||
|
ID int64
|
||||||
|
AccountID int32 // id аккаунта в амо к которому привязан контакт
|
||||||
|
AmoID int32 // id контакта в амо
|
||||||
|
Field string // значение поля
|
||||||
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
|
||||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||||||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -1080,3 +1081,60 @@ func (r *AmoRepository) UpdatingDealAmoStatus(ctx context.Context, deps SaveDeal
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// методы для contact в амо
|
||||||
|
|
||||||
|
func (r *AmoRepository) GetExistingContactAmo(ctx context.Context, accountID int32, fields []string) (map[int32][]model.ContactAmo, error) {
|
||||||
|
rows, err := r.queries.GetExistingContactAmo(ctx, sqlcgen.GetExistingContactAmoParams{
|
||||||
|
Accountid: accountID,
|
||||||
|
Column2: fields,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, pj_errors.ErrNotFound
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make(map[int32][]model.ContactAmo)
|
||||||
|
|
||||||
|
for _, row := range rows {
|
||||||
|
result[row.Amoid] = append(result[row.Amoid], model.ContactAmo{
|
||||||
|
ID: row.ID,
|
||||||
|
AmoID: row.Amoid,
|
||||||
|
AccountID: row.Accountid,
|
||||||
|
Field: row.Field,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *AmoRepository) InsertContactAmo(ctx context.Context, val model.ContactAmo) (int32, error) {
|
||||||
|
amoID, err := r.queries.InsertContactAmo(ctx, sqlcgen.InsertContactAmoParams{
|
||||||
|
Accountid: val.AccountID,
|
||||||
|
Amoid: val.AmoID,
|
||||||
|
Field: val.Field,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return amoID, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *AmoRepository) UpdateAmoContact(ctx context.Context, id int64, field string, newAmoID int32) error {
|
||||||
|
err := r.queries.UpdateAmoContact(ctx, sqlcgen.UpdateAmoContactParams{
|
||||||
|
Field: field,
|
||||||
|
ID: id,
|
||||||
|
Amoid: newAmoID,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@ -32,6 +32,8 @@ packages:
|
|||||||
- "./dal/schema/000013_init.down.sql"
|
- "./dal/schema/000013_init.down.sql"
|
||||||
- "./dal/schema/000014_init.up.sql"
|
- "./dal/schema/000014_init.up.sql"
|
||||||
- "./dal/schema/000014_init.down.sql"
|
- "./dal/schema/000014_init.down.sql"
|
||||||
|
- "./dal/schema/000016_init.up.sql"
|
||||||
|
- "./dal/schema/000016_init.down.sql"
|
||||||
engine: "postgresql"
|
engine: "postgresql"
|
||||||
emit_json_tags: true
|
emit_json_tags: true
|
||||||
emit_db_tags: true
|
emit_db_tags: true
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user