mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-27 14:23:05 +00:00
- Collections Notification e PushSubscription com migration - Sino no header (mobile + desktop) com drawer de avisos lidos/não lidos - Hooks em municipalityUpdate, supporter, activity e convite aceito - Push Web via web-push + handlers no service worker /campanha/sw.js - Opt-in LGPD em /campanha/perfil (chave campanha-notificacoes-push) - Testes unit/int de contrato e access Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
69 lines
2 KiB
TypeScript
69 lines
2 KiB
TypeScript
import 'server-only'
|
|
|
|
/** Trusted server reads in this module use admin bypass (`overrideAccess: true`). */
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import { uniqueRelationshipIds } from '@/lib/relationship'
|
|
|
|
import type { NotificationWriteRequest } from '@/utilities/notification/createCampaignNotification'
|
|
|
|
const UNRESTRICTED_ROLES = ['coordinator', 'candidate'] as const
|
|
|
|
const loadUnrestrictedStaffUserIds = async (
|
|
payload: Payload,
|
|
req?: NotificationWriteRequest,
|
|
): Promise<number[]> => {
|
|
const result = await payload.find({
|
|
collection: 'campaignUser',
|
|
where: { role: { in: [...UNRESTRICTED_ROLES] } },
|
|
depth: 0,
|
|
limit: 200,
|
|
pagination: false,
|
|
overrideAccess: true,
|
|
req,
|
|
})
|
|
|
|
return result.docs.map((doc) => doc.id)
|
|
}
|
|
|
|
const loadMunicipalityAdvisorUserIds = async (
|
|
payload: Payload,
|
|
municipalityID: number,
|
|
req?: NotificationWriteRequest,
|
|
): Promise<number[]> => {
|
|
const municipality = await payload.findByID({
|
|
collection: 'municipality',
|
|
id: municipalityID,
|
|
depth: 0,
|
|
select: { advisors: true },
|
|
overrideAccess: true,
|
|
req,
|
|
})
|
|
|
|
return uniqueRelationshipIds(municipality.advisors)
|
|
}
|
|
|
|
type MunicipalityStaffRecipientsOptions = {
|
|
excludeUserId?: number | null
|
|
includeUnrestricted?: boolean
|
|
}
|
|
|
|
/** Advisors of the município plus optional coordinator/candidate staff. */
|
|
export const resolveMunicipalityStaffRecipientIds = async (
|
|
payload: Payload,
|
|
municipalityID: number,
|
|
options: MunicipalityStaffRecipientsOptions = {},
|
|
req?: NotificationWriteRequest,
|
|
): Promise<number[]> => {
|
|
const { excludeUserId = null, includeUnrestricted = true } = options
|
|
const [advisorIDs, unrestrictedIDs] = await Promise.all([
|
|
loadMunicipalityAdvisorUserIds(payload, municipalityID, req),
|
|
includeUnrestricted ? loadUnrestrictedStaffUserIds(payload, req) : Promise.resolve([]),
|
|
])
|
|
|
|
const recipientIDs = [...new Set([...advisorIDs, ...unrestrictedIDs])]
|
|
if (excludeUserId === null) return recipientIDs
|
|
|
|
return recipientIDs.filter((id) => id !== excludeUserId)
|
|
}
|