Order signature by people full name
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -39,6 +39,13 @@ type (
|
||||
}
|
||||
|
||||
DocumentVersionSignatures []*DocumentVersionSignature
|
||||
|
||||
DocumentVersionSignatureWithPeople struct {
|
||||
DocumentVersionSignature
|
||||
SignedByFullName string `db:"signed_by_full_name"`
|
||||
}
|
||||
|
||||
DocumentVersionSignaturesWithPeople []*DocumentVersionSignatureWithPeople
|
||||
)
|
||||
|
||||
func (pvs DocumentVersionSignature) CursorKey(orderBy DocumentVersionSignatureOrderField) page.CursorKey {
|
||||
@@ -298,3 +305,72 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pvss *DocumentVersionSignaturesWithPeople) LoadByDocumentVersionIDWithPeople(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
documentVersionID gid.GID,
|
||||
limit int,
|
||||
) error {
|
||||
q := `
|
||||
WITH sigs AS (
|
||||
SELECT
|
||||
dvs.id,
|
||||
dvs.tenant_id,
|
||||
dvs.document_version_id,
|
||||
dvs.state,
|
||||
dvs.signed_by,
|
||||
dvs.signed_at,
|
||||
dvs.requested_at,
|
||||
dvs.created_at,
|
||||
dvs.updated_at,
|
||||
p.full_name as signed_by_full_name
|
||||
FROM
|
||||
document_version_signatures dvs
|
||||
INNER JOIN
|
||||
peoples p ON dvs.signed_by = p.id
|
||||
WHERE
|
||||
dvs.document_version_id = @document_version_id
|
||||
ORDER BY
|
||||
p.full_name ASC
|
||||
LIMIT @limit
|
||||
)
|
||||
SELECT
|
||||
id,
|
||||
document_version_id,
|
||||
state,
|
||||
signed_by,
|
||||
signed_at,
|
||||
requested_at,
|
||||
created_at,
|
||||
updated_at,
|
||||
signed_by_full_name
|
||||
FROM
|
||||
sigs
|
||||
WHERE
|
||||
%s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"document_version_id": documentVersionID,
|
||||
"limit": limit,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query document version signatures with people: %w", err)
|
||||
}
|
||||
|
||||
signatures, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[DocumentVersionSignatureWithPeople])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect document version signatures with people: %w", err)
|
||||
}
|
||||
|
||||
*pvss = signatures
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1354,8 +1354,6 @@ func exportDocumentPDF(
|
||||
version := &coredata.DocumentVersion{}
|
||||
owner := &coredata.People{}
|
||||
organization := &coredata.Organization{}
|
||||
signatures := coredata.DocumentVersionSignatures{}
|
||||
peopleMap := make(map[gid.GID]*coredata.People)
|
||||
|
||||
if err := version.LoadByID(ctx, conn, scope, documentVersionID); err != nil {
|
||||
return nil, fmt.Errorf("cannot load document version: %w", err)
|
||||
@@ -1375,35 +1373,15 @@ func exportDocumentPDF(
|
||||
|
||||
var signatureData []docgen.SignatureData
|
||||
if options.WithSignatures {
|
||||
cursor := page.NewCursor(
|
||||
1_000,
|
||||
nil,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.DocumentVersionSignatureOrderField]{
|
||||
Field: coredata.DocumentVersionSignatureOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
if err := signatures.LoadByDocumentVersionID(ctx, conn, scope, documentVersionID, cursor); err != nil {
|
||||
signaturesWithPeople := &coredata.DocumentVersionSignaturesWithPeople{}
|
||||
if err := signaturesWithPeople.LoadByDocumentVersionIDWithPeople(ctx, conn, scope, documentVersionID, 1_000); err != nil {
|
||||
return nil, fmt.Errorf("cannot load document version signatures: %w", err)
|
||||
}
|
||||
|
||||
// TODO: refactor this to use a single query
|
||||
for _, sig := range signatures {
|
||||
if _, ok := peopleMap[sig.SignedBy]; !ok {
|
||||
people := &coredata.People{}
|
||||
if err := people.LoadByID(ctx, conn, scope, sig.SignedBy); err != nil {
|
||||
return nil, fmt.Errorf("cannot load people %q: %w", sig.SignedBy, err)
|
||||
}
|
||||
peopleMap[sig.SignedBy] = people
|
||||
}
|
||||
}
|
||||
|
||||
signatureData = make([]docgen.SignatureData, len(signatures))
|
||||
for i, sig := range signatures {
|
||||
signatureData = make([]docgen.SignatureData, len(*signaturesWithPeople))
|
||||
for i, sig := range *signaturesWithPeople {
|
||||
signatureData[i] = docgen.SignatureData{
|
||||
SignedBy: peopleMap[sig.SignedBy].FullName,
|
||||
SignedBy: sig.SignedByFullName,
|
||||
SignedAt: sig.SignedAt,
|
||||
State: sig.State,
|
||||
RequestedAt: sig.RequestedAt,
|
||||
|
||||
Reference in New Issue
Block a user