diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 98d8247..bc95763 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1045,3 +1045,15 @@ WHERE AccountID = $1 AND Deleted = false; 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 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; diff --git a/dal/schema/000016_init.down.sql b/dal/schema/000016_init.down.sql new file mode 100644 index 0000000..28b2333 --- /dev/null +++ b/dal/schema/000016_init.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS amoContact; \ No newline at end of file diff --git a/dal/schema/000016_init.up.sql b/dal/schema/000016_init.up.sql new file mode 100644 index 0000000..a4866d0 --- /dev/null +++ b/dal/schema/000016_init.up.sql @@ -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 +) \ No newline at end of file diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index c6d928d..90a4ade 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -32,6 +32,13 @@ type Accountsamo struct { 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 { ID int64 `db:"id" json:"id"` Accountid int32 `db:"accountid" json:"accountid"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 2315192..3fbd37c 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1449,6 +1449,46 @@ func (q *Queries) GetCurrentCompany(ctx context.Context, accountid string) (Acco 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 SELECT p.id, p.privilegeID, p.privilege_name, p.amount, p.created_at, a.user_id FROM privileges p @@ -2893,6 +2933,23 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) (A 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 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 } +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 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 diff --git a/model/amo.go b/model/amo.go index bdcbcf1..5a0dae0 100644 --- a/model/amo.go +++ b/model/amo.go @@ -288,3 +288,11 @@ type AmoUsersTrueResults struct { QuizAccountID string DriveURL string } + +// возможно стоит добавить enum? тип ContactQuizConfig уже есть +type ContactAmo struct { + ID int64 + AccountID int32 // id аккаунта в амо к которому привязан контакт + AmoID int32 // id контакта в амо + Field string // значение поля +} diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c00080d..63e2e3d 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -6,6 +6,7 @@ import ( "encoding/json" "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/pj_errors" "time" ) @@ -1080,3 +1081,60 @@ func (r *AmoRepository) UpdatingDealAmoStatus(ctx context.Context, deps SaveDeal 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 +} diff --git a/sqlc.yaml b/sqlc.yaml index d367be4..07bb059 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -32,6 +32,8 @@ packages: - "./dal/schema/000013_init.down.sql" - "./dal/schema/000014_init.up.sql" - "./dal/schema/000014_init.down.sql" + - "./dal/schema/000016_init.up.sql" + - "./dal/schema/000016_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true