85 lines
2.3 KiB
TypeScript
85 lines
2.3 KiB
TypeScript
import type { SanitizedConfig } from "payload";
|
|
import { getPayload } from "payload";
|
|
|
|
export const script = async (config: SanitizedConfig) => {
|
|
const payload = await getPayload({ config });
|
|
|
|
payload.logger.info("[Generate Mission] Starting next mission template generation...");
|
|
|
|
const latest = await payload.find({
|
|
collection: "missions",
|
|
where: { operationType: { equals: "main" } },
|
|
sort: "-createdAt",
|
|
limit: 1,
|
|
depth: 0,
|
|
});
|
|
|
|
const source = latest.docs[0];
|
|
if (!source) {
|
|
payload.logger.info(
|
|
'[Generate Mission] No "main" operation mission found; skipping. Create the first one manually.',
|
|
);
|
|
process.exit(0);
|
|
}
|
|
|
|
// Same logic as the Missions collection startDateTime default: the upcoming
|
|
// Saturday at 20:00, always at least a week out.
|
|
const today = new Date();
|
|
const day = today.getDay();
|
|
const diff = (6 - day) % 7;
|
|
const nextSaturday = new Date(today);
|
|
nextSaturday.setDate(today.getDate() + diff + 7);
|
|
nextSaturday.setHours(20, 0, 0, 0);
|
|
|
|
const dateStamp = [
|
|
nextSaturday.getFullYear(),
|
|
String(nextSaturday.getMonth() + 1).padStart(2, "0"),
|
|
String(nextSaturday.getDate()).padStart(2, "0"),
|
|
].join("");
|
|
|
|
const codeName = `${source.codeName}-${dateStamp}`;
|
|
|
|
const existing = await payload.find({
|
|
collection: "missions",
|
|
where: { codeName: { equals: codeName } },
|
|
limit: 1,
|
|
depth: 0,
|
|
});
|
|
|
|
if (existing.docs.length > 0) {
|
|
payload.logger.info(
|
|
`[Generate Mission] Mission with codeName "${codeName}" already exists; skipping.`,
|
|
);
|
|
process.exit(0);
|
|
}
|
|
|
|
const {
|
|
id: _sourceId,
|
|
createdAt: _sourceCreatedAt,
|
|
updatedAt: _sourceUpdatedAt,
|
|
...template
|
|
} = source;
|
|
|
|
const mission = await payload.create({
|
|
collection: "missions",
|
|
data: {
|
|
...template,
|
|
codeName,
|
|
classification: {
|
|
...template.classification,
|
|
startDateTime: nextSaturday.toISOString(),
|
|
},
|
|
ownershipAndStatus: {
|
|
...template.ownershipAndStatus,
|
|
status: "Concept",
|
|
},
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
|
|
payload.logger.info(
|
|
`[Generate Mission] Created next mission template: id=${mission.id} name="${mission.name}" codeName="${mission.codeName}" startDateTime=${mission.classification.startDateTime}`,
|
|
);
|
|
process.exit(0);
|
|
};
|