feat: fixes and formatter
This commit is contained in:
+32
-39
@@ -1,22 +1,22 @@
|
||||
const admin = require("firebase-admin");
|
||||
const { FieldValue } = require("firebase-admin/firestore");
|
||||
const { onSchedule } = require("firebase-functions/v2/scheduler");
|
||||
const { refList } = require("../index");
|
||||
const firestore = refList.projects.firestore;
|
||||
const admin = require('firebase-admin')
|
||||
const { FieldValue } = require('firebase-admin/firestore')
|
||||
const { onSchedule } = require('firebase-functions/v2/scheduler')
|
||||
const { refList } = require('../index')
|
||||
const firestore = refList.projects.firestore
|
||||
|
||||
function buildMonthContext(referenceDate) {
|
||||
const current = referenceDate ? new Date(referenceDate) : new Date();
|
||||
current.setHours(0, 0, 0, 0);
|
||||
current.setDate(1);
|
||||
const current = referenceDate ? new Date(referenceDate) : new Date()
|
||||
current.setHours(0, 0, 0, 0)
|
||||
current.setDate(1)
|
||||
|
||||
const target = new Date(current);
|
||||
target.setMonth(target.getMonth() - 1);
|
||||
const target = new Date(current)
|
||||
target.setMonth(target.getMonth() - 1)
|
||||
|
||||
const month = target.getMonth();
|
||||
const year = target.getFullYear();
|
||||
const monthKey = `${year}-${String(month + 1).padStart(2, "0")}`;
|
||||
const rangeStart = new Date(year, month, 1, 0, 0, 0, 0);
|
||||
const rangeEnd = new Date(year, month + 1, 0, 23, 59, 59, 999);
|
||||
const month = target.getMonth()
|
||||
const year = target.getFullYear()
|
||||
const monthKey = `${year}-${String(month + 1).padStart(2, '0')}`
|
||||
const rangeStart = new Date(year, month, 1, 0, 0, 0, 0)
|
||||
const rangeEnd = new Date(year, month + 1, 0, 23, 59, 59, 999)
|
||||
|
||||
return {
|
||||
year,
|
||||
@@ -24,27 +24,22 @@ function buildMonthContext(referenceDate) {
|
||||
monthKey,
|
||||
rangeStart,
|
||||
rangeEnd,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
exports.snapshotMonthlyTopSongs = onSchedule(
|
||||
{
|
||||
schedule: "5 0 1 * *",
|
||||
timeZone: "Europe/Paris",
|
||||
schedule: '5 0 1 * *',
|
||||
timeZone: 'Europe/Paris',
|
||||
},
|
||||
async (event) => {
|
||||
const { scheduleTime } = event;
|
||||
const context = buildMonthContext(
|
||||
scheduleTime ? new Date(scheduleTime) : new Date()
|
||||
);
|
||||
const { scheduleTime } = event
|
||||
const context = buildMonthContext(scheduleTime ? new Date(scheduleTime) : new Date())
|
||||
|
||||
const topProjectsSnap = await refList.projects
|
||||
.orderBy("views", "desc")
|
||||
.limit(3)
|
||||
.get();
|
||||
const topProjectsSnap = await refList.projects.orderBy('views', 'desc').limit(3).get()
|
||||
|
||||
const topProjects = topProjectsSnap.docs.map((doc, index) => {
|
||||
const data = doc.data() || {};
|
||||
const data = doc.data() || {}
|
||||
return {
|
||||
rank: index + 1,
|
||||
projectId: doc.id,
|
||||
@@ -54,14 +49,12 @@ exports.snapshotMonthlyTopSongs = onSchedule(
|
||||
coverUrl: data.coverUrl || null,
|
||||
songUrl: data.songUrl || null,
|
||||
views: data.views || 0,
|
||||
};
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
const docRef = firestore
|
||||
.collection("monthlyTopSongs")
|
||||
.doc(context.monthKey);
|
||||
const docRef = firestore.collection('monthlyTopSongs').doc(context.monthKey)
|
||||
|
||||
const existingSnapshot = await docRef.get();
|
||||
const existingSnapshot = await docRef.get()
|
||||
const payload = {
|
||||
monthKey: context.monthKey,
|
||||
month: context.month,
|
||||
@@ -73,12 +66,12 @@ exports.snapshotMonthlyTopSongs = onSchedule(
|
||||
topProjects,
|
||||
totalProjects: topProjects.length,
|
||||
updatedAt: FieldValue.serverTimestamp(),
|
||||
};
|
||||
|
||||
if (!existingSnapshot.exists) {
|
||||
payload.createdAt = FieldValue.serverTimestamp();
|
||||
}
|
||||
|
||||
await docRef.set(payload, { merge: true });
|
||||
if (!existingSnapshot.exists) {
|
||||
payload.createdAt = FieldValue.serverTimestamp()
|
||||
}
|
||||
|
||||
await docRef.set(payload, { merge: true })
|
||||
}
|
||||
);
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user