Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mail everyone #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions src/commands/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import writeToFiles from "../modules/writeToFiles";
import mailgunSender from "../modules/mailgunSender";
import recipientExporter from "../modules/recipientExporter";
import djangoBackendUserRetriever from "../modules/djangoBackendUserRetriever";
import djangoBackendAllUserRetriever from "../modules/djangoBackendAllUserRetriever";
import Setting from "../utils/settings";
import State from "../utils/state";

Expand Down Expand Up @@ -46,6 +47,9 @@ export default class SendCommand extends Command {
django_backend: flags.boolean({
description: "retrieve recipients from the D-sektionen Django backend",
}),
django_everyone_backend: flags.boolean({
description: "retrieve all recipients from the D-sektionen Django backend",
}),
// wp: flags.boolean({
// description: "retrieve recipients from Wordpress",
// }),
Expand Down Expand Up @@ -73,6 +77,7 @@ export default class SendCommand extends Command {
test: flags.boolean({
description: "enable mailgun test mode",
}),

export_recipients: flags.string({
description: "path to json file which recipients are exported to.",
}),
Expand All @@ -83,6 +88,7 @@ export default class SendCommand extends Command {
const { flags } = this.parse(SendCommand);
const {
django_backend,
django_everyone_backend,
// wp,
recipients,
content,
Expand Down Expand Up @@ -156,9 +162,19 @@ export default class SendCommand extends Command {
// cli.action.stop();
// }

// Get all recipients from the django backend if flag present.
if (django_everyone_backend) {
cli.action.start("Retrieving EVERYONE. (Even non-subscribers)");
await djangoBackendAllUserRetriever(
state,
await Setting.DJANGO_TOKEN.getValue(this.config.configDir)
);
cli.action.stop();
}

// Get recipients from the django backend if flag present.
if (django_backend) {
cli.action.start("Retrieving Django backend users");
else if (django_backend ) {
cli.action.start("Retrieving all subscribed users");
await djangoBackendUserRetriever(
state,
await Setting.DJANGO_TOKEN.getValue(this.config.configDir)
Expand Down
27 changes: 27 additions & 0 deletions src/modules/djangoBackendAllUserRetriever.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import axios from "axios";
import State from "../utils/state";
import { Recipient } from "../utils/models";

const ENDPOINT = "https://backend.d-sektionen.se/account/infomail-everyone/";

const axiosOptions = (token: string) => ({
headers: {
Authorization: `Token ${token}`,
},
});

const djangoBackendAllUserRetriever = async (
state: State,
token: string
): Promise<void> => {
const res = await axios.get(ENDPOINT, axiosOptions(token));

state.addRecipients(
res.data.map(
(user: { email: string; pretty_name: string }) =>
new Recipient(user.email, user.pretty_name)
)
);
};

export default djangoBackendAllUserRetriever;