|
| 1 | +const FirebaseConfig = require("./FirebaseConfig"); |
| 2 | +const recipesApi = require("./recipesApi"); |
| 3 | +const functions = FirebaseConfig.functions; |
| 4 | +const firestore = FirebaseConfig.firestore; |
| 5 | +const storageBucket = FirebaseConfig.storageBucket; |
| 6 | +const admin = FirebaseConfig.admin; |
| 7 | + |
| 8 | +exports.api = functions.https.onRequest(recipesApi); |
| 9 | + |
| 10 | +exports.onCreateRecipe = functions.firestore |
| 11 | + .document("recipes/{recipeId}") |
| 12 | + .onCreate(async (snapshot) => { |
| 13 | + const countDocRef = firestore.collection("recipeCounts").doc("all"); |
| 14 | + const countDoc = await countDocRef.get(); |
| 15 | + |
| 16 | + if (countDoc.exists) { |
| 17 | + countDocRef.update({ count: admin.firestore.FieldValue.increment(1) }); |
| 18 | + } else { |
| 19 | + countDocRef.set({ count: 1 }); |
| 20 | + } |
| 21 | + |
| 22 | + const recipe = snapshot.data(); |
| 23 | + |
| 24 | + if (recipe.isPublished) { |
| 25 | + const countPublishedDocRef = firestore |
| 26 | + .collection("recipeCounts") |
| 27 | + .doc("published"); |
| 28 | + const countPublishedDoc = await countPublishedDocRef.get(); |
| 29 | + |
| 30 | + if (countPublishedDoc.exists) { |
| 31 | + countPublishedDocRef.update({ |
| 32 | + count: admin.firestore.FieldValue.increment(1), |
| 33 | + }); |
| 34 | + } else { |
| 35 | + countPublishedDocRef.set({ count: 1 }); |
| 36 | + } |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | +exports.onUpdateRecipe = functions.firestore |
| 41 | + .document("recipes/{recipeId}") |
| 42 | + .onUpdate(async (changes) => { |
| 43 | + const oldRecipe = changes.before.data(); |
| 44 | + const newRecipe = changes.after.data(); |
| 45 | + |
| 46 | + let publishCount = 0; |
| 47 | + |
| 48 | + if (!oldRecipe.isPublished && newRecipe.isPublished) { |
| 49 | + publishCount += 1; |
| 50 | + } else if (oldRecipe.isPublished && !newRecipe.isPublished) { |
| 51 | + publishCount -= 1; |
| 52 | + } |
| 53 | + |
| 54 | + if (publishCount !== 0) { |
| 55 | + const publishedCountDocRef = firestore |
| 56 | + .collection("recipeCounts") |
| 57 | + .doc("published"); |
| 58 | + |
| 59 | + const publishedCountDoc = await publishedCountDocRef.get(); |
| 60 | + |
| 61 | + if (publishedCountDoc.exists) { |
| 62 | + publishedCountDocRef.update({ |
| 63 | + count: admin.firestore.FieldValue.increment(publishCount), |
| 64 | + }); |
| 65 | + } else { |
| 66 | + if (publishCount > 0) { |
| 67 | + publishedCountDocRef.set({ count: publishCount }); |
| 68 | + } else { |
| 69 | + publishedCountDocRef.set({ count: 0 }); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | +exports.onDeleteRecipe = functions.firestore |
| 76 | + .document("recipes/{recipeId}") |
| 77 | + .onDelete(async (snapshot) => { |
| 78 | + const recipe = snapshot.data(); |
| 79 | + const imageUrl = recipe.imageUrl; |
| 80 | + |
| 81 | + if (imageUrl) { |
| 82 | + const decodedUrl = decodeURIComponent(imageUrl); |
| 83 | + const startIndex = decodedUrl.indexOf("/o/") + 3; |
| 84 | + const endIndex = decodedUrl.indexOf("?"); |
| 85 | + const fullFilePath = decodedUrl.substring(startIndex, endIndex); |
| 86 | + const file = storageBucket.file(fullFilePath); |
| 87 | + |
| 88 | + console.log(`Attemping to delete: ${fullFilePath}`); |
| 89 | + |
| 90 | + try { |
| 91 | + await file.delete(); |
| 92 | + console.log("Successfully deleted image."); |
| 93 | + } catch (error) { |
| 94 | + console.log(`Failed to delete file: ${error.message}`); |
| 95 | + } |
| 96 | + |
| 97 | + const countDocRef = firestore.collection("recipeCounts").doc("all"); |
| 98 | + const countDoc = await countDocRef.get(); |
| 99 | + |
| 100 | + if (countDoc.exists) { |
| 101 | + countDocRef.update({ count: admin.firestore.FieldValue.increment(-1) }); |
| 102 | + } else { |
| 103 | + countDocRef.set({ count: 0 }); |
| 104 | + } |
| 105 | + |
| 106 | + const recipe = snapshot.data(); |
| 107 | + |
| 108 | + if (recipe.isPublished) { |
| 109 | + const countPublishedDocRef = firestore |
| 110 | + .collection("recipeCounts") |
| 111 | + .doc("published"); |
| 112 | + const countPublishedDoc = await countPublishedDocRef.get(); |
| 113 | + |
| 114 | + if (countPublishedDoc.exists) { |
| 115 | + countPublishedDocRef.update({ |
| 116 | + count: admin.firestore.FieldValue.increment(-1), |
| 117 | + }); |
| 118 | + } else { |
| 119 | + countPublishedDocRef.set({ count: 0 }); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | +// https://crontab.guru/ |
| 126 | + |
| 127 | +const runtimeOptions = { |
| 128 | + timeoutSeconds: 300, |
| 129 | + memory: "256MB", |
| 130 | +}; |
| 131 | + |
| 132 | +exports.dailyCheckRecipePublishDate = functions |
| 133 | + .runWith(runtimeOptions) |
| 134 | + .pubsub.schedule("0 0 * * *") |
| 135 | + .onRun(async () => { |
| 136 | + console.log("dailyCheckRecipePublishDate() called - time to check"); |
| 137 | + |
| 138 | + const snapshot = await firestore |
| 139 | + .collection("recipes") |
| 140 | + .where("isPublished", "==", false) |
| 141 | + .get(); |
| 142 | + |
| 143 | + snapshot.forEach(async (doc) => { |
| 144 | + const data = doc.data(); |
| 145 | + const now = Date.now() / 1000; |
| 146 | + const isPublished = data.publishDate._seconds <= now ? true : false; |
| 147 | + |
| 148 | + if (isPublished) { |
| 149 | + console.log(`Recipe: ${data.name} is now published!`); |
| 150 | + |
| 151 | + firestore.collection("recipes").doc(doc.id).set( |
| 152 | + { |
| 153 | + isPublished, |
| 154 | + }, |
| 155 | + { |
| 156 | + merge: true, |
| 157 | + } |
| 158 | + ); |
| 159 | + } |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | +console.log("SERVER STARTED!"); |
0 commit comments