Skip to content

Commit

Permalink
feat(prayerTimes): add external prayer times and added cron it (#1)
Browse files Browse the repository at this point in the history
* 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
ayocodingit authored Mar 9, 2023
1 parent e7e919b commit 0eed7f8
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 18 deletions.
10 changes: 5 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#app
APP_NAME=Clean Architecture
APP_NAME=Aljabbar Service
APP_ENV=local
APP_PORT_HTTP=3000
APP_LOG=info
Expand All @@ -15,11 +15,11 @@ DB_AUTH_SOURCE=admin
#JWT
JWT_ACCESS_SECRET=token-secret

#Elastic
ELASTIC_CLOUD_ID=
ELASTIC_API_KEY=

#Redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_TTL=0

#PRAYER TIMES
PRAYER_TIMES_URL=
PRAYER_TIMES_LOCATION=
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
# Clean Architecture TS

<a href="https://codeclimate.com/github/ayocodingit/clean-architecture-ts/maintainability"><img src="https://api.codeclimate.com/v1/badges/386d765f6f1aa1b4a4c8/maintainability" /></a>

Reference for implementation clean architecture:

- [the clean architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)
- [hexagonal architecture](https://medium.com/ssense-tech/hexagonal-architecture-there-are-always-two-sides-to-every-story-bc0780ed7d9c)
- [project layout from golang standards](https://github.com/golang-standards/project-layout)
# Aljabbar Service

## Tech Stack

Expand All @@ -20,7 +12,7 @@ Reference for implementation clean architecture:
1. clone repository

```bash
$ git clone git@github.com:ayocodingit/clean-architecture-ts.git
$ git clone git@github.com:jabardigitalservice/aljabbar-service.git
```

2. install node
Expand Down
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"scripts": {
"start:dev": "npx nodemon",
"build": "rimraf ./build && tsc",
"start": "node build/src/main.js",
"start": "node build/src/main",
"lint:fix": "npx prettier --write .",
"cron:run": "node build/src/cron/$npm_config_name.cron",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
Expand All @@ -25,6 +26,7 @@
"@types/express": "^4.17.15",
"@types/i18n": "^0.13.6",
"@types/jsonwebtoken": "^9.0.1",
"@types/luxon": "^3.2.0",
"@types/node": "^18.11.18",
"nodemon": "^2.0.20",
"prettier": "2.8.2",
Expand All @@ -33,6 +35,7 @@
"typescript": "^4.9.4"
},
"dependencies": {
"axios": "^1.3.4",
"body-parser": "^1.20.1",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand All @@ -43,6 +46,7 @@
"i18n": "^0.15.1",
"joi": "^17.7.0",
"jsonwebtoken": "^9.0.0",
"luxon": "^3.3.0",
"mongoose": "^6.8.3",
"redis": "^4.5.1",
"winston": "^3.8.2"
Expand Down
4 changes: 4 additions & 0 deletions src/config/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export interface Config {
port: number
ttl: number
}
prayerTimes: {
url: string
location: string
}
}
2 changes: 2 additions & 0 deletions src/config/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ export default Joi.object({
REDIS_HOST: Joi.string().optional(),
REDIS_PORT: Joi.number().optional(),
REDIS_TTL: Joi.number().optional(),
PRAYER_TIMES_URL: Joi.string().uri().required(),
PRAYER_TIMES_LOCATION: Joi.string().required(),
})
4 changes: 4 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const config: Config = {
port: env.REDIS_PORT,
ttl: env.REDIS_TTL,
},
prayerTimes: {
url: env.PRAYER_TIMES_URL,
location: env.PRAYER_TIMES_LOCATION,
},
}

export default config
41 changes: 41 additions & 0 deletions src/cron/prayerTimes.cron.ts
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()
2 changes: 1 addition & 1 deletion src/database/mongo/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Mongo {
public static async connect(logger: winston.Logger, { db }: Config) {
mongoose.set('strictQuery', false)
return mongoose
.connect(`mongodb://${db.host}:${db.port}/${db.database}`, {
.connect(`mongodb://${db.host}:${db.port}/${db.name}`, {
authSource: db.auth_source,
pass: db.password,
user: db.username,
Expand Down
23 changes: 23 additions & 0 deletions src/database/mongo/schema/prayerTimes.schema.ts
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 removed src/database/mongo/schemas/.keep
Empty file.
Empty file removed src/external/.keep
Empty file.
74 changes: 74 additions & 0 deletions src/external/prayerTimes.ts
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
5 changes: 5 additions & 0 deletions src/helpers/date.ts
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()
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2016",
"target": "es2017",
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
Expand Down

0 comments on commit 0eed7f8

Please sign in to comment.