teqo-backup/src/lib/schemas/contentPiece.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

225 lines
9 KiB
TypeScript

import { z } from 'zod'
import { formatBahiaCivilDate } from '@/lib/campaignTime'
import {
CONTENT_PIECE_DESCRIPTION_MAX_LENGTH,
CONTENT_PIECE_INSTITUTION_MAX_LENGTH,
CONTENT_PIECE_LEADERS_MAX,
CONTENT_PIECE_PUBLIC_FIGURES_MAX,
CONTENT_PIECE_PUBLIC_FIGURE_MAX_LENGTH,
CONTENT_PIECE_TEXT_TOO_LARGE_MESSAGE,
CONTENT_PIECE_TITLE_MAX_LENGTH,
CONTENT_PIECE_TYPES,
} from '@/lib/contentPiece'
import { CONTENT_PIECE_BATCH_ACTIONS } from '@/lib/contentPieceBatch'
import { contentPieceProfilePeriodError } from '@/lib/contentPieceProfileWindow'
import { trimmedNullableText } from '@/lib/schemas/primitives'
import { TRANSCRIPT_TEXT_MAX_LENGTH } from '@/lib/speechSearch'
/** The Central gate refused the actor — one literal shared with the vertical. */
export const CONTENT_PIECE_FORBIDDEN_MESSAGE = 'Seu perfil não tem acesso à Central de Conteúdos.'
/** The piece does not exist (or the actor cannot read it). */
export const CONTENT_PIECE_NOT_FOUND_MESSAGE = 'Peça não encontrada.'
/** Retry only applies to a failed piece. */
export const CONTENT_PIECE_RETRY_NOT_FAILED_MESSAGE =
'Só é possível reprocessar uma peça que falhou.'
/** Transport/storage failure (or anything unmapped) of a JSON action. */
export const CONTENT_PIECE_GENERIC_ERROR_MESSAGE =
'Não foi possível concluir a ação. Verifique seu acesso e tente novamente.'
/** The pasted link is not an Instagram/YouTube publication. */
export const CONTENT_PIECE_LINK_INVALID_MESSAGE = 'Cole um link do Instagram ou do YouTube.'
/** The same link is already catalogued (the `sourceUrl` identity probe). */
export const CONTENT_PIECE_LINK_DUPLICATE_MESSAGE = 'Esta peça já está na Central.'
/** C230 — the official Instagram credential is missing or the feed is off. */
export const CONTENT_PIECE_PROFILE_IMPORT_UNAVAILABLE_MESSAGE = 'Instagram ainda não configurado.'
/** C230 — the Graph API did not answer the profile listing. */
export const CONTENT_PIECE_PROFILE_IMPORT_FEED_ERROR_MESSAGE =
'Não foi possível falar com o Instagram agora. Tente novamente.'
/** The upload arrived without a usable file name (a field error). */
const CONTENT_PIECE_FILE_NAME_MESSAGE = 'Nome de arquivo inválido.'
/** The picked file is outside the four families the Central catalogues. */
export const CONTENT_PIECE_FILE_TYPE_MESSAGE = 'Envie um vídeo, um áudio, uma foto ou um texto.'
/** The raw-body upload arrived without a body (shared with the upload module). */
export const CONTENT_PIECE_BODY_MISSING_MESSAGE = 'A requisição não trouxe o arquivo da peça.'
/** The stream guard's ceiling refusal (the picker's per-file copy is dynamic). */
export const CONTENT_PIECE_UPLOAD_TOO_LARGE_MESSAGE = 'O arquivo excede o limite de 4 GB.'
/** "Anexar arquivo original" on a piece that already has a file. */
export const CONTENT_PIECE_MEDIA_ALREADY_ATTACHED_MESSAGE = 'Esta peça já tem um arquivo original.'
/** The ficha sent a type outside the vocabulary. */
export const CONTENT_PIECE_TYPE_INVALID_MESSAGE = 'Tipo de peça inválido.'
/** The ficha saved an empty title (a field error, never a safe wire message). */
const CONTENT_PIECE_TITLE_REQUIRED_MESSAGE = 'Informe um título para a peça.'
/** Filename sanity: no path parts, no empties — the temp file keeps the extension. */
const uploadFilename = z
.string()
.trim()
.min(1, CONTENT_PIECE_FILE_NAME_MESSAGE)
.max(255, CONTENT_PIECE_FILE_NAME_MESSAGE)
.refine((value) => !value.includes('/') && !value.includes('\\'), CONTENT_PIECE_FILE_NAME_MESSAGE)
/** Metadata of the raw-body upload (`POST .../conteudos/enviar`). */
export const contentPieceUploadMetadataSchema = z.object({
filename: uploadFilename,
})
export type ContentPieceUploadMetadata = z.infer<typeof contentPieceUploadMetadataSchema>
/** Add a piece by link (Instagram/YouTube). */
export const contentPieceLinkRequestSchema = z.object({
url: z.string().trim().min(1, CONTENT_PIECE_LINK_INVALID_MESSAGE),
})
/**
* C235 — the window of the profile import: the default recency slice (`{}` or
* `recent`, the C230 contract) or an explicit civil period (`since` required,
* `until` optional). The period is validated against the Bahia civil day —
* real date, order, not in the future — so the wire never carries an
* impossible window.
*/
const contentPieceProfileImportWindowSchema = z.discriminatedUnion('mode', [
z.object({ mode: z.literal('recent') }),
z.object({
mode: z.literal('period'),
since: z.string().trim(),
until: z.string().trim().nullish(),
}),
])
export const contentPieceProfileImportRequestSchema = z.preprocess(
(value) =>
value && typeof value === 'object' && Object.keys(value).length === 0
? { mode: 'recent' }
: value,
contentPieceProfileImportWindowSchema.superRefine((window, context) => {
if (window.mode !== 'period') return
const message = contentPieceProfilePeriodError({
since: window.since,
until: window.until ?? null,
today: formatBahiaCivilDate(new Date()),
})
if (message) context.addIssue({ code: 'custom', message })
}),
)
/** The editable catalogue of one piece (the ficha form). */
export const contentPieceUpdateRequestSchema = z.object({
contentPieceId: z.number().int().positive(),
title: z
.string()
.trim()
.min(1, CONTENT_PIECE_TITLE_REQUIRED_MESSAGE)
.max(
CONTENT_PIECE_TITLE_MAX_LENGTH,
`O título deve ter até ${CONTENT_PIECE_TITLE_MAX_LENGTH} caracteres.`,
),
description: trimmedNullableText(CONTENT_PIECE_DESCRIPTION_MAX_LENGTH),
type: z.enum(CONTENT_PIECE_TYPES),
pieceDate: trimmedNullableText(32),
// Kept as raw tokens: the action filters them against the taxonomy and the
// field counts as curated whenever the form carried it at all.
topics: z.array(z.string()).max(32).optional(),
municipalityId: z.number().int().positive().nullable().optional(),
institution: trimmedNullableText(CONTENT_PIECE_INSTITUTION_MAX_LENGTH),
transcript: trimmedNullableText(TRANSCRIPT_TEXT_MAX_LENGTH),
// S37 — who appears in the piece: the leader relation ids and the curated
// public figures. The action canonicalizes the figures against the catalog.
leaderIds: z.array(z.number().int().positive()).max(CONTENT_PIECE_LEADERS_MAX).optional(),
publicFigures: z
.array(z.string().trim().max(CONTENT_PIECE_PUBLIC_FIGURE_MAX_LENGTH))
.max(CONTENT_PIECE_PUBLIC_FIGURES_MAX)
.optional(),
})
export type ContentPieceUpdateRequest = z.infer<typeof contentPieceUpdateRequestSchema>
/** Retry the processing of a failed piece. */
export const contentPieceRetryRequestSchema = z.object({
contentPieceId: z.number().int().positive(),
})
/** Delete one piece for good (row + its private media file). */
export const contentPieceDeleteRequestSchema = z.object({
contentPieceId: z.number().int().positive(),
})
/** Poll the statuses of the visible pieces (bounded batch). */
export const contentPieceStatusRequestSchema = z.object({
contentPieceIds: z.array(z.number().int().positive()).min(1).max(50),
})
export type ContentPieceStatusRequest = z.infer<typeof contentPieceStatusRequestSchema>
/** The kill switch of one piece. */
export const contentPiecePublicationRequestSchema = z.object({
contentPieceId: z.number().int().positive(),
published: z.boolean(),
})
/**
* C236 — apply the same verdict to a bounded set of pieces: the selection is
* one page of the list, so the ceiling mirrors the status poll (50 ids) and the
* action dedupes whatever arrives.
*/
export const contentPieceBatchRequestSchema = z.object({
action: z.enum(CONTENT_PIECE_BATCH_ACTIONS),
contentPieceIds: z.array(z.number().int().positive()).min(1).max(50),
})
export type ContentPieceBatchRequest = z.infer<typeof contentPieceBatchRequestSchema>
/** Attach the original file to a link piece. */
export const contentPieceAttachRequestSchema = z.object({
contentPieceId: z.number().int().positive(),
filename: uploadFilename,
})
/**
* The domain messages the JSON routes and the ficha form are allowed to pass
* through verbatim (everything else collapses to the generic one).
*/
export const CONTENT_PIECE_SAFE_MESSAGES = [
CONTENT_PIECE_FORBIDDEN_MESSAGE,
CONTENT_PIECE_NOT_FOUND_MESSAGE,
CONTENT_PIECE_RETRY_NOT_FAILED_MESSAGE,
CONTENT_PIECE_LINK_INVALID_MESSAGE,
CONTENT_PIECE_LINK_DUPLICATE_MESSAGE,
CONTENT_PIECE_MEDIA_ALREADY_ATTACHED_MESSAGE,
CONTENT_PIECE_TYPE_INVALID_MESSAGE,
] as const
/**
* The messages the C230 profile-import routes may return verbatim: the shared
* piece messages plus the feed failure and the fail-closed credential refusal.
*/
export const CONTENT_PIECE_PROFILE_IMPORT_SAFE_MESSAGES = [
...CONTENT_PIECE_SAFE_MESSAGES,
CONTENT_PIECE_PROFILE_IMPORT_UNAVAILABLE_MESSAGE,
CONTENT_PIECE_PROFILE_IMPORT_FEED_ERROR_MESSAGE,
] as const
/**
* The messages the two raw-body piece routes (upload and attach) may return
* verbatim; the JSON routes use `CONTENT_PIECE_SAFE_MESSAGES` alone.
*/
export const CONTENT_PIECE_UPLOAD_SAFE_MESSAGES = [
...CONTENT_PIECE_SAFE_MESSAGES,
CONTENT_PIECE_FILE_TYPE_MESSAGE,
CONTENT_PIECE_BODY_MISSING_MESSAGE,
CONTENT_PIECE_UPLOAD_TOO_LARGE_MESSAGE,
CONTENT_PIECE_TEXT_TOO_LARGE_MESSAGE,
] as const