FIREBASE Cloud Functions: Function returned undefined, expected Promise or value

Viewed 28

I created an app using Firebase and expo. I have written a firebase function to schedule unsubscribe by stripe extension.

I always receive this WARNING always ( but the function works): Function returned undefined, expected Promise or value

Here's the code of my like function:

exports.subscriptionTrigger = functions.firestore.document("users/{docId}/subscriptions/{paymentMethodId}").onWrite((change, context) => {
const subscriptionsErrors = [
  "incomplete",
  "past_due",
  "unpaid",
  "incomplete_expired",
  "canceled",
];

//get document, if documents not exists it has been deleted
const newValue = change.after.exists ? change.after.data() : null;

//get userId
const userId = context.params.docId;

const beforeData = change.before.exists ? change.before.data() : null;

if (beforeData && newValue && beforeData.status === newValue.status) {
  console.log("Not a status change. Exiting.");
  return null;
} else {
  //stripe subscritpion
  if (newValue.status === "active" || newValue.status === "trialing") {
    //TODO indentify PLAN and set current plan
    const priceId = newValue.items[0].price.id;

    db.doc(`users/${userId}`)
      .set(
        {
          plan: priceId,
        },
        { merge: true }
      )
      .then(() => {
        console.log("plan added to the user!");
      })
      .catch((error) => {
        console.log("plan not added to the user", error);
      });

    if (newValue.status === "active") {
      db.doc(`functions/data-module-exports`)
        .get()
        .then((snapshot) => {
          const iterations = snapshot.data().moduleExports[priceId]?.iterations
          console.log("iterations", iterations)
          console.log("Document data:", snapshot.data())
          if (iterations > 0) {
            const subId = context.params.paymentMethodId
            request.post(
              "https://api.stripe.com/v1/subscription_schedules",
              {
                form: {
                  from_subscription: subId,
                },
                headers: {
                  Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
                },
              },
              (err, res, body) => {
                console.log("err1 interations,", err)
                const info = JSON.parse(body)
                console.log("info,", info)
                const scheduleId = info.id
                const startDate = info.current_phase.start_date
                  const couponId = info.phases[0].coupon
                  request.post(
                    `https://api.stripe.com/v1/subscription_schedules/${scheduleId}`,
                    {
                      form: {
                        end_behavior: "cancel",
                        "phases[0]start_date": startDate,
                        "phases[0][items][0][price]": priceId,
                        "phases[0][iterations]": iterations,
                        "phases[0][items][0][quantity]": 1,
                        "phases[0][coupon]": couponId,
                      },
                      headers: {
                        Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
                      },
                    },
                    (err, res, body) => {
                      const info = JSON.parse(body);
                      console.log("erro,", err);
                    }
                  )
              }
            );
          }
        })
        .catch((error) => {console.log("could not find iterations", error)});
    }
  } else if (subscriptionsErrors.includes(newValue.status)) {
    //Set plan = 0, se subscription status for incomplete, past_due, unpaid
    //excluir plan se subscription status for incomplete_expirer ou canceled
    if (
      newValue.status === "canceled" ||
      newValue.status === "incomplete_expired"
    ) {
      db.doc(`users/${userId}`).set(
        {plan: admin.firestore.FieldValue.delete()},
        { merge: true }
      ).then(() => {console.log("plan of the user deleted!")})
        .catch((error) => {console.log("plan of the user could not be deleted!", error)});
    } else if (
      newValue.status === "incomplete" ||
      newValue.status === "past_due" ||
      newValue.status === "unpaid"
    ) {
      db.doc(`users/${userId}`).set(
        {plan: "0",},
        { merge: true }
      ).then(() => {console.log("plan of the user changed to 0!");})
        .catch((error) => {console.log("plan of the user could not be changed to 0!", error)});
    }
  }
}

});

Can someone help me with this?

0 Answers
Related