package amo import ( "context" "database/sql" "encoding/json" "fmt" "github.com/sqlc-dev/pqtype" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" "time" ) type AmoRepository struct { queries *sqlcgen.Queries pool *sql.DB } type Deps struct { Queries *sqlcgen.Queries Pool *sql.DB } func NewAmoRepository(deps Deps) *AmoRepository { return &AmoRepository{ queries: deps.Queries, pool: deps.Pool, } } // методы пользователя func (r *AmoRepository) UpdateListUsers(ctx context.Context) error { //TODO:IMPLEMENT ME return nil } func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListResp, error) { rows, err := r.queries.GetUsersWithPagination(ctx, sqlcgen.GetUsersWithPaginationParams{ Column1: req.Page, Limit: req.Size, }) if err != nil { return nil, err } var count int64 var users []model.User for _, row := range rows { user := model.User{ ID: row.ID, Accountid: row.Accountid, AmoID: row.Amoid, Name: row.Name, Email: row.Email, Role: row.Role, Createdat: row.Createdat.Time.Unix(), Subdomain: row.Subdomain, Amouserid: row.Amouserid, Country: row.Country, } count = row.TotalCount var group []model.UserGroups if !row.Group.Valid { err := json.Unmarshal(row.Group.RawMessage, &group) if err != nil { return nil, err } } user.Group = group users = append(users, user) } resp := model.UserListResp{ Count: count, Items: users, } return &resp, nil } func (r *AmoRepository) SoftDeleteAccount(ctx context.Context, accountID string) error { err := r.queries.SoftDeleteAccount(ctx, accountID) if err != nil { return err } return nil } func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string) (*model.User, error) { row, err := r.queries.GetCurrentAccount(ctx, accountID) if err != nil { return nil, err } user := model.User{ ID: row.ID, Accountid: row.Accountid, AmoID: row.Amoid, Name: row.Name, Email: row.Email, Role: row.Role, Createdat: row.Createdat.Time.Unix(), Subdomain: row.Subdomain, Amouserid: row.Amouserid, Country: row.Country, } var group []model.UserGroups if !row.Group.Valid { err := json.Unmarshal(row.Group.RawMessage, &group) if err != nil { return nil, err } } user.Group = group return &user, nil } func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string, userInfo model.User) error { group, err := json.Marshal(userInfo.Group) if err != nil { return err } err = r.queries.CreateAmoAccount(ctx, sqlcgen.CreateAmoAccountParams{ Accountid: accountID, Amoid: userInfo.AmoID, Name: userInfo.Name, Email: userInfo.Email, Role: userInfo.Role, Group: pqtype.NullRawMessage{RawMessage: group, Valid: len(group) > 0}, Createdat: sql.NullTime{Time: time.Now(), Valid: true}, Subdomain: userInfo.Subdomain, Amouserid: userInfo.Amouserid, Country: userInfo.Country, }) if err != nil { return err } return nil } func (r *AmoRepository) CheckUsers(ctx context.Context, amouserid int32, user model.User) error { group, err := json.Marshal(user.Group) if err != nil { return err } err = r.queries.CheckUsers(ctx, sqlcgen.CheckUsersParams{ Amoid: amouserid, Name: user.Name, Email: user.Email, Role: user.Role, Group: pqtype.NullRawMessage{RawMessage: group, Valid: len(group) > 0}, }) if err != nil { return err } return nil } // методы webhook func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) error { err := r.queries.CreateWebHook(ctx, sqlcgen.CreateWebHookParams{ Accountid: tokens.AccountID, Refreshtoken: tokens.RefreshToken, Accesstoken: tokens.AccessToken, Authcode: tokens.AuthCode, Expiration: time.Unix(tokens.Expiration, 0).In(time.UTC), Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, }) if err != nil { return err } return nil } func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens model.Token) error { err := r.queries.WebhookUpdate(ctx, sqlcgen.WebhookUpdateParams{ Accountid: tokens.AccountID, Accesstoken: tokens.AccessToken, Refreshtoken: tokens.RefreshToken, Expiration: time.Unix(tokens.Expiration, 0).In(time.UTC), Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, }) if err != nil { return err } return nil } // воркер запускается каждые 5 минут, поэтомму ищем токены котторые исекают менее чем через 10 минут отдаем их на обноление func (r *AmoRepository) CheckExpired(ctx context.Context) ([]model.Token, error) { rows, err := r.queries.CheckExpired(ctx) if err != nil { return nil, err } var tokens []model.Token for _, row := range rows { token := model.Token{ AccountID: row.Accountid, AccessToken: row.Accesstoken, RefreshToken: row.Refreshtoken, AuthCode: row.Authcode, Expiration: row.Expiration.Unix(), CreatedAt: row.Createdat.Time.Unix(), } tokens = append(tokens, token) } return tokens, nil } func (r *AmoRepository) GetAllTokens(ctx context.Context) ([]model.Token, error) { rows, err := r.queries.GetAllTokens(ctx) if err != nil { return nil, err } var tokens []model.Token for _, row := range rows { token := model.Token{ AccountID: row.Accountid, AccessToken: row.Accesstoken, RefreshToken: row.Refreshtoken, AuthCode: row.Authcode, Expiration: row.Expiration.Unix(), CreatedAt: row.Createdat.Time.Unix(), } tokens = append(tokens, token) } return tokens, nil } func (r *AmoRepository) WebhookDelete(ctx context.Context) error { return nil } // методы pipelines func (r *AmoRepository) UpdateListPipelines(ctx context.Context) error { //TODO:IMPLEMENT ME return nil } func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListPipelinesResp, error) { rows, err := r.queries.GetPipelinesWithPagination(ctx, sqlcgen.GetPipelinesWithPaginationParams{ Column1: req.Page, Limit: req.Size, }) if err != nil { return nil, err } var count int64 var pipelines []model.Pipeline for _, row := range rows { count = row.TotalCount pipeline := model.Pipeline{ ID: row.ID, Amoid: row.Amoid, AccountID: row.Accountid, Name: row.Name, Isarchive: row.Isarchive, Createdat: row.Createdat.Time.Unix(), } pipelines = append(pipelines, pipeline) } resp := model.UserListPipelinesResp{ Count: count, Items: pipelines, } return &resp, nil } func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pipeline) error { //dollar1, err := json.Marshal(pipelines) //if err != nil { // return err //} //_, err = r.queries.CheckPipelines(ctx, dollar1) //if err != nil { // return err //} return nil } // методы steps func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListStepsResp, error) { rows, err := r.queries.GetStepsWithPagination(ctx, sqlcgen.GetStepsWithPaginationParams{ Column1: req.Page, Limit: req.Size, }) if err != nil { return nil, err } var count int64 var steps []model.Step for _, row := range rows { count = row.TotalCount step := model.Step{ ID: row.ID, Amoid: row.Amoid, Pipelineid: row.Pipelineid, Accountid: row.Accountid, Name: row.Name, Color: row.Color, Createdat: row.Createdat.Time.Unix(), } steps = append(steps, step) } resp := model.UserListStepsResp{ Count: count, Items: steps, } return &resp, nil } func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { //TODO:IMPLEMENT ME return nil } func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error { //dollar1, err := json.Marshal(steps) //if err != nil { // return err //} // //_, err = r.queries.CheckSteps(ctx, dollar1) //if err != nil { // return err //} return nil } // методы tags func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListTagsResp, error) { rows, err := r.queries.GetTagsWithPagination(ctx, sqlcgen.GetTagsWithPaginationParams{ Column1: req.Page, Limit: req.Size, }) if err != nil { return nil, err } var count int64 var tags []model.Tag for _, row := range rows { count = row.TotalCount var entity model.EntityType if v, ok := row.Entity.(string); ok { entity = model.EntityType(v) } else { fmt.Println("unexpected type for EntityType:", row.Entity) } tag := model.Tag{ ID: row.ID, Amoid: row.Amoid, Accountid: row.Accountid, Entity: entity, Name: row.Name, Color: &row.Color, Createdat: row.Createdat.Time.Unix(), } tags = append(tags, tag) } resp := model.UserListTagsResp{ Count: count, Items: tags, } return &resp, nil } func (r *AmoRepository) UpdateListTags(ctx context.Context) error { //TODO:IMPLEMENT ME return nil } func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID string) error { column2, err := json.Marshal(tags) if err != nil { return err } rows, err := r.queries.CheckTags(ctx, sqlcgen.CheckTagsParams{ Accountid: tokenID, Column2: column2, }) if rows != nil { var toUpdate []model.Tag for _, row := range rows { var entity model.EntityType if v, ok := row.Entity.(string); ok { entity = model.EntityType(v) } else { fmt.Println("unexpected type for EntityType:", row.Entity) } to := model.Tag{ ID: row.ID, Amoid: row.Amoid, Accountid: row.Accountid, Entity: entity, Name: row.Name, Color: &row.Color, Createdat: row.Createdat.Time.Unix(), } toUpdate = append(toUpdate, to) } dollar1, err := json.Marshal(toUpdate) if err != nil { return err } err = r.queries.UpdateTags(ctx, dollar1) if err != nil { return err } } if err != nil { return err } return nil } // методы fields func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListFieldsResp, error) { rows, err := r.queries.GetFieldsWithPagination(ctx, sqlcgen.GetFieldsWithPaginationParams{ Column1: req.Page, Limit: req.Size, }) if err != nil { return nil, err } var count int64 var fields []model.Field for _, row := range rows { count = row.TotalCount var entity model.EntityType if v, ok := row.Entity.(string); ok { entity = model.EntityType(v) } else { fmt.Println("unexpected type for EntityType:", row.Entity) } field := model.Field{ ID: row.ID, Amoid: row.Amoid, Code: row.Code, Accountid: row.Accountid, Name: row.Name, Entity: entity, Type: row.Type, Createdat: row.Createdat.Time.Unix(), } fields = append(fields, field) } resp := model.UserListFieldsResp{ Count: count, Items: fields, } return &resp, nil } func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { //TODO:IMPLEMENT ME return nil } func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, tokenID string) error { //column2, err := json.Marshal(fields) //if err != nil { // return err //} // //_, err = r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{ // Accountid: tokenID, // Column2: column2, //}) // //if err != nil { // return err //} return nil } // методы rules func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq) error { //TODO:IMPLEMENT ME return nil } func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq) error { //TODO:IMPLEMENT ME return nil } func (r *AmoRepository) GettingQuizRules(ctx context.Context) (*model.Rule, error) { //TODO:IMPLEMENT ME return &model.Rule{}, nil } // методы UTMs func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error { return nil } func (r *AmoRepository) SavingUserUtm(ctx context.Context, request *model.SaveUserListUTMReq, accountID string, quizID int) (*model.ListSavedIDUTMResp, error) { return nil, nil } func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) { return nil, nil }