52 lines
1.8 KiB
Go
52 lines
1.8 KiB
Go
package sendico
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.sendico.io/tech/sxchnge/product/identity"
|
|
merrors "git.sendico.io/tech/sxchnge/shared/errors"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
// Only consumed wire fields are owned here; never import platform modules.
|
|
type factsDTO struct {
|
|
FullName string `json:"full_name"`
|
|
Email string `json:"email"`
|
|
CustomerRef string `json:"external_customer_ref"`
|
|
}
|
|
type submissionDTO struct {
|
|
SubjectRef string `json:"subject_ref"`
|
|
SourceRef string `json:"source_submission_ref"`
|
|
Facts factsDTO `json:"facts"`
|
|
}
|
|
type receiptDTO struct {
|
|
SubjectRef string `json:"subject_ref"`
|
|
RegisteredAt time.Time `json:"registered_at"`
|
|
SubmissionRef string `json:"submission_ref"`
|
|
AcceptedAt time.Time `json:"accepted_at"`
|
|
}
|
|
type envelopeDTO struct {
|
|
Status string `json:"status"`
|
|
Data receiptDTO `json:"data"`
|
|
}
|
|
|
|
func validRef(ref string) bool {
|
|
id, err := bson.ObjectIDFromHex(ref)
|
|
return err == nil && !id.IsZero() && id.Hex() == ref
|
|
}
|
|
func mapFacts(subject, source string, facts identity.Facts) submissionDTO {
|
|
return submissionDTO{SubjectRef: subject, SourceRef: source, Facts: factsDTO{FullName: facts.FullName, Email: facts.Email, CustomerRef: facts.CustomerRef}}
|
|
}
|
|
func mapSubject(dto receiptDTO) (string, error) {
|
|
if !validRef(dto.SubjectRef) || dto.RegisteredAt.IsZero() {
|
|
return "", merrors.ServiceUnavailable("invalid Sendico registration receipt")
|
|
}
|
|
return dto.SubjectRef, nil
|
|
}
|
|
func mapReceipt(dto receiptDTO, subject string) (identity.Receipt, error) {
|
|
if !validRef(dto.SubmissionRef) || dto.SubjectRef != subject || dto.AcceptedAt.IsZero() {
|
|
return identity.Receipt{}, merrors.ServiceUnavailable("invalid Sendico intake receipt")
|
|
}
|
|
return identity.Receipt{SubmissionRef: dto.SubmissionRef, SubjectRef: dto.SubjectRef, AcceptedAt: dto.AcceptedAt}, nil
|
|
}
|