teqo-backup/src/app/(campaign)/campanha/actions/contentPieces.ts
Francisco Solla 10327a7451 feat(C236): publicar, despublicar e apagar em lote na Central de Conteúdos
A lista /campanha/comunicacao/conteudos ganha um modo de seleção opt-in
que envolve os dois corpos (tabela e card list) com um estado client
compartilhado, chaveado pela URL canônica (a seleção morre em
página/filtro e nunca cresce para além da página visível). Com seleção
ativa, a barra flutuante aplica o mesmo veredito unitário à seleção:
Publicar/Despublicar agem direto com recibo honesto; Apagar sempre
confirma no CampaignDeleteDialog reusado (novo caminho onConfirm
aditivo), avisando os links públicos só quando a seleção carrega
publicadas.

O transporte é uma rota única (POST .../conteudos/lote,
campaignJsonMutationRoute, teto de 50 ids) que itera server-side as ações
donas: cada peça mantém a própria transação, o gate fresco e a limpeza
best-effort de mídia; a falha de uma nunca reverte as demais e o recibo
separa afetadas x falhas (2 de 3 peças apagadas. 1 falhou.).

Sem migration, sem Consent novo, sem rota pública (S27) e sem mudar a
semântica do C222. Cobertura: int do lote (papéis, dedupe, mídia, evento,
tag, falha parcial, schema), unit do builder/barra/seleção e e2e HTTP.

Design tier: DEGRADED (artefato de origem sem certificação frontier) —
crítica do designer aplicada no escopo do item; entrega bloqueada para
sign-off humano antes do PR. Related #1388
2026-09-29 22:26:26 -03:00

489 lines
17 KiB
TypeScript

'use server'
import type { Payload } from 'payload'
import { canReadCommunicationCatalog } from '@/lib/campaignRoles'
import {
CONTENT_MEDIA_SLUG,
contentPieceLinkTitle,
isContentPieceTopic,
parseContentPieceLink,
toContentPieceViewModel,
type ContentPieceCuratedField,
type ContentPieceViewModel,
} from '@/lib/contentPiece'
import type { ContentPieceBatchOutcome } from '@/lib/contentPieceBatch'
import { normalizeContentPiecePublicFigures } from '@/lib/publicFigureCatalog'
import {
CONTENT_PIECE_FORBIDDEN_MESSAGE,
CONTENT_PIECE_GENERIC_ERROR_MESSAGE,
CONTENT_PIECE_LINK_DUPLICATE_MESSAGE,
CONTENT_PIECE_LINK_INVALID_MESSAGE,
CONTENT_PIECE_NOT_FOUND_MESSAGE,
CONTENT_PIECE_RETRY_NOT_FAILED_MESSAGE,
CONTENT_PIECE_SAFE_MESSAGES,
contentPieceBatchRequestSchema,
contentPieceDeleteRequestSchema,
contentPieceLinkRequestSchema,
contentPieceProfileImportRequestSchema,
contentPiecePublicationRequestSchema,
contentPieceRetryRequestSchema,
contentPieceStatusRequestSchema,
contentPieceUpdateRequestSchema,
type ContentPieceBatchRequest,
type ContentPieceStatusRequest,
type ContentPieceUpdateRequest,
} from '@/lib/schemas/contentPiece'
import type { SpeechTopic } from '@/lib/speechFacets'
import type { CampaignUser } from '@/payload-types'
import { getCampaignActionContext } from '@/utilities/campaignActionContext'
import { mapCampaignFormActionError } from '@/utilities/campaignFormActionError'
import { reapStaleContentPiece } from '@/utilities/content/contentPieceJob'
import {
searchContentPieceLeaderOptions,
type ContentPieceLeaderOption,
} from '@/utilities/content/contentPieceLeaderOptions'
import {
contentPieceExistsForPostIdentity,
isContentPieceSourceUrlDuplicateError,
} from '@/utilities/content/contentPieceLink'
import {
createContentPieceFromProfilePost,
listContentPieceProfileImportCandidates,
type ContentPieceProfileImportListing,
type ContentPieceProfileImportOutcome,
} from '@/utilities/content/contentPieceProfileImport'
import { startContentPieceJobInBackground } from '@/utilities/content/contentPieceScheduler'
import { onPayloadTransactionCommit, withPayloadTransaction } from '@/utilities/payloadTransaction'
/**
* C211 — mutations of the Central de Conteúdos: edit one piece's catalogue,
* move its kill switch, add a piece by link and retry a failed processing. The
* communication gate is repeated fresh before any read or write (same contract
* as the acervo/cut actions); the collection access is the final barrier.
*/
const contentPieceSelect = {
title: true,
updatedAt: true,
type: true,
status: true,
processingStatus: true,
step: true,
origin: true,
topics: true,
cityLabel: true,
region: true,
durationSeconds: true,
pieceDate: true,
publishedAt: true,
error: true,
linkFailureReason: true,
media: true,
curatedFields: true,
} as const
const loadContentPieceForActor = async (
payload: Payload,
actor: CampaignUser,
contentPieceId: number,
) => {
const result = await payload.find({
collection: 'contentPiece',
where: { id: { equals: contentPieceId } },
depth: 0,
limit: 1,
pagination: false,
select: contentPieceSelect,
user: actor,
overrideAccess: false,
})
return result.docs[0]
}
/**
* Saves the ficha and records what the assessoria curated: every field the form
* carried is added to `curatedFields`, so a running job never overwrites a
* human decision (D6) — including an explicit clear (an empty value is a
* decision, not an invitation to refill).
*/
export const updateContentPieceForActor = async (
input: ContentPieceUpdateRequest,
): Promise<ContentPieceViewModel> => {
const parsed = contentPieceUpdateRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
const current = await loadContentPieceForActor(payload, actor, parsed.contentPieceId)
if (!current) throw new Error(CONTENT_PIECE_NOT_FOUND_MESSAGE)
const curated = new Set(current.curatedFields ?? [])
const curatedOnSave: [ContentPieceCuratedField, boolean][] = [
['title', parsed.title !== undefined],
['description', parsed.description !== undefined],
['topics', parsed.topics !== undefined],
// The request carries the relationship id; the curated vocabulary uses the
// persisted field name.
['municipality', parsed.municipalityId !== undefined],
['institution', parsed.institution !== undefined],
['pieceDate', parsed.pieceDate !== undefined],
['transcript', parsed.transcript !== undefined],
['type', parsed.type !== undefined],
]
for (const [field, touched] of curatedOnSave) if (touched) curated.add(field)
const topics = (parsed.topics ?? [])
.filter(isContentPieceTopic)
.filter((value, index, values) => values.indexOf(value) === index) as SpeechTopic[]
const updated = await payload.update({
collection: 'contentPiece',
id: parsed.contentPieceId,
data: {
title: parsed.title,
type: parsed.type,
description: parsed.description ?? null,
pieceDate: parsed.pieceDate ?? null,
topics,
municipality: parsed.municipalityId ?? null,
institution: parsed.institution ?? null,
transcript: parsed.transcript ?? null,
leaders: parsed.leaderIds ?? [],
publicFigures: normalizeContentPiecePublicFigures(parsed.publicFigures ?? []),
curatedFields: [...curated],
},
depth: 0,
select: contentPieceSelect,
user: actor,
overrideAccess: false,
})
return toContentPieceViewModel(updated)
}
/**
* The kill switch: `publicado` puts the piece on the public Central (the first
* publication generates its canonical slug inside the transaction), `rascunho`
* takes it off at once and preserves slug, `publishedAt` and file.
*/
export const setContentPiecePublishedForActor = async (input: {
contentPieceId: number
published: boolean
}): Promise<ContentPieceViewModel> => {
const parsed = contentPiecePublicationRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
const current = await loadContentPieceForActor(payload, actor, parsed.contentPieceId)
if (!current) throw new Error(CONTENT_PIECE_NOT_FOUND_MESSAGE)
const updated = await withPayloadTransaction(payload, async ({ req }) =>
payload.update({
collection: 'contentPiece',
id: parsed.contentPieceId,
data: { status: parsed.published ? 'publicado' : 'rascunho' },
depth: 0,
select: contentPieceSelect,
user: actor,
overrideAccess: false,
req,
}),
)
return toContentPieceViewModel(updated)
}
/** Retries the processing of a failed piece (same row, same job). */
export const retryContentPieceForActor = async (input: {
contentPieceId: number
}): Promise<ContentPieceViewModel> => {
const parsed = contentPieceRetryRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
const current = await loadContentPieceForActor(payload, actor, parsed.contentPieceId)
if (!current) throw new Error(CONTENT_PIECE_NOT_FOUND_MESSAGE)
if (current.processingStatus !== 'falhou') throw new Error(CONTENT_PIECE_RETRY_NOT_FAILED_MESSAGE)
// Conditional update: two concurrent retries cannot both schedule a job.
const updated = await payload.update({
collection: 'contentPiece',
where: {
and: [{ id: { equals: parsed.contentPieceId } }, { processingStatus: { equals: 'falhou' } }],
},
data: {
processingStatus: 'processando',
step: 'extraindo',
error: null,
linkFailureReason: null,
},
depth: 0,
select: contentPieceSelect,
user: actor,
overrideAccess: false,
})
const piece = updated.docs[0]
if (!piece) throw new Error(CONTENT_PIECE_RETRY_NOT_FAILED_MESSAGE)
startContentPieceJobInBackground(piece.id)
return toContentPieceViewModel(piece)
}
/**
* C222 — hard-deletes one piece: the row goes inside the transaction (the
* collection access is the final barrier); the private media row is removed
* after commit, best-effort, so a storage hiccup cannot fail the delete the
* person asked for (same shape as the C199 recording delete). The media id is
* read from the doc the delete returned — reading it in the delete itself
* closes the window in which the pipeline could have attached a file between a
* pre-read and the commit. Deleting a `processando` row is deliberate: it is
* how the assessoria cancels a stuck piece, and the job tolerates the row
* disappearing.
*/
export const deleteContentPieceForActor = async (input: {
contentPieceId: number
}): Promise<{ deleted: true }> => {
const parsed = contentPieceDeleteRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
const current = await loadContentPieceForActor(payload, actor, parsed.contentPieceId)
if (!current) throw new Error(CONTENT_PIECE_NOT_FOUND_MESSAGE)
await withPayloadTransaction(payload, async ({ transactionID, req }) => {
const deleted = await payload.delete({
collection: 'contentPiece',
id: parsed.contentPieceId,
depth: 0,
select: { media: true },
user: actor,
overrideAccess: false,
req,
})
const mediaId = typeof deleted.media === 'number' ? deleted.media : (deleted.media?.id ?? null)
if (mediaId !== null) {
onPayloadTransactionCommit(transactionID, () => {
void payload
.delete({
collection: CONTENT_MEDIA_SLUG,
id: mediaId,
// Intentional admin bypass: cleanup of the file that belonged to
// the piece this actor was authorized to delete.
overrideAccess: true,
})
.catch(() => undefined)
})
}
})
return { deleted: true }
}
/**
* C236 — applies the same verdict to a bounded set of pieces. The batch is the
* unit gesture repeated, so every id goes through the unit action: its own
* transaction, its own fresh gate, its own post-commit media cleanup, its own
* collection access as the final barrier. A failure is named in the outcome and
* never reverts the pieces that succeeded (the product's honest partial
* failure); the communication gate is checked once before the loop, so a denied
* actor fails the whole request instead of turning into per-item noise. The
* loop is sequential on purpose: the page ceiling is 25 ids and determinism
* beats latency for a desk gesture.
*/
export const applyContentPieceBatchActionForActor = async (
input: ContentPieceBatchRequest,
): Promise<ContentPieceBatchOutcome> => {
const parsed = contentPieceBatchRequestSchema.parse(input)
const { actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
// A repeated id would act twice on the same row (the second one failing as
// not found), so the batch normalizes the selection before looping.
const contentPieceIds = [...new Set(parsed.contentPieceIds)]
const failures: ContentPieceBatchOutcome['failures'] = []
let affected = 0
for (const contentPieceId of contentPieceIds) {
try {
if (parsed.action === 'apagar') {
await deleteContentPieceForActor({ contentPieceId })
} else {
await setContentPiecePublishedForActor({
contentPieceId,
published: parsed.action === 'publicar',
})
}
affected += 1
} catch (error) {
failures.push({
contentPieceId,
message:
mapCampaignFormActionError({
error,
safeMessages: CONTENT_PIECE_SAFE_MESSAGES,
genericMessage: CONTENT_PIECE_GENERIC_ERROR_MESSAGE,
}).message ?? CONTENT_PIECE_GENERIC_ERROR_MESSAGE,
})
}
}
return { action: parsed.action, affected, failures }
}
/**
* Polls the statuses of the visible pieces; a stale `processando` row is reaped
* to `falhou` on the way (there is no queue to ask), then the page is read
* fresh so the returned status is the repaired one.
*/
export const getContentPieceStatusesForActor = async (
input: ContentPieceStatusRequest,
): Promise<ContentPieceViewModel[]> => {
const parsed = contentPieceStatusRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
const where = { id: { in: parsed.contentPieceIds } }
const first = await payload.find({
collection: 'contentPiece',
where,
depth: 0,
limit: parsed.contentPieceIds.length,
pagination: false,
select: contentPieceSelect,
user: actor,
overrideAccess: false,
})
await Promise.all(
first.docs.map((piece) =>
reapStaleContentPiece(payload, {
id: piece.id,
processingStatus: piece.processingStatus,
updatedAt: piece.updatedAt,
}),
),
)
const result = await payload.find({
collection: 'contentPiece',
where,
depth: 0,
limit: parsed.contentPieceIds.length,
pagination: false,
select: contentPieceSelect,
user: actor,
overrideAccess: false,
})
return result.docs.map((piece) => toContentPieceViewModel(piece))
}
/**
* Adds a piece by link: the canonical URL is the identity (unique in the DB), so
* pasting the same post twice answers the same domain message instead of
* creating a twin row. The pipeline resolves the official extraction in the
* background; a link without an official path becomes a peça-link.
*/
export const addContentPieceByLinkForActor = async (input: {
url: string
}): Promise<ContentPieceViewModel> => {
const parsed = contentPieceLinkRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
const link = parseContentPieceLink(parsed.url)
if (!link) throw new Error(CONTENT_PIECE_LINK_INVALID_MESSAGE)
// The post's identity, not the URL spelling: pasting `/p/ABC/` answers the
// same duplicate as `/reel/ABC/` (C230 shares this probe with the importer).
if (await contentPieceExistsForPostIdentity({ payload, actor, link })) {
throw new Error(CONTENT_PIECE_LINK_DUPLICATE_MESSAGE)
}
try {
// A link is catalogued as a video piece; the extraction reclassifies an
// Instagram image to `foto` and the assessoria can change the type.
const piece = await payload.create({
collection: 'contentPiece',
data: {
title: contentPieceLinkTitle(link),
type: 'video',
origin: link.origin,
sourceUrl: link.canonicalUrl,
status: 'rascunho',
processingStatus: 'processando',
step: 'extraindo',
},
depth: 0,
user: actor,
overrideAccess: false,
})
startContentPieceJobInBackground(piece.id)
return toContentPieceViewModel(piece)
} catch (error) {
// A concurrent paste can still hit the unique index; it is the same
// duplicate the probe above answers, so it maps to the same message.
if (isContentPieceSourceUrlDuplicateError(error)) {
throw new Error(CONTENT_PIECE_LINK_DUPLICATE_MESSAGE)
}
throw error
}
}
/**
* C230 — lists the novelties of the official profile: the feed is read with
* the global's credential (fail-closed) and each post is checked against the
* Central by the identity of the post, never by the URL spelling. The listing
* persists nothing; the confirmation creates the pieces one by one.
*/
export const listContentPieceProfileImportCandidatesForActor = async (
input: unknown = {},
): Promise<ContentPieceProfileImportListing> => {
const window = contentPieceProfileImportRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
return listContentPieceProfileImportCandidates({ payload, actor, window })
}
/**
* C230 — creates ONE draft from ONE listed media through the C220 pipeline
* (`existing` covers a repeated/concurrent import). The dialog calls it per
* candidate so one failure never stops the others.
*/
export const createContentPieceFromProfilePostForActor = async (input: {
url: string
}): Promise<{ outcome: ContentPieceProfileImportOutcome }> => {
const parsed = contentPieceLinkRequestSchema.parse(input)
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
const { outcome } = await createContentPieceFromProfilePost({ payload, actor, url: parsed.url })
return { outcome }
}
/**
* S37 — the ficha's leader picker search. Same communication gate as every
* action of the vertical; the returned options carry ONLY `{ id, label }` (the
* display-name projection the int tests assert), never any other leadership
* field.
*/
export const searchContentPieceLeaderOptionsForActor = async (
query: string,
): Promise<ContentPieceLeaderOption[]> => {
const { payload, actor } = await getCampaignActionContext()
if (!canReadCommunicationCatalog(actor.role)) throw new Error(CONTENT_PIECE_FORBIDDEN_MESSAGE)
return searchContentPieceLeaderOptions(payload, actor, query)
}