Gives a live link that has no Sisu person id yet the one the registry resolved its number to,
along with the registry’s names where the link has none.
Checks both unique keys before linking: a student who changed programme keeps their Sisu person
id and gets a new number, so checking the number alone lets the link through and then trips
uq_verified_student_numbers_person as a bare 500. Another account’s hold wins over our own.
Live links only, newest first: a retired link is not a number we hold. Returns the page together
with how many rows match the filters in total, from one query via COUNT(*) OVER().
The account’s most recent link, retired ones included: a retired link is the only record an
unlinked account has of the Sisu person its linking mail was addressed to.
Links each of user_ids to the student number a registrar last reported for them, as a
StudentNumberVerificationMethod::StudyRegistry link, and records a conflict wherever a live
link still stands in the way. A link someone made by hand (the student or an admin) always wins,
but another account’s study-registry link on the reported number moves to the account reported
most recently, unless the holder’s own report of it is newer: a student with two accounts gets
the number on the later one. A retired link does
not block: the registrar’s report is authoritative, so a number the student or an admin unlinked,
or one resolve-person-ids dropped over a conflict, is linked again. Returns how many links were
made.
Retires current_link_id (the account’s link the caller already resolved, if any), inserts new
in its place, clears the mailed links to new’s number that are no longer owed, and audits the
change on the account’s live registrations.