-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prayerTimes): add external prayer times and added cron it (#1)
* feat(prayerTimes): add external prayer times and added cron it * add shorcut for cron run * move method FormatTimings to extenal prayer Times
- Loading branch information
1 parent
e7e919b
commit 0eed7f8
Showing
15 changed files
with
226 additions
and
18 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,8 @@ export interface Config { | |
port: number | ||
ttl: number | ||
} | ||
prayerTimes: { | ||
url: string | ||
location: string | ||
} | ||
} |
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,41 @@ | ||
import config from '../config/config' | ||
import PrayerTimes from '../external/prayerTimes' | ||
import Logger from '../pkg/logger' | ||
import { DateTime } from 'luxon' | ||
import { ConvertTimestampToISODate } from '../helpers/date' | ||
import prayerTimesSchema from '../database/mongo/schema/prayerTimes.schema' | ||
import Mongo from '../database/mongo/mongo' | ||
|
||
const prayerTimes = async () => { | ||
const { logger } = new Logger(config) | ||
await Mongo.connect(logger, config) | ||
|
||
const prayerTimes = new PrayerTimes(config, logger) | ||
const today = DateTime.now() | ||
const calendarByCity = await prayerTimes.CalenderByCity( | ||
today.year, | ||
today.month, | ||
config.prayerTimes.location | ||
) | ||
|
||
for (const item of calendarByCity) { | ||
await prayerTimesSchema.updateOne( | ||
{ | ||
location: config.prayerTimes.location, | ||
date: ConvertTimestampToISODate(item.date.timestamp), | ||
}, | ||
{ | ||
timings: prayerTimes.FormatTimings(item.timings, '(WIB)'), | ||
location: config.prayerTimes.location, | ||
date: ConvertTimestampToISODate(item.date.timestamp), | ||
}, | ||
{ | ||
upsert: true, | ||
} | ||
) | ||
} | ||
|
||
process.exit() | ||
} | ||
|
||
export default prayerTimes() |
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,23 @@ | ||
import mongoose, { Schema } from 'mongoose' | ||
|
||
const schema = new Schema( | ||
{ | ||
timings: { | ||
type: Object, | ||
required: true, | ||
}, | ||
date: { | ||
type: Date, | ||
required: true, | ||
}, | ||
location: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
{ | ||
versionKey: false, | ||
} | ||
) | ||
|
||
export default mongoose.model('prayerTimes', schema, 'prayerTimes') |
Empty file.
Empty file.
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,74 @@ | ||
import axios from 'axios' | ||
import winston from 'winston' | ||
import { Config } from '../config/config.interface' | ||
import { ConvertTimestampToISODate } from '../helpers/date' | ||
|
||
class PrayerTimes { | ||
private options = { | ||
country: 'IDN', | ||
iso8601: true, | ||
tune: '1,1,1,1,0,0', | ||
method: 11, | ||
} | ||
|
||
constructor(private config: Config, private logger: winston.Logger) {} | ||
|
||
public CalenderByCity = async ( | ||
year: number, | ||
month: number, | ||
city: string | ||
) => { | ||
try { | ||
const response = await axios.get( | ||
`${this.config.prayerTimes.url}/calendarByCity/${year}/${month}`, | ||
{ | ||
params: { | ||
...this.options, | ||
city, | ||
}, | ||
} | ||
) | ||
const { data } = response.data | ||
return data | ||
} catch (error) { | ||
this.logger.error(error) | ||
throw error | ||
} | ||
} | ||
|
||
public TimingsByCity = async (date: string, city: string) => { | ||
try { | ||
const response = await axios.get( | ||
`${this.config.prayerTimes.url}/timingsByCity/${date}`, | ||
{ | ||
params: { | ||
...this.options, | ||
city, | ||
}, | ||
} | ||
) | ||
|
||
const { data } = response.data | ||
return { | ||
timings: data.timings, | ||
date: ConvertTimestampToISODate(data.date.timestamp), | ||
} | ||
} catch (error) { | ||
this.logger.error(error) | ||
throw error | ||
} | ||
} | ||
|
||
public FormatTimings = (timings: any, replace: string) => { | ||
for (const key in timings) { | ||
if (Object.prototype.hasOwnProperty.call(timings, key)) { | ||
const element = timings[key] | ||
timings[key] = element.replace(` ${replace}`, '') | ||
} | ||
} | ||
|
||
return timings | ||
} | ||
} | ||
|
||
export default PrayerTimes |
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,5 @@ | ||
import { DateTime } from 'luxon' | ||
|
||
export const ConvertTimestampToISODate = (date: string) => { | ||
return DateTime.fromSeconds(parseInt(date)).toISODate() | ||
} |
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