Skip to content

Commit

Permalink
feat: store FCMToken also in fcm_token POST api
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Cappa <[email protected]>
  • Loading branch information
Ks89 committed Dec 27, 2024
1 parent 2a4c831 commit ec55e08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
28 changes: 22 additions & 6 deletions api/fcm_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/net/context"
"net/http"
"os"
"time"
)

// InitFCMTokenReq struct
Expand Down Expand Up @@ -53,18 +54,18 @@ func NewFCMToken(ctx context.Context, logger *zap.SugaredLogger, client *mongo.C
// PostFCMToken function to associate smartphone app with Firebase client to this server via APIToken
// This will be sent to online server to store that data in Redis to be able to send Push Notifications
func (handler *FCMToken) PostFCMToken(c *gin.Context) {
handler.logger.Info("REST - PostFCMToken called")
handler.logger.Info("REST - POST - PostFCMToken called")

var initFCMTokenBody InitFCMTokenReq
if err := c.ShouldBindJSON(&initFCMTokenBody); err != nil {
handler.logger.Errorf("REST - PostFCMToken - Cannot bind request body. Err = %v\n", err)
handler.logger.Errorf("REST - POST - PostFCMToken - Cannot bind request body. Err = %v\n", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request payload"})
return
}

err := handler.validate.Struct(initFCMTokenBody)
if err != nil {
handler.logger.Errorf("REST - PostFCMToken - request body is not valid, err %#v", err)
handler.logger.Errorf("REST - POST - PostFCMToken - request body is not valid, err %#v", err)
var errFields = utils.GetErrorMessage(err)
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body, these fields are not valid:" + errFields})
return
Expand All @@ -76,16 +77,31 @@ func (handler *FCMToken) PostFCMToken(c *gin.Context) {
"apiToken": initFCMTokenBody.APIToken,
}).Decode(&profileFound)
if errProfile != nil {
handler.logger.Errorf("REST - PostFCMToken - Cannot find profile with that apiToken. Err = %v\n", errProfile)
handler.logger.Errorf("REST - POST - PostFCMToken - Cannot find profile with that apiToken. Err = %v\n", errProfile)
c.JSON(http.StatusBadRequest, gin.H{"error": "cannot initialize FCM Token, profile token missing or not valid"})
return
}

// store FCM Token also on profile
_, err = handler.collProfiles.UpdateOne(handler.ctx, bson.M{
"_id": profileFound.ID,
}, bson.M{
"$set": bson.M{
"fcmToken": initFCMTokenBody.FCMToken,
"modifiedAt": time.Now(),
},
})
if err != nil {
handler.logger.Error("REST - POST - PostFCMToken - Cannot update profile with fcmToken")
c.JSON(http.StatusInternalServerError, gin.H{"error": "cannot update profile with fcmToken"})
return
}

err = handler.initFCMTokenViaHTTP(&initFCMTokenBody)
if err != nil {
handler.logger.Errorf("REST - PostFCMToken - cannot initialize FCM Token via HTTP. Err %v\n", err)
handler.logger.Errorf("REST - POST - PostFCMToken - cannot initialize FCM Token via HTTP. Err %v\n", err)
if re, ok := err.(*customerrors.ErrorWrapper); ok {
handler.logger.Errorf("REST - PostFCMToken - cannot initialize FCM Token with status = %d, message = %s\n", re.Code, re.Message)
handler.logger.Errorf("REST - POST - PostFCMToken - cannot initialize FCM Token with status = %d, message = %s\n", re.Code, re.Message)
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Cannot initialize FCM Token"})
return
Expand Down
1 change: 1 addition & 0 deletions api/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (handler *Profiles) PostProfilesAPIToken(c *gin.Context) {
}

// PostProfilesFCMToken function to store the Firebase Cloud Messaging Token
// this api is unused, because I set FCM Token on profile while calling fcm_token POST API
func (handler *Profiles) PostProfilesFCMToken(c *gin.Context) {
handler.logger.Info("REST - POST - PostProfilesFCMToken called")

Expand Down

0 comments on commit ec55e08

Please sign in to comment.