fix: shortlink generation logic update/create
This commit is contained in:
@@ -27,6 +27,7 @@ type GenerateShortLinkResponse struct {
|
||||
ShortToken string `json:"short_token"`
|
||||
FullURL string `json:"full_url"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
IsExisting bool `json:"is_existing"` // Indicates if this is an existing link that was reused
|
||||
}
|
||||
|
||||
// ShortLinkAuthRequest represents the shortlink authentication request
|
||||
|
||||
@@ -139,3 +139,28 @@ func (r *ShortLinkRepository) UpdateShortLink(shortLink *models.ShortLink) error
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetActiveShortLinkByPatientAndStudy retrieves an active (unexpired, not revoked) shortlink
|
||||
// for the given patient ID and study UID
|
||||
func (r *ShortLinkRepository) GetActiveShortLinkByPatientAndStudy(patientID string, studyUID string) (*models.ShortLink, error) {
|
||||
var dbShortLink DBShortLink
|
||||
|
||||
query := `SELECT * FROM shortlink
|
||||
WHERE Shortlink_PatientID = ?
|
||||
AND Shortlink_Study_IUID = ?
|
||||
AND ShortlinkExpiredAt > NOW()
|
||||
AND ShortlinkIsRevoked = FALSE
|
||||
ORDER BY ShortlinkExpiredAt DESC
|
||||
LIMIT 1`
|
||||
|
||||
err := database.DB.Get(&dbShortLink, query, patientID, studyUID)
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("database error getting active shortlink: %w", err)
|
||||
}
|
||||
|
||||
return dbShortLink.ToShortLink(), nil
|
||||
}
|
||||
|
||||
@@ -94,6 +94,31 @@ func (s *ShortLinkService) GenerateShortLink(req *models.GenerateShortLinkReques
|
||||
return nil, errors.New("invalid date of birth format, expected YYYY-MM-DD")
|
||||
}
|
||||
|
||||
// Check if an unexpired shortlink already exists for this patient and study
|
||||
existingShortLink, err := s.shortLinkRepo.GetActiveShortLinkByPatientAndStudy(req.PatientID, req.StudyUID)
|
||||
if err != nil {
|
||||
s.logger.Error("Error checking for existing shortlinks", zap.Error(err))
|
||||
return nil, ErrCreationFailed
|
||||
}
|
||||
|
||||
// If an active shortlink exists, return it instead of creating a new one
|
||||
if existingShortLink != nil {
|
||||
s.logger.Info("Returning existing active shortlink",
|
||||
zap.String("patientID", req.PatientID),
|
||||
zap.String("studyUID", req.StudyUID),
|
||||
zap.String("token", existingShortLink.Token))
|
||||
|
||||
// Generate the full URL using the configured base URL
|
||||
fullURL := fmt.Sprintf("%s/short-auth?short=%s", s.baseURL, existingShortLink.Token)
|
||||
|
||||
return &models.GenerateShortLinkResponse{
|
||||
ShortToken: existingShortLink.Token,
|
||||
FullURL: fullURL,
|
||||
ExpiresAt: existingShortLink.ExpiresAt,
|
||||
IsExisting: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Set expiration if not provided
|
||||
expiresIn := s.defaultExpiryTime
|
||||
if req.ExpiresIn > 0 {
|
||||
@@ -139,6 +164,7 @@ func (s *ShortLinkService) GenerateShortLink(req *models.GenerateShortLinkReques
|
||||
ShortToken: token,
|
||||
FullURL: fullURL,
|
||||
ExpiresAt: shortLink.ExpiresAt,
|
||||
IsExisting: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user