Skip to content

Commit

Permalink
feat: improve ux by allowing individual/multiple ids or all templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Lüder committed Sep 30, 2023
1 parent 1bb9c7e commit 0f3ba45
Showing 1 changed file with 107 additions and 34 deletions.
141 changes: 107 additions & 34 deletions src/commands/templates/delete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// TODO
// - allow deleting template by id
// - allow deleting a list of ids (template1, template2, template3, etc.)
// - allow deleting all templates without confirmation (-f flag useful for CI)
// - allow specifying template type to delete
// - get all templates from server and add to checkbox list so user can choose which ones to delete
// - allow users to choose type of template to delete "Templates", "Layouts" or "both" default to Templates
// - Prettier config?

import { confirm } from "@inquirer/prompts";
import { confirm, input, select } from "@inquirer/prompts";
import ora from "ora";
import { ServerClient } from "postmark";
import { TemplateTypes } from "postmark/dist/client/models";

import {
fatalError,
Expand All @@ -33,31 +34,92 @@ export async function handler(args: TemplateDeleteArguments): Promise<void> {
}

/**
* Ask user to confirm delete
* Ask user a series of questions before deleting templates.
*/

async function deletePrompt(
serverToken: string,
requestHost: string
): Promise<void> {
const answer = await confirm({
default: false,
message: `Delete ALL templates? Are you sure?`,
const choice = await select({
message: "Choose how you want to delete templates:",
choices: [
{
name: "Delete templates by id",
value: "byId",
},
{
name: "Delete all templates",
value: "all",
},
],
});

if (answer) {
return deleteTemplates({
sourceServer: serverToken,
requestHost: requestHost,
/**
* if the user has selected to delete templates by id
*/

if (choice === "byId") {
// TODO we probably want an actual validation
// against ids that exist in the server
const validateUserInput = async (input: string) =>
input !== undefined || input !== null;

const userInput = await input({
message: "Enter template id(s) - separated by commas if multiple:",
validate: validateUserInput,
});

const templateIds = userInput.split(",");

// if user has entered at least one id
if (templateIds.length) {
return deleteTemplates(templateIds, {
sourceServer: serverToken,
requestHost: requestHost,
});
}
}

/**
* if the user has selected to delete all templates
*/

if (choice === "all") {
const confirmed = await confirm({
default: false,
message: `Delete ALL templates? Are you sure?`,
});

const validateInput = async (input: string) =>
input !== "delete all templates" ? false : true;

let inputIsValid;

if (confirmed) {
inputIsValid = await input({
message: 'Enter "delete all templates" to confirm:',
validate: validateInput,
});
}

if (inputIsValid) {
return deleteTemplates([], {
sourceServer: serverToken,
requestHost: requestHost,
});
}
}
}

/**
* Delete templates from PM
*/

async function deleteTemplates(options: TemplateListOptions) {
async function deleteTemplates(
templateIds: string[],
options: TemplateListOptions
) {
const { sourceServer, requestHost } = options;

// keep track of templates deleted
Expand All @@ -73,43 +135,54 @@ async function deleteTemplates(options: TemplateListOptions) {
}

try {
const templates = await client.getTemplates({ count: 300 });

const totalTemplates = templates.Templates.filter(
(template) => template.TemplateType !== "Layout"
).length;
let templates = [];
let totalCount = 0;

// if user has provided ids we use them
if (templateIds.length) {
for (const id of templateIds) {
const template = await client.getTemplate(id);
templates.push(template);
}
totalCount = templates.length;
} else {
// otherwise fetch all templates from server
const response = await client.getTemplates({
count: 300,
templateType: TemplateTypes.Standard, // NOTE we might add this as an option users select
});
templates = response.Templates;
totalCount = response.TotalCount;
}

if (!templates.TotalCount) {
if (!totalCount) {
spinner.stop();
return fatalError("There are no templates on this server.");
} else {
spinner.text = `Deleting ${totalTemplates} templates from Postmark...`;
spinner.text = `Deleting ${totalCount} templates from Postmark...`;

for (const template of templates.Templates) {
for (const template of templates) {
spinner.text = `Deleting template: ${template.Alias || template.Name}`;

// TemplateId is always defined so use it instead of Alias
const id = template.TemplateId;

// NOTE we do not want to delete "Layouts"
if (template.TemplateType !== "Layout") {
try {
await client.deleteTemplate(id);
try {
await client.deleteTemplate(id);

spinner.text = `Template: ${
template.Alias || template.Name
} removed.`;
spinner.text = `Template: ${
template.Alias || template.Name
} removed.`;

totalDeleted++;
} catch (e) {
spinner.stop();
logError(e);
}
totalDeleted++;
} catch (e) {
spinner.stop();
logError(e);
}
}

// Show feedback when finished deleting templates
if (totalDeleted === totalTemplates) {
if (totalDeleted === totalCount) {
spinner.stop();
log(
`All finished! ${totalDeleted} ${pluralize(
Expand Down

0 comments on commit 0f3ba45

Please sign in to comment.