edit: field Shortcode dan expire duration
This commit is contained in:
@@ -3,7 +3,7 @@ package models
|
||||
// ShortLink represents a short URL token for patient access
|
||||
type ShortLink struct {
|
||||
ID string `db:"id" json:"id"`
|
||||
Token string `db:"token" json:"token"` // The short token used in the URL
|
||||
Shortcode string `db:"shortcode" json:"shortcode"` // The short token used in the URL
|
||||
PatientID string `db:"patient_id" json:"patient_id"`
|
||||
StudyUID string `db:"study_uid" json:"study_uid"` // The StudyInstanceUID this token grants access to
|
||||
HashedDOB string `db:"hashed_dob" json:"-"` // Hashed Date of Birth for verification
|
||||
@@ -26,6 +26,7 @@ type GenerateShortLinkRequest struct {
|
||||
type GenerateShortLinkResponse struct {
|
||||
ShortToken string `json:"short_token"`
|
||||
FullURL string `json:"full_url"`
|
||||
URI string `json:"uri"` // The URI path and query without the base URL
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
IsExisting bool `json:"is_existing"` // Indicates if this is an existing link that was reused
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func (s *DBShortLink) ToShortLink() *models.ShortLink {
|
||||
|
||||
return &models.ShortLink{
|
||||
ID: fmt.Sprintf("%d", s.ShortlinkID),
|
||||
Token: s.ShortlinkCode,
|
||||
Shortcode: s.ShortlinkCode,
|
||||
PatientID: s.Shortlink_PatientID,
|
||||
StudyUID: s.Shortlink_Study_IUID,
|
||||
HashedDOB: s.ShortlinkHashDoB,
|
||||
@@ -113,7 +113,7 @@ func (r *ShortLinkRepository) CreateShortLinkTx(tx *sqlx.Tx, shortLink *models.S
|
||||
|
||||
_, err = tx.Exec(
|
||||
query,
|
||||
shortLink.Token,
|
||||
shortLink.Shortcode,
|
||||
shortLink.PatientID,
|
||||
shortLink.StudyUID,
|
||||
shortLink.HashedDOB,
|
||||
@@ -148,7 +148,7 @@ func (r *ShortLinkRepository) UpdateShortLinkTx(tx *sqlx.Tx, shortLink *models.S
|
||||
shortLink.IsRevoked,
|
||||
shortLink.RemainingTries,
|
||||
expiresAt,
|
||||
shortLink.Token,
|
||||
shortLink.Shortcode,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -110,14 +110,16 @@ func (s *ShortLinkService) GenerateShortLink(req *models.GenerateShortLinkReques
|
||||
s.logger.Info("Returning existing active shortlink",
|
||||
zap.String("patientID", req.PatientID),
|
||||
zap.String("studyUID", req.StudyUID),
|
||||
zap.String("token", existingShortLink.Token))
|
||||
zap.String("token", existingShortLink.Shortcode))
|
||||
|
||||
// Generate the full URL using the configured base URL
|
||||
fullURL := fmt.Sprintf("%s/short-auth?short=%s", s.baseURL, existingShortLink.Token)
|
||||
// Generate the full URL and URI
|
||||
uri := fmt.Sprintf("short-auth?short=%s", existingShortLink.Shortcode)
|
||||
fullURL := fmt.Sprintf("%s/%s", s.baseURL, uri)
|
||||
|
||||
return &models.GenerateShortLinkResponse{
|
||||
ShortToken: existingShortLink.Token,
|
||||
ShortToken: existingShortLink.Shortcode,
|
||||
FullURL: fullURL,
|
||||
URI: uri,
|
||||
ExpiresAt: existingShortLink.ExpiresAt,
|
||||
IsExisting: true,
|
||||
}, nil
|
||||
@@ -161,7 +163,7 @@ func (s *ShortLinkService) GenerateShortLink(req *models.GenerateShortLinkReques
|
||||
|
||||
// Create the short link record
|
||||
shortLink := &models.ShortLink{
|
||||
Token: *unusedShortcode,
|
||||
Shortcode: *unusedShortcode,
|
||||
PatientID: req.PatientID,
|
||||
StudyUID: req.StudyUID,
|
||||
HashedDOB: hashedDOB,
|
||||
@@ -181,14 +183,14 @@ func (s *ShortLinkService) GenerateShortLink(req *models.GenerateShortLinkReques
|
||||
|
||||
// Get the ID of the created shortlink
|
||||
var shortlinkID int
|
||||
err = tx.Get(&shortlinkID, "SELECT ShortlinkID FROM shortlink WHERE ShortlinkCode = ?", shortLink.Token)
|
||||
err = tx.Get(&shortlinkID, "SELECT ShortlinkID FROM shortlink WHERE ShortlinkCode = ?", shortLink.Shortcode)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get shortlink ID", zap.Error(err))
|
||||
return nil, ErrCreationFailed
|
||||
}
|
||||
|
||||
// Mark the shortcode as used
|
||||
err = s.shortCodeRepo.MarkShortCodeAsUsed(tx, shortLink.Token, shortlinkID)
|
||||
err = s.shortCodeRepo.MarkShortCodeAsUsed(tx, shortLink.Shortcode, shortlinkID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to mark shortcode as used", zap.Error(err))
|
||||
return nil, ErrCreationFailed
|
||||
@@ -203,12 +205,14 @@ func (s *ShortLinkService) GenerateShortLink(req *models.GenerateShortLinkReques
|
||||
// Clear the tx to prevent the deferred rollback
|
||||
tx = nil
|
||||
|
||||
// Generate the full URL using the configured base URL
|
||||
fullURL := fmt.Sprintf("%s/short-auth?short=%s", s.baseURL, shortLink.Token)
|
||||
// Generate the full URL and URI
|
||||
uri := fmt.Sprintf("short-auth?short=%s", shortLink.Shortcode)
|
||||
fullURL := fmt.Sprintf("%s/%s", s.baseURL, uri)
|
||||
|
||||
return &models.GenerateShortLinkResponse{
|
||||
ShortToken: shortLink.Token,
|
||||
ShortToken: shortLink.Shortcode,
|
||||
FullURL: fullURL,
|
||||
URI: uri,
|
||||
ExpiresAt: shortLink.ExpiresAt,
|
||||
IsExisting: false,
|
||||
}, nil
|
||||
|
||||
Reference in New Issue
Block a user