99package api
1010
1111import (
12+ "context"
1213 "encoding/json"
1314 "errors"
1415 "fmt"
@@ -20,12 +21,14 @@ import (
2021 "time"
2122
2223 "github.com/fossology/LicenseDb/pkg/db"
24+ email "github.com/fossology/LicenseDb/pkg/email"
25+ template "github.com/fossology/LicenseDb/pkg/email/templetes"
26+ logger "github.com/fossology/LicenseDb/pkg/log"
2327 "github.com/fossology/LicenseDb/pkg/models"
2428 "github.com/fossology/LicenseDb/pkg/utils"
2529 "github.com/fossology/LicenseDb/pkg/validations"
2630 "github.com/gin-gonic/gin"
2731 "github.com/go-playground/validator/v10"
28-
2932 "gorm.io/gorm"
3033)
3134
@@ -308,7 +311,15 @@ func CreateLicense(c *gin.Context) {
308311 c .JSON (http .StatusInternalServerError , er )
309312 return err
310313 }
311-
314+ // send email
315+ if email .Email != nil && email .Email .IsRunning () {
316+ userName := * lic .User .UserName
317+ userEmail := * lic .User .UserEmail
318+ shortName := * lic .Shortname
319+ email .NotifyLicenseCreated (userEmail , userName , shortName )
320+ } else {
321+ logger .LogInfo ("SMTP disabled or not reachable. Skipping email." )
322+ }
312323 res := models.LicenseResponse {
313324 Data : []models.LicenseResponseDTO {lic .ConvertToLicenseResponseDTO ()},
314325 Status : http .StatusCreated ,
@@ -321,6 +332,7 @@ func CreateLicense(c *gin.Context) {
321332
322333 return nil
323334 })
335+
324336}
325337
326338// UpdateLicense Update license with given shortname and create audit and changelog entries.
@@ -451,6 +463,14 @@ func UpdateLicense(c *gin.Context) {
451463 c .JSON (http .StatusInternalServerError , er )
452464 return err
453465 }
466+ if email .Email != nil && email .Email .IsRunning () {
467+ userName := * newLicense .User .UserName
468+ userEmail := * newLicense .User .UserEmail
469+ shortName := * newLicense .Shortname
470+ email .NotifyLicenseUpdated (userEmail , userName , shortName )
471+ } else {
472+ logger .LogInfo ("SMTP disabled or not reachable. Skipping email." )
473+ }
454474
455475 res := models.LicenseResponse {
456476 Data : []models.LicenseResponseDTO {newLicense .ConvertToLicenseResponseDTO ()},
@@ -573,6 +593,7 @@ func SearchInLicense(c *gin.Context) {
573593// @Router /licenses/import [post]
574594func ImportLicenses (c * gin.Context ) {
575595 userId := c .MustGet ("userId" ).(int64 )
596+ var user models.User
576597 file , header , err := c .Request .FormFile ("file" )
577598 if err != nil {
578599 er := models.LicenseError {
@@ -641,8 +662,21 @@ func ImportLicenses(c *gin.Context) {
641662 res := models.ImportLicensesResponse {
642663 Status : http .StatusOK ,
643664 }
644-
665+ var total , success , failed int
666+ err = db .DB .Where (models.User {Id : userId }).First (& user ).Error
667+ if err != nil {
668+ er := models.LicenseError {
669+ Status : http .StatusNotFound ,
670+ Message : fmt .Sprintf ("no with userId '%d' exists" , userId ),
671+ Error : err .Error (),
672+ Path : c .Request .URL .Path ,
673+ Timestamp : time .Now ().Format (time .RFC3339 ),
674+ }
675+ c .JSON (http .StatusNotFound , er )
676+ return
677+ }
645678 for i := range licenses {
679+ total ++
646680 lic , err := licenses [i ].ConvertToLicenseDB ()
647681 if err != nil {
648682 res .Data = append (res .Data , models.LicenseError {
@@ -683,6 +717,37 @@ func ImportLicenses(c *gin.Context) {
683717 })
684718 // error is not returned here as it will rollback the transaction
685719 }
720+ if importStatus == utils .IMPORT_LICENSE_CREATED ||
721+ importStatus == utils .IMPORT_LICENSE_UPDATED ||
722+ importStatus == utils .IMPORT_LICENSE_UPDATED_EXCEPT_TEXT {
723+ success ++
724+ } else {
725+ failed ++
726+ }
727+ }
728+ if email .Email != nil && email .Email .IsRunning () {
729+ userName := * user .UserName
730+ userEmail := * user .UserEmail
731+ subject , html := template .ImportSummaryEmailTemplate (
732+ userName ,
733+ "Licenses" ,
734+ total ,
735+ success ,
736+ failed ,
737+ time .Now (),
738+ )
739+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 2 )
740+ defer cancel ()
741+
742+ if err := email .Email .Queue (ctx , email.EmailData {
743+ To : []string {userEmail },
744+ Subject : subject ,
745+ HTML : html ,
746+ }); err != nil {
747+ logger .LogError ("Failed to enqueue email" )
748+ }
749+ } else {
750+ logger .LogInfo ("SMTP disabled or not reachable. Skipping email." )
686751 }
687752
688753 c .JSON (http .StatusOK , res )
0 commit comments