* [MM-45564] - Notify Admin v2 * add dummy data * update dummy data * add store methods * experiment with recurring task * complete saving of the notification * make improvements * make improvements * add store layer tests * fix lint * update store layer tests * add app layer unit tests * add store layers * add app layer tests * fix lint * fix lint * fix tests * fix tests lint * fix lint * fix lint * fix retry layer test * add notifications manual trigger * filter notifications based on current plan * add test case * temp change * feedback impl * fix translations * change job scheduler * refactor job * fix store layer tests * extract i18n * fix lint * fix translations * fix translations * add license statement for new file * feedback impl-2 * fix lint * update make file * add intl ids * improve * fix lint * feedback impl * move code and rename files * fix lint * feedback impl * add config for trigger notifications api * fix tests * tmp change * undo temp changes Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
88 строки
2.7 KiB
Go
88 строки
2.7 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package sqlstore
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
"github.com/mattermost/mattermost-server/v6/store"
|
|
sq "github.com/mattermost/squirrel"
|
|
)
|
|
|
|
type SqlNotifyAdminStore struct {
|
|
*SqlStore
|
|
}
|
|
|
|
func newSqlNotifyAdminStore(sqlStore *SqlStore) store.NotifyAdminStore {
|
|
return &SqlNotifyAdminStore{sqlStore}
|
|
}
|
|
|
|
func (s SqlNotifyAdminStore) insert(data *model.NotifyAdminData) (sql.Result, error) {
|
|
query := `INSERT INTO NotifyAdmin (UserId, CreateAt, RequiredPlan, RequiredFeature, Trial) VALUES (:UserId, :CreateAt, :RequiredPlan, :RequiredFeature, :Trial)`
|
|
return s.GetMasterX().NamedExec(query, data)
|
|
}
|
|
|
|
func (s SqlNotifyAdminStore) Save(data *model.NotifyAdminData) (*model.NotifyAdminData, error) {
|
|
if err := data.IsValid(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
data.PreSave()
|
|
|
|
_, err := s.insert(data)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to save Notify Admin data")
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
func (s SqlNotifyAdminStore) GetDataByUserIdAndFeature(userId string, feature model.MattermostPaidFeature) ([]*model.NotifyAdminData, error) {
|
|
data := []*model.NotifyAdminData{}
|
|
query, args, err := s.getQueryBuilder().
|
|
Select("*").
|
|
From("NotifyAdmin").
|
|
Where(sq.Eq{"UserId": userId, "RequiredFeature": feature}).
|
|
ToSql()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not build sql query to get all notifcation data by user id and required feature")
|
|
}
|
|
|
|
if err := s.GetReplicaX().Select(&data, query, args...); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, store.NewErrNotFound("NotifyAdmin", fmt.Sprintf("user id: %s and required feature: %s", userId, feature))
|
|
}
|
|
return nil, errors.Wrapf(err, "notifcation data by user id: %s and required feature: %s", userId, feature)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func (s SqlNotifyAdminStore) Get(trial bool) ([]*model.NotifyAdminData, error) {
|
|
data := []*model.NotifyAdminData{}
|
|
query, args, err := s.getQueryBuilder().
|
|
Select("*").
|
|
From("NotifyAdmin").
|
|
Where(sq.Eq{"trial": trial}).
|
|
ToSql()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not build sql query to get all notifcation data")
|
|
}
|
|
|
|
if err := s.GetReplicaX().Select(&data, query, args...); err != nil {
|
|
return nil, errors.Wrap(err, "notifcation data")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func (s SqlNotifyAdminStore) DeleteBefore(trial bool, now int64) error {
|
|
if _, err := s.GetMasterX().Exec("DELETE FROM NotifyAdmin WHERE trial = ? AND createat < ?", trial, now); err != nil {
|
|
return errors.Wrapf(err, "failed to remove all notification data with trial=%t", trial)
|
|
}
|
|
return nil
|
|
}
|