Last active
April 19, 2020 18:12
-
-
Save armanso/3931465e39d224cde2883110661d1fca to your computer and use it in GitHub Desktop.
Index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as functions from 'firebase-functions'; | |
import * as admin from 'firebase-admin'; | |
import axios from "axios" | |
import * as path from 'path' | |
import * as os from 'os' | |
import * as fs from 'fs' | |
import { PublishedReviews, Config } from 'app-reviews/lib/global-types'; | |
import Reviews from 'app-reviews' | |
import serviceAccount from './google-services.json' | |
let firstTimeRunning = false | |
const bucketFilePath = `apps-reviews/published_reviews.json` | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount as admin.ServiceAccount), | |
storageBucket: "[project bucket name]" | |
}); | |
const bucket = admin.storage().bucket() | |
const storePublishedReviewsList = async (reviews: PublishedReviews) => { | |
let tempPath = path.join(os.tmpdir(), '/temp.json'); | |
fs.writeFileSync(tempPath, JSON.stringify(reviews), { flag: 'w' }) | |
await bucket.upload(tempPath, { | |
destination: bucketFilePath | |
}) | |
} | |
const retrivePublishedReviewsList = async (): Promise<PublishedReviews> => { | |
try { | |
let file = await bucket.file(bucketFilePath).download() | |
return JSON.parse(file.toString()) | |
} catch { | |
// first time running | |
firstTimeRunning = true | |
return {} | |
} | |
} | |
const onNewMessageAvailable = async (messages: string[]) => { | |
if(firstTimeRunning) | |
return | |
var requets = messages.map(message => { | |
return axios.post( | |
"[slack webhook]", | |
message | |
) | |
}) | |
await Promise.all(requets) | |
} | |
const config: Config = { | |
apps: [ | |
{ id: "com.you.app", showAppIcon: true, publisherKey: "./api-key.json" }, | |
{ id: "appstore-id", showAppIcon: true, regions: 'all' } | |
], | |
storePublishedReviewsList, | |
retrivePublishedReviewsList, | |
onNewMessageAvailable | |
} | |
exports.appReviews = functions.runWith({ timeoutSeconds: 5 * 60 }) | |
.pubsub | |
.schedule('every 15 minutes').onRun(async context => { | |
await new Reviews(config).init() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment