teqo-backup/src/utilities/relationship.ts
Francisco Ignácio Fontoura Solla bcac665cb5 Ship /campanha nuclei MVP with RBAC, invites, and vote estimates.
Adds electoral nuclei, leaderships, updates, and WhatsApp invite flows behind campaign auth, plus the consolidated schema migration that Vercel applies on deploy.
2026-07-18 15:01:56 -03:00

23 lines
790 B
TypeScript

export type PopulatedRelationship = { id: number }
const isRelationshipId = (value: unknown): value is number =>
typeof value === 'number' && Number.isSafeInteger(value) && value > 0
export const isPopulatedRelationship = <Relationship extends PopulatedRelationship>(
value: unknown,
): value is Relationship =>
typeof value === 'object' && value !== null && 'id' in value && isRelationshipId(value.id)
export const relationshipId = (value: unknown): number | null => {
if (isRelationshipId(value)) return value
return isPopulatedRelationship(value) ? value.id : null
}
export const requireRelationshipId = (
value: unknown,
message = 'Relacionamento inválido.',
): number => {
const id = relationshipId(value)
if (id === null) throw new Error(message)
return id
}