-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
6,253 additions
and
3,386 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
import mongoose from "mongoose"; | ||
import config from "../config"; | ||
import File from "../models/File"; | ||
import Film from "../models/Film"; | ||
import Keyword from "../models/Keyword"; | ||
import User from "../models/User"; | ||
import mongoose from 'mongoose'; | ||
import config from '../config'; | ||
import File from '../models/File'; | ||
import Film from '../models/Film'; | ||
import Keyword from '../models/Keyword'; | ||
import User from '../models/User'; | ||
import * as admin from 'firebase-admin'; | ||
|
||
var serviceAccount = require('../../firebase-admin.json'); | ||
|
||
const connectDB = async () => { | ||
try { | ||
await mongoose.connect(config.mongoURI); | ||
|
||
mongoose.set('autoCreate', true); | ||
|
||
User.createCollection().then(function (collection) { | ||
console.log("User Collection is created!"); | ||
}); | ||
|
||
File.createCollection().then(function (collection) { | ||
console.log("File Collection is created!"); | ||
}); | ||
|
||
Film.createCollection().then(function (collection) { | ||
console.log("Film Collection is created!"); | ||
}); | ||
|
||
Keyword.createCollection().then(function (collection) { | ||
console.log("Keyword Collection is created!"); | ||
}); | ||
|
||
console.log("Mongoose Connected ..."); | ||
} catch (err: any) { | ||
console.error(err.message); | ||
process.exit(1); | ||
} | ||
try { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount as admin.ServiceAccount), | ||
}); | ||
|
||
await mongoose.connect(config.mongoURI); | ||
|
||
mongoose.set('autoCreate', true); | ||
|
||
User.createCollection().then(function (collection) { | ||
console.log('User Collection is created!'); | ||
}); | ||
|
||
File.createCollection().then(function (collection) { | ||
console.log('File Collection is created!'); | ||
}); | ||
|
||
Film.createCollection().then(function (collection) { | ||
console.log('Film Collection is created!'); | ||
}); | ||
|
||
Keyword.createCollection().then(function (collection) { | ||
console.log('Keyword Collection is created!'); | ||
}); | ||
|
||
console.log('Mongoose Connected ...'); | ||
} catch (err: any) { | ||
console.error(err.message); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
export default connectDB; | ||
export default connectDB; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import User from '../models/User'; | ||
import * as admin from 'firebase-admin'; | ||
import mongoose from 'mongoose'; | ||
import Film from '../models/Film'; | ||
|
||
var nodeschedule = require('node-schedule'); | ||
|
||
const postCapsuleNotice = async (userId: string) => { | ||
try { | ||
const user = await User.find({ _id: userId }, { fcmToken: 1, count: 1 }); | ||
if (!user) { | ||
return null; | ||
} | ||
|
||
const count = user[0].count; | ||
if ((count as number) % 10 !== 0) { | ||
return; | ||
} | ||
|
||
const fcmToken = user[0].fcmToken; | ||
|
||
const message = { | ||
android: { | ||
data: { | ||
title: 'happic', | ||
body: '어제 떨어진 별똥별을 선물로 가져왔어!', | ||
}, | ||
}, | ||
apns: { | ||
payload: { | ||
aps: { | ||
contentAvailable: true, | ||
alert: { | ||
title: 'happic', | ||
body: '어제 떨어진 별똥별을 선물로 가져왔어!', | ||
}, | ||
}, | ||
}, | ||
}, | ||
token: fcmToken, | ||
}; | ||
|
||
// 푸시알림 보내기 | ||
admin | ||
.messaging() | ||
.send(message) | ||
.then(function (response: any) { | ||
// const rule = '0 0 8 * * *'; | ||
// nodeschedule.scheduleJob(rule, function () { | ||
// console.log('스케줄러 성공!'); | ||
// }); | ||
console.log('Successfully sent message: ', response); | ||
}) | ||
.catch(function (err) { | ||
console.log('Error Sending message!!! : ', err); | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
throw err; | ||
} | ||
}; | ||
|
||
const postCheckNotice = async (userId: string) => { | ||
const dayjs = require('dayjs'); | ||
try { | ||
let films = await Film.find({ writer: userId }).sort({ createdAt: -1 }); | ||
|
||
const lastDate = dayjs(films[0].createdAt); | ||
const lastDateFormat = lastDate.format('YY-MM-DD'); | ||
|
||
const nowDate = dayjs(); | ||
const nowDateFormat = nowDate.format('YY-MM-DD'); | ||
|
||
const todayPosted = lastDateFormat === nowDateFormat; | ||
|
||
if (todayPosted == true) { | ||
return; | ||
} | ||
|
||
const user = await User.find({ _id: userId }, { fcmToken: 1 }); | ||
if (!user) { | ||
return null; | ||
} | ||
const fcmToken = user[0].fcmToken; | ||
const message = { | ||
android: { | ||
data: { | ||
title: 'happic', | ||
body: '길잡이와 함께 해픽을 행복으로 채워보세요!', | ||
}, | ||
}, | ||
apns: { | ||
payload: { | ||
aps: { | ||
contentAvailable: true, | ||
alert: { | ||
title: 'happic', | ||
body: '길잡이와 함께 해픽을 행복으로 채워보세요!', | ||
}, | ||
}, | ||
}, | ||
}, | ||
token: fcmToken, | ||
}; | ||
// 푸시알림 보내기 | ||
admin | ||
.messaging() | ||
.send(message) | ||
.then(function (response: any) { | ||
// const rule = '0 0 22 * * *'; | ||
// nodeschedule.scheduleJob(rule, function () { | ||
// console.log('스케줄러 성공!'); | ||
// }); | ||
console.log('Successfully sent message: ', response); | ||
}) | ||
.catch(function (err) { | ||
console.log('Error Sending message!!! : ', err); | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
throw err; | ||
} | ||
}; | ||
|
||
export default { | ||
postCapsuleNotice, | ||
postCheckNotice, | ||
}; |
Oops, something went wrong.