251
service/service_kyc/query_kyc.go
Normal file
251
service/service_kyc/query_kyc.go
Normal file
@@ -0,0 +1,251 @@
|
||||
package service_kyc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/internal/kyc"
|
||||
"nixcn-cms/service/shared"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type KycQueryData struct {
|
||||
KycId string `json:"kyc_id"`
|
||||
}
|
||||
|
||||
type KycQueryPayload struct {
|
||||
Context context.Context
|
||||
Data *KycQueryData
|
||||
}
|
||||
|
||||
type KycQueryResponse struct {
|
||||
Status string `json:"status"` // success | pending | failed
|
||||
}
|
||||
|
||||
type KycQueryResult struct {
|
||||
Common shared.CommonResult
|
||||
Data *KycQueryResponse
|
||||
}
|
||||
|
||||
func (self *KycServiceImpl) QueryKyc(payload *KycQueryPayload) (result *KycQueryResult) {
|
||||
var err error
|
||||
|
||||
sessionState, err := kyc.GetSessionState(payload.Context, self.PassportReaderSessionId)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if sessionState == kyc.StateApproved {
|
||||
sessionDetails, err := kyc.GetSessionDetails(payload.Context, self.PassportReaderSessionId)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var kycInfo = kyc.PassportResp{
|
||||
GivenNames: sessionDetails.GivenNames,
|
||||
Surname: sessionDetails.Surname,
|
||||
Nationality: sessionDetails.Nationality,
|
||||
DateOfBirth: sessionDetails.DateOfBirth,
|
||||
DocumentType: sessionDetails.DocumentType,
|
||||
DocumentNumber: sessionDetails.DocumentNumber,
|
||||
ExpiryDate: sessionDetails.ExpiryDate,
|
||||
}
|
||||
|
||||
if kycInfo.DocumentType != "PASSPORT" || kycInfo.DocumentNumber != self.PassportId {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
encodedKycInfo, err := kyc.EncodeAES(kycInfo)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.KycQueryFailed).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
kycId, err := uuid.Parse(payload.Data.KycId)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var updates = map[string]any{
|
||||
"kyc_info": kycInfo,
|
||||
}
|
||||
|
||||
err = new(data.Kyc).
|
||||
SetKycInfo(*encodedKycInfo).
|
||||
UpdateByKycID(payload.Context, &kycId, updates)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorDatabase).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
SetError(nil).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: &KycQueryResponse{
|
||||
Status: "success",
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if sessionState == kyc.StateCreated || sessionState == kyc.StateInitiated || sessionState == kyc.StateCompleted {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
SetError(nil).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: &KycQueryResponse{
|
||||
Status: "pending",
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if sessionState == kyc.StateFailed || sessionState == kyc.StateAborted || sessionState == kyc.StateRejected {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceKyc).
|
||||
SetEndpoint(exception.EndpointKycServiceQuery).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
SetError(nil).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &KycQueryResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: &KycQueryResponse{
|
||||
Status: "failed",
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user