Skip to content

Commit c946f12

Browse files
authored
feat: implement update admin notification (#2721)
1 parent bf965df commit c946f12

22 files changed

Lines changed: 2092 additions & 1036 deletions

api-description/web-api.swagger.yaml

Lines changed: 272 additions & 257 deletions
Large diffs are not rendered by default.

pkg/notification/api/api.go

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ func (s *NotificationService) ListDraftAdminNotifications(
187187
}, nil
188188
}
189189

190-
func (s *NotificationService) CreateNotification(
190+
func (s *NotificationService) CreateAdminNotification(
191191
ctx context.Context,
192-
req *proto.CreateNotificationRequest,
193-
) (*proto.CreateNotificationResponse, error) {
192+
req *proto.CreateAdminNotificationRequest,
193+
) (*proto.CreateAdminNotificationResponse, error) {
194194
editor, err := s.checkSystemAdminRole(ctx)
195195
if err != nil {
196196
return nil, err
197197
}
198-
if err := validateCreateNotificationRequest(req); err != nil {
198+
if err := validateCreateAdminNotificationRequest(req); err != nil {
199199
s.logger.Error(
200200
"Failed to validate create notification request",
201201
log.FieldsFromIncomingContext(ctx).AddFields(zap.Error(err))...,
@@ -211,7 +211,7 @@ func (s *NotificationService) CreateNotification(
211211
return nil, api.NewGRPCStatus(err).Err()
212212
}
213213
err = s.dbClient.RunInTransactionV2(ctx, func(ctxWithTx context.Context) error {
214-
return s.notificationStorage.CreateNotification(ctxWithTx, notification)
214+
return s.notificationStorage.CreateAdminNotification(ctxWithTx, notification)
215215
})
216216
if err != nil {
217217
if errors.Is(err, storage.ErrNotificationAlreadyExists) {
@@ -226,17 +226,21 @@ func (s *NotificationService) CreateNotification(
226226
)
227227
return nil, api.NewGRPCStatus(err).Err()
228228
}
229-
return &proto.CreateNotificationResponse{
229+
return &proto.CreateAdminNotificationResponse{
230230
Notification: notification.Notification,
231231
}, nil
232232
}
233233

234-
func validateCreateNotificationRequest(req *proto.CreateNotificationRequest) error {
235-
if len(req.Localizations) == 0 {
234+
func validateCreateAdminNotificationRequest(req *proto.CreateAdminNotificationRequest) error {
235+
return validateLocalizations(req.Localizations)
236+
}
237+
238+
func validateLocalizations(localizations []*proto.NotificationLocalization) error {
239+
if len(localizations) == 0 {
236240
return statusLocalizationRequired.Err()
237241
}
238-
languages := make(map[string]struct{}, len(req.Localizations))
239-
for _, l := range req.Localizations {
242+
languages := make(map[string]struct{}, len(localizations))
243+
for _, l := range localizations {
240244
l.Language = strings.TrimSpace(l.Language)
241245
l.Title = strings.TrimSpace(l.Title)
242246
if l.Language == "" {
@@ -256,23 +260,72 @@ func validateCreateNotificationRequest(req *proto.CreateNotificationRequest) err
256260
return nil
257261
}
258262

259-
func (s *NotificationService) UpdateNotification(
263+
func (s *NotificationService) UpdateAdminNotification(
260264
ctx context.Context,
261-
req *proto.UpdateNotificationRequest,
262-
) (*proto.UpdateNotificationResponse, error) {
263-
return nil, statusNotImplemented
265+
req *proto.UpdateAdminNotificationRequest,
266+
) (*proto.UpdateAdminNotificationResponse, error) {
267+
editor, err := s.checkSystemAdminRole(ctx)
268+
if err != nil {
269+
return nil, err
270+
}
271+
if err := validateUpdateAdminNotificationRequest(req); err != nil {
272+
s.logger.Error(
273+
"Failed to validate update notification request",
274+
log.FieldsFromIncomingContext(ctx).AddFields(zap.Error(err))...,
275+
)
276+
return nil, err
277+
}
278+
var notification *domain.Notification
279+
err = s.dbClient.RunInTransactionV2(ctx, func(ctxWithTx context.Context) error {
280+
var err error
281+
notification, err = s.notificationStorage.GetAdminNotification(ctxWithTx, req.Id)
282+
if err != nil {
283+
return err
284+
}
285+
if notification.Status != proto.Notification_DRAFT {
286+
return statusNotificationAlreadyPublished.Err()
287+
}
288+
notification.Update(editor.Email, req.Localizations)
289+
return s.notificationStorage.UpdateAdminNotification(ctxWithTx, notification)
290+
})
291+
if err != nil {
292+
if errors.Is(err, storage.ErrNotificationNotFound) {
293+
return nil, statusNotificationNotFound.Err()
294+
}
295+
if errors.Is(err, statusNotificationAlreadyPublished.Err()) {
296+
return nil, statusNotificationAlreadyPublished.Err()
297+
}
298+
s.logger.Error(
299+
"Failed to update notification",
300+
log.FieldsFromIncomingContext(ctx).AddFields(
301+
zap.Error(err),
302+
zap.String("notificationId", req.Id),
303+
)...,
304+
)
305+
return nil, api.NewGRPCStatus(err).Err()
306+
}
307+
return &proto.UpdateAdminNotificationResponse{
308+
Notification: notification.Notification,
309+
}, nil
310+
}
311+
312+
func validateUpdateAdminNotificationRequest(req *proto.UpdateAdminNotificationRequest) error {
313+
if len(strings.TrimSpace(req.Id)) == 0 {
314+
return statusNotificationIDRequired.Err()
315+
}
316+
return validateLocalizations(req.Localizations)
264317
}
265318

266-
func (s *NotificationService) PublishNotification(
319+
func (s *NotificationService) PublishAdminNotification(
267320
ctx context.Context,
268-
req *proto.PublishNotificationRequest,
269-
) (*proto.PublishNotificationResponse, error) {
321+
req *proto.PublishAdminNotificationRequest,
322+
) (*proto.PublishAdminNotificationResponse, error) {
270323
return nil, statusNotImplemented
271324
}
272325

273-
func (s *NotificationService) DeleteNotification(
326+
func (s *NotificationService) DeleteAdminNotification(
274327
ctx context.Context,
275-
req *proto.DeleteNotificationRequest,
276-
) (*proto.DeleteNotificationResponse, error) {
328+
req *proto.DeleteAdminNotificationRequest,
329+
) (*proto.DeleteAdminNotificationResponse, error) {
277330
return nil, statusNotImplemented
278331
}

0 commit comments

Comments
 (0)