SharezoneApp / sharezone-app

Sharezone is a collaborative school organization app for iOS, Android, macOS and web with +500,000 downloads. Built with Flutter & Firebase.
https://sharezone.net
European Union Public License 1.2
254 stars 46 forks source link

Give users who donated a few months sz plus for free? #1612

Closed nilsreichardt closed 1 month ago

nilsreichardt commented 1 month ago

We granted every person who donated 6 months of Sharezone Plus. The code of the script:

import { gateways, userCollection } from "./firebase_globals";
import * as admin from "firebase-admin";
import { DonateMessage } from "./message/app/feedback/donated_message";

export async function giveDonatersPlus() {
  const messageService = gateways.getMessageService();
  const builder = gateways.getMessageReciepentBuilder();

  const donators = await getDonators();
  for (const donatorId of donators) {
    const user = await userCollection.doc(donatorId).get();
    const hasAlreadyPlus = user.get("sharezonePlus.hasPlus") ?? false;
    const isStudent = user.get("typeOfUser") === "student";

    if (!isStudent) {
      console.log(`User ${donatorId} is not a student`);
      continue;
    }

    if (hasAlreadyPlus) {
      console.log(`User ${donatorId} already has plus`);
      continue;
    }

    await userCollection.doc(donatorId).update({
      sharezonePlus: {
        hasPlus: true,
        period: "lifetime",
        reason: "has-donated",
      },
    });
    try {
      await admin.auth().setCustomUserClaims(donatorId, {
        hasPlus: true,
      });
    } catch (error) {
      console.log(
        `Error setting custom claims for user ${donatorId}: ${error}`,
      );
    }

    const receivers = await builder.getMessageRecieversForSingleUser(donatorId);
    const message = new DonateMessage(receivers);

    await messageService.sendMessage(message);

    console.log(`User ${donatorId} got plus`);
  }
}

async function getDonators(): Promise<string[]> {
  const docs = await userCollection.where("hasDonated", "==", true).get();
  return docs.docs.map((doc) => doc.id);
}
export class NoActionTypeMessageMetaData implements MessageMetaData {
  constructor() {
    // Empty constructor
  }

  metaData = {
    click_action: "FLUTTER_NOTIFICATION_CLICK",
  };

  toString() {
    return this.metaData;
  }
}