Skip to content

Commit 23a8795

Browse files
committed
notification smtp configuration api integration
1 parent 93c3f3d commit 23a8795

File tree

19 files changed

+506
-503
lines changed

19 files changed

+506
-503
lines changed

api/internal/features/notification/controller/add_smtp.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ func (c *NotificationController) AddSmtp(w http.ResponseWriter, r *http.Request)
3333
return
3434
}
3535

36-
err := c.service.AddSmtp(SMTPConfigs)
36+
userAny := r.Context().Value(shared_types.UserContextKey)
37+
user, ok := userAny.(*shared_types.User)
38+
39+
if !ok {
40+
c.logger.Log(logger.Error, shared_types.ErrFailedToGetUserFromContext.Error(), shared_types.ErrFailedToGetUserFromContext.Error())
41+
utils.SendErrorResponse(w, shared_types.ErrFailedToGetUserFromContext.Error(), http.StatusInternalServerError)
42+
return
43+
}
44+
45+
err := c.service.AddSmtp(SMTPConfigs,user.ID)
3746
if err != nil {
3847
c.logger.Log(logger.Error, err.Error(), "")
3948
utils.SendErrorResponse(w, err.Error(), http.StatusInternalServerError)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package service
22

33
import (
4+
"github.com/google/uuid"
45
"github.com/raghavyuva/nixopus-api/internal/features/logger"
56
"github.com/raghavyuva/nixopus-api/internal/features/notification"
67
)
78

8-
func (s *NotificationService) AddSmtp(SMTPConfigs notification.CreateSMTPConfigRequest) error {
9+
func (s *NotificationService) AddSmtp(SMTPConfigs notification.CreateSMTPConfigRequest, userID uuid.UUID) error {
910
s.logger.Log(logger.Info, "Adding SMTP configuration", "")
10-
config := notification.NewSMTPConfig(&SMTPConfigs)
11+
config := notification.NewSMTPConfig(&SMTPConfigs, userID)
1112
return s.storage.AddSmtp(config)
1213
}

api/internal/features/notification/storage/init.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func (s NotificationStorage) AddSmtp(config *shared_types.SMTPConfigs) error {
2020
}
2121

2222
func (s NotificationStorage) UpdateSmtp(config *notification.UpdateSMTPConfigRequest) error {
23-
var smtp shared_types.SMTPConfigs
23+
var smtp *shared_types.SMTPConfigs
2424
_, err := s.DB.NewUpdate().Model(smtp).
25-
SetColumn("host", config.Host).
26-
SetColumn("port", strconv.Itoa(config.Port)).
27-
SetColumn("username", config.Username).
28-
SetColumn("password", config.Password).
29-
SetColumn("from_name", config.FromName).
30-
SetColumn("from_email", config.FromEmail).
25+
Set("host = ?", config.Host).
26+
Set("port = ?", strconv.Itoa(config.Port)).
27+
Set("username = ?", config.Username).
28+
Set("password = ?", config.Password).
29+
Set("from_name = ?", config.FromName).
30+
Set("from_email = ?", config.FromEmail).
3131
Where("id = ?", config.ID).Exec(s.Ctx)
3232
return err
3333
}

api/internal/features/notification/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ type CreateSMTPConfigRequest struct {
110110
Password string `json:"password"`
111111
FromName string `json:"from_name"`
112112
FromEmail string `json:"from_email"`
113-
UserID uuid.UUID `json:"user_id"`
114113
}
115114

116115
type UpdateSMTPConfigRequest struct {
@@ -140,7 +139,7 @@ var (
140139
ErrMissingID = errors.New("id is required")
141140
)
142141

143-
func NewSMTPConfig(c *CreateSMTPConfigRequest) *shared_types.SMTPConfigs {
142+
func NewSMTPConfig(c *CreateSMTPConfigRequest,userID uuid.UUID) *shared_types.SMTPConfigs {
144143
if c.FromEmail == "" {
145144
c.FromEmail = c.Username
146145
}
@@ -155,6 +154,7 @@ func NewSMTPConfig(c *CreateSMTPConfigRequest) *shared_types.SMTPConfigs {
155154
Password: c.Password,
156155
FromName: c.FromName,
157156
FromEmail: c.FromEmail,
158-
UserID: c.UserID,
157+
UserID: userID,
158+
ID: uuid.New(),
159159
}
160160
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
useCreateSMPTConfigurationMutation,
3+
useGetSMTPConfigurationsQuery,
4+
useUpdateSMTPConfigurationMutation
5+
} from '@/redux/services/settings/notification';
6+
import { CreateSMTPConfigRequest, UpdateSMTPConfigRequest } from '@/redux/types/notification';
7+
import { toast } from 'sonner';
8+
9+
function useNotificationSettings() {
10+
const { data: smtpConfigs, isLoading, error } = useGetSMTPConfigurationsQuery();
11+
const [createSMTPConfiguration, { isLoading: isCreating }] = useCreateSMPTConfigurationMutation();
12+
const [updateSMTPConfiguration, { isLoading: isUpdating }] = useUpdateSMTPConfigurationMutation();
13+
14+
const handleCreateSMTPConfiguration = async (data: CreateSMTPConfigRequest) => {
15+
await createSMTPConfiguration(data);
16+
};
17+
18+
const handleUpdateSMTPConfiguration = async (data: UpdateSMTPConfigRequest) => {
19+
await updateSMTPConfiguration(data);
20+
};
21+
22+
const handleOnSave = async (data: any) => {
23+
try {
24+
const smtpConfig = {
25+
host: data.smtpServer,
26+
port: parseInt(data.port),
27+
username: data.username,
28+
password: data.password,
29+
from_email: data.fromEmail,
30+
from_name: data.fromName
31+
}
32+
if (smtpConfigs?.id) {
33+
await handleUpdateSMTPConfiguration({ ...smtpConfig, id: smtpConfigs?.id, });
34+
} else {
35+
await handleCreateSMTPConfiguration(smtpConfig);
36+
}
37+
toast.success('Email configuration saved successfully');
38+
} catch (error) {
39+
toast.error('Failed to save email configuration');
40+
}
41+
};
42+
43+
return {
44+
smtpConfigs,
45+
isLoading,
46+
error,
47+
isCreating,
48+
isUpdating,
49+
handleOnSave
50+
};
51+
}
52+
53+
export default useNotificationSettings;

0 commit comments

Comments
 (0)