Skip to content

Commit

Permalink
feat(prayerTime): add endpoint prayer time by date (#4)
Browse files Browse the repository at this point in the history
* feat(prayerTime): add endpoint prayer time by date

* feat(prayerTime): changes by location to longlat
  • Loading branch information
ayocodingit authored Mar 9, 2023
1 parent a1a859c commit 9aae3f4
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ REDIS_TTL=0

#PRAYER TIMES
PRAYER_TIMES_URL=
PRAYER_TIMES_LOCATION=
PRAYER_TIMES_LONGITUDE=
PRAYER_TIMES_LATITUDE=

#Jabarprov
JABARPROV_URL=
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start:dev": "npx nodemon",
"build": "rimraf ./build && tsc",
"start": "node build/src/main",
"lint:fix": "npx prettier --write .",
"lint:fix": "npx prettier --write ./src",
"cron:run": "node build/src/cron/$npm_config_name.cron",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
3 changes: 2 additions & 1 deletion src/config/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export interface Config {
}
prayerTimes: {
url: string
location: string
longitude: number
latitude: number
}
jabarprov: {
url: string
Expand Down
3 changes: 2 additions & 1 deletion src/config/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default Joi.object({
REDIS_PORT: Joi.number().optional(),
REDIS_TTL: Joi.number().optional(),
PRAYER_TIMES_URL: Joi.string().uri().required(),
PRAYER_TIMES_LOCATION: Joi.string().required(),
PRAYER_TIMES_LONGITUDE: Joi.number().required(),
PRAYER_TIMES_LATITUDE: Joi.number().required(),
JABARPROV_URL: Joi.string().uri().required(),
JABARPROV_KEYWORD: Joi.string().required(),
})
3 changes: 2 additions & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const config: Config = {
},
prayerTimes: {
url: env.PRAYER_TIMES_URL,
location: env.PRAYER_TIMES_LOCATION,
longitude: env.PRAYER_TIMES_LONGITUDE,
latitude: env.PRAYER_TIMES_LATITUDE,
},
jabarprov: {
url: env.JABARPROV_URL,
Expand Down
10 changes: 2 additions & 8 deletions src/cron/prayerTimes.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ const prayerTimes = async () => {

const prayerTimes = new PrayerTimes(config, logger)
const today = DateTime.now()
const calendarByCity = await prayerTimes.CalenderByCity(
today.year,
today.month,
config.prayerTimes.location
)
const calendar = await prayerTimes.Calender(today.year, today.month)

for (const item of calendarByCity) {
for (const item of calendar) {
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),
},
{
Expand Down
4 changes: 0 additions & 4 deletions src/database/mongo/schema/prayerTimes.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ const schema = new Schema(
type: Date,
required: true,
},
location: {
type: String,
required: true,
},
},
{
versionKey: false,
Expand Down
42 changes: 6 additions & 36 deletions src/external/prayerTimes.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
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
) => {
public Calender = async (year: number, month: number) => {
try {
const response = await axios.get(
`${this.config.prayerTimes.url}/calendarByCity/${year}/${month}`,
`${this.config.prayerTimes.url}/calendar/${year}/${month}`,
{
params: {
...this.options,
city,
longitude: this.config.prayerTimes.longitude,
latitude: this.config.prayerTimes.latitude,
},
}
)
Expand All @@ -35,35 +29,11 @@ class PrayerTimes {
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}`, '')
const time = timings[key].replace(` ${replace}`, '')
timings[key] = time
}
}

Expand Down
Empty file.
Empty file removed src/internal/example/example.ts
Empty file.
Empty file.
Empty file removed src/internal/example/usecase/.keep
Empty file.
24 changes: 24 additions & 0 deletions src/internal/prayerTimes/delivery/http/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NextFunction, Request, Response } from 'express'
import winston from 'winston'
import Usecase from '../../usecase/usecase'
import { FindByDate } from '../../entity/schema'
import { ValidateParams } from '../../../../helpers/validate'
import statusCode from '../../../../pkg/statusCode'

class Handler {
constructor(private usecase: Usecase, private logger: winston.Logger) {}
public FindByDate() {
return async (req: Request, res: Response, next: NextFunction) => {
try {
const value = ValidateParams(FindByDate, req.params.date)

const result = await this.usecase.FindByDate(value)
return res.status(statusCode.OK).json({ data: result })
} catch (error) {
return next(error)
}
}
}
}

export default Handler
3 changes: 3 additions & 0 deletions src/internal/prayerTimes/entity/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface FindByDate {
date: Date
}
3 changes: 3 additions & 0 deletions src/internal/prayerTimes/entity/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Joi from 'joi'

export const FindByDate = Joi.string().isoDate().required()
26 changes: 26 additions & 0 deletions src/internal/prayerTimes/prayerTimes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import winston from 'winston'
import { Config } from '../../config/config.interface'
import Http from '../../transport/http/http'
import Handler from './delivery/http/handler'
import Repository from './repository/mongo/repository'
import Usecase from './usecase/usecase'

class PrayerTime {
constructor(
private http: Http,
private logger: winston.Logger,
private config: Config
) {
const repository = new Repository(logger)
const usecase = new Usecase(repository, logger)

this.loadHttp(usecase)
}

private loadHttp(usecase: Usecase) {
const handler = new Handler(usecase, this.logger)
this.http.app.get('/v1/prayer-times/:date', handler.FindByDate())
}
}

export default PrayerTime
13 changes: 13 additions & 0 deletions src/internal/prayerTimes/repository/mongo/repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import winston from 'winston'
import prayerTimesSchema from '../../../../database/mongo/schema/prayerTimes.schema'

class Repository {
private prayerTime = prayerTimesSchema
constructor(private logger: winston.Logger) {}

public async FindByDate(date: Date) {
return this.prayerTime.findOne({ date })
}
}

export default Repository
23 changes: 23 additions & 0 deletions src/internal/prayerTimes/usecase/usecase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import winston from 'winston'
import { Translate } from '../../../helpers/translate'
import error from '../../../pkg/error'
import statusCode from '../../../pkg/statusCode'
import Repository from '../repository/mongo/repository'

class Usecase {
constructor(
private repository: Repository,
private logger: winston.Logger
) {}

public async FindByDate(date: Date) {
const item = await this.repository.FindByDate(date)

if (!item)
throw new error(statusCode.NOT_FOUND, Translate('not_found', {}))

return item
}
}

export default Usecase
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import config from './config/config'
import Mongo from './database/mongo/mongo'
import PrayerTime from './internal/prayerTimes/prayerTimes'
import Logger from './pkg/logger'
import Redis from './pkg/redis'
import Http from './transport/http/http'
Expand All @@ -10,6 +11,9 @@ const main = async () => {
const redis = new Redis(config, logger)
const http = new Http(logger, config)

// Load App Internal
new PrayerTime(http, logger, config)

if (config.app.env !== 'test') {
http.Run(config.app.port.http)
}
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/lang/en.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"not_found": "Not Found!"
}
2 changes: 0 additions & 2 deletions src/transport/http/middleware/verifyAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export const VerifyAuth = (
options?: jwt.VerifyOptions
) => {
return (req: any, res: Response, next: NextFunction) => {
console.log(secretOrPublicKey)

const { authorization } = req.headers

if (!authorization) {
Expand Down

0 comments on commit 9aae3f4

Please sign in to comment.