Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
feat: schedule clean invitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Haoran committed Apr 14, 2021
1 parent 37a63af commit c94cc84
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const sgMail = require('@sendgrid/mail')
const schedule = require('./schedule')

exports.cleanInvitation = schedule.cleanInvitation

let config, url
// any better way to do this?
Expand Down
25 changes: 25 additions & 0 deletions functions/schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const functions = require('firebase-functions')
const admin = require('firebase-admin')

exports.cleanInvitation = functions
.region('europe-west1')
.pubsub.schedule('0 15 * * *')
.timeZone('Europe/Copenhagen')
.onRun(() => {
const last_8_dates = [...Array(8)].map((_, i) => {
const day = new Date()
day.setDate(day.getDate() - i)
return day.toISOString().slice(0, 10)
})

const query = admin
.firestore()
.collection('invitations')
.where('date', 'not-in', last_8_dates)

query.get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
doc.ref.delete()
})
})
})

0 comments on commit c94cc84

Please sign in to comment.