Skip to content

Commit

Permalink
Add migrations for contact fields (#142)
Browse files Browse the repository at this point in the history
* Add migrations for contact fields

* Dynamic menuItemType id

* Fix error notification contacts field check and update item types to use dynamic menuItemType.id in migrations
  • Loading branch information
sjoerdbeentjes authored Dec 6, 2024
1 parent 1a74ded commit 95d6b0f
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions migrations/1732891349_contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Client } from "@datocms/cli/lib/cma-client-node";

export default async function (client: Client) {
const itemTypes = await client.itemTypes.list();
const menuItemType = itemTypes.find(
(itemType: { api_key: string }) => itemType.api_key === "menu"
) || { id: "" };

const fields = await client.fields.list(menuItemType.id);
const findField = (label: string) =>
fields.find((field: any) => field.label === label);

const errorNotificationContactsField = findField(
"Error notification contacts"
);
if (!errorNotificationContactsField) {
console.log(
'Create Multiple links field "Error notification contacts" (`error_notification_contacts`) in model "Menu" (`menu`)'
);
await client.fields.create(menuItemType.id, {
id: "GlLvpmJFTBup26dDM2OMuQ",
label: "Error notification contacts",
field_type: "links",
api_key: "error_notification_contacts",
validators: {
items_item_type: {
on_publish_with_unpublished_references_strategy: "fail",
on_reference_unpublish_strategy: "delete_references",
on_reference_delete_strategy: "delete_references",
item_types: [menuItemType.id],
},
},
appearance: { addons: [], editor: "links_select", parameters: {} },
default_value: null,
});
}

const feedbackContactsField = findField("Feedback contacts");
if (!feedbackContactsField) {
console.log(
'Create Multiple links field "Feedback contacts" (`feedback_contacts`) in model "Menu" (`menu`)'
);
await client.fields.create(menuItemType.id, {
id: "CPxFkf5cRPKm0ORLakLX3Q",
label: "Feedback contacts",
field_type: "links",
api_key: "feedback_contacts",
validators: {
items_item_type: {
on_publish_with_unpublished_references_strategy: "fail",
on_reference_unpublish_strategy: "delete_references",
on_reference_delete_strategy: "delete_references",
item_types: [menuItemType.id],
},
},
appearance: { addons: [], editor: "links_select", parameters: {} },
default_value: null,
});
}
}

3 comments on commit 95d6b0f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://nl2120.netlify.app as production
🚀 Deployed on https://6752d62078490f0a9831153d--nl2120.netlify.app

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.