Skip to content

Commit 8c4d2bd

Browse files
author
simonho
committed
fix: removed duplicated cfw bindings in backend
1 parent 1a3c5ae commit 8c4d2bd

20 files changed

Lines changed: 96 additions & 131 deletions

src/api/google.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Hono } from 'hono'
2-
import { Bindings, Env } from '@/bindings'
2+
import { Bindings } from '@/bindings'
33
// import { nanoid } from 'nanoid'
44
import getCurrentLine from 'get-current-line'
55

src/api/nonLoggedIn.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { Hono } from 'hono'
66
import { html } from 'hono/html'
7-
import { Bindings, Env } from '@/bindings'
7+
import { Bindings } from '@/bindings'
88
import { nanoid } from 'nanoid'
99
import getCurrentLine from 'get-current-line'
1010
import jwt from '@tsndr/cloudflare-worker-jwt'
@@ -438,7 +438,7 @@ nonLoggedInApi.post('/_getOneUnit', async (c) => {
438438
Util.logCurLine(getCurrentLine())
439439

440440
type Param = { userId: string }
441-
const env: Env = c.env
441+
// const env: Env = c.env
442442

443443
try {
444444
const authHdr = c.req.headers.get('Authorization')
@@ -462,7 +462,7 @@ nonLoggedInApi.post('/scanUnitQrcode', async (c) => {
462462
Util.logCurLine(getCurrentLine())
463463

464464
type Param = { url: string }
465-
const env: Env = c.env
465+
// const env: Env = c.env
466466

467467
try {
468468
let param = await c.req.json() as Param
@@ -473,11 +473,11 @@ nonLoggedInApi.post('/scanUnitQrcode', async (c) => {
473473
if (!unitId) throw new Error('invalid code b')
474474

475475
console.log('Codes', userId, unitId)
476-
let resp = await env.DB.prepare(`SELECT type,block,floor,number FROM Units WHERE id=? AND userId=?`).bind(unitId, userId).first() as any
476+
let resp = await c.env.DB.prepare(`SELECT type,block,floor,number FROM Units WHERE id=? AND userId=?`).bind(unitId, userId).first() as any
477477
if (resp == null) throw new Error('unit not found')
478478
const { type, block, floor, number } = resp
479479

480-
let estate = await env.DB.prepare(`SELECT id,name,address,contact,langEntries,timezone,timezoneMeta,currency,tenantApp FROM Estates WHERE userId=?`).bind(userId).first() as any
480+
let estate = await c.env.DB.prepare(`SELECT id,name,address,contact,langEntries,timezone,timezoneMeta,currency,tenantApp FROM Estates WHERE userId=?`).bind(userId).first() as any
481481
if (estate == null) throw new Error('estate not found')
482482

483483
// Creating a expirable JWT & return it in JSON
@@ -518,7 +518,7 @@ nonLoggedInApi.post('/createNewTenant', async (c) => {
518518
role: string
519519
fcmDeviceToken: string
520520
}
521-
const env: Env = c.env
521+
// const env: Env = c.env
522522

523523
try {
524524
const param = await c.req.json() as Param
@@ -533,13 +533,13 @@ nonLoggedInApi.post('/createNewTenant', async (c) => {
533533
if (['owner', 'tenant', 'occupant', 'agent'].includes(param.role) == false) throw new Error(`invalid role: ${param.role}`)
534534

535535
// Create a new tenant record
536-
const { tenant, unit } = await TenantModel.tryCreateTenant(env, param.userId, param.unitId, param.name, param.email, param.password, param.phone, param.role, param.fcmDeviceToken)
536+
const { tenant, unit } = await TenantModel.tryCreateTenant(c.env, param.userId, param.unitId, param.name, param.email, param.password, param.phone, param.role, param.fcmDeviceToken)
537537

538538
// If there is Firebase device token, subscribe to corresponding FCM topics
539539
if (param.fcmDeviceToken) {
540-
if (env.FCM_SERVER_KEY == null) throw new Error('FCM_SERVER_KEY not defined')
540+
if (c.env.FCM_SERVER_KEY == null) throw new Error('FCM_SERVER_KEY not defined')
541541

542-
const serverKey = env.FCM_SERVER_KEY
542+
const serverKey = c.env.FCM_SERVER_KEY
543543
const devToken = param.fcmDeviceToken
544544
// 1. Subscribe to userId
545545
await FirebaseUtil.fcmSubscribeDeviceToTopic(serverKey, devToken, param.userId)

src/bindings.d.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,4 @@
1-
export interface Env {
2-
DB: D1Database
3-
ENCRYPTION_KEY: string
4-
USER_ENCRYPTION_KEY: string
5-
TENANT_ENCRYPTION_KEY: string
6-
API_SECRET: string
7-
DBINIT_SECRET: string
8-
S3_ACCESS_KEY: string
9-
S3_ACCESS_SECRET: string
10-
S3_BUCKET: string
11-
S3_REGION: string
12-
S3_HOST: string
13-
S3_ENDPOINT: string
14-
IS_DEBUG: string
15-
MAILGUN_API_KEY: string
16-
MAILGUN_API_URL: string
17-
SYSTEM_HOST: string
18-
SYSTEM_EMAIL_SENDER: string
19-
TURNSTILE_SECRET: string
20-
INITIAL_ADMIN_EMAIL: string
21-
INITIAL_ADMIN_PASSWORD: string
22-
INITIAL_TENANT_EMAIL: string
23-
INITIAL_TENANT_PASSWORD: string
24-
NOTIFICATION_ICON_URL: string
25-
FIREBASE_PROJECT_ID: string
26-
FCM_SERVER_KEY: string
27-
GOOGLE_CLIENT_ID: string
28-
GOOGLE_CLIENT_SECRET: string
29-
GOOGLE_SRVACC_EMAIL: string
30-
GOOGLE_SRVACC_PRIVATE_KEY: string
31-
TEST_DEVICE_TOKEN: string
32-
}
33-
341
export interface Bindings {
35-
env: Env
362
DB: D1Database
373
ENCRYPTION_KEY: string
384
USER_ENCRYPTION_KEY: string

src/data/samples/insertOthers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Insert sample data for the tables other than the units table
33
*/
44

5-
import { Env } from '@/bindings'
5+
import { Bindings } from '@/bindings'
66
// import { Env } from '../../bindings'
77
import moment from 'moment'
88
import { nanoid } from 'nanoid'
@@ -14,7 +14,7 @@ import { Util } from '../../util'
1414
// 1. Admin User: provides the email and password
1515
// 2. A sample tenant: provides the phone no. and password
1616
export const insertSampleOthers = async (
17-
env: Env,
17+
env: Bindings,
1818
adminEmail: string,
1919
adminPwd: string,
2020
) => {

src/data/samples/insertUnits.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// import { Env } from '@/bindings'
66
import getCurrentLine from 'get-current-line'
77

8-
import { Env } from '../../bindings'
8+
import { Bindings } from '../../bindings'
99
import { nanoid } from 'nanoid'
1010
import { Constant } from '../../const'
1111
import { Util } from '../../util'
@@ -1927,7 +1927,7 @@ function parseCsv(csv: string): Unit[] {
19271927
return units
19281928
}
19291929

1930-
export const insertSampleUnits = async (env: Env) => {
1930+
export const insertSampleUnits = async (env: Bindings) => {
19311931
let sql: string
19321932
let rst: D1Result
19331933
let rst2: D1Result[]

src/data/schema/createTables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import getCurrentLine from 'get-current-line'
66

7-
import { Env } from '../../bindings'
7+
import { Bindings } from '../../bindings'
88
import { Util } from '../../util'
99

1010
const CREATE_TABLES_SQL = `
@@ -233,7 +233,7 @@ CREATE INDEX idx_tenamenbkgs_bookingno on TenantAmenityBookings (bookingNo);
233233
CREATE INDEX idx_loops_tenantid on Loops (tenantId);
234234
`
235235

236-
export const createTables = async (env: Env) => {
236+
export const createTables = async (env: Bindings) => {
237237
let sql: string
238238
let rst: D1Result
239239

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Env } from '@/bindings'
21
import { Hono } from 'hono'
32
import getCurrentLine from 'get-current-line'
43
import { cors } from 'hono/cors'

src/models/amenity.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import getCurrentLine from 'get-current-line'
22

3-
import { Env } from '@/bindings'
3+
import { Bindings } from '@/bindings'
44
import { nanoid } from 'nanoid'
55

66
import { Util } from '../util'
@@ -26,7 +26,7 @@ export interface IAmenity {
2626
}
2727

2828
// D1 doc: https://developers.cloudflare.com/d1/client-api
29-
export const getById = async (env: Env, id: string, fields?: string)
29+
export const getById = async (env: Bindings, id: string, fields?: string)
3030
: Promise<IAmenity | undefined> => {
3131
Util.logCurLine(getCurrentLine())
3232

@@ -38,7 +38,7 @@ export const getById = async (env: Env, id: string, fields?: string)
3838
return record
3939
}
4040

41-
export const getAll = async (env: Env, userId: string, crit?: string, fields?: string, sort?: string, pageNo?: number, pageSize?: number)
41+
export const getAll = async (env: Bindings, userId: string, crit?: string, fields?: string, sort?: string, pageNo?: number, pageSize?: number)
4242
: Promise<IAmenity[] | undefined> => {
4343
Util.logCurLine(getCurrentLine())
4444
if (userId == null) throw new Error('Missing parameter: userId')
@@ -57,7 +57,7 @@ export const getAll = async (env: Env, userId: string, crit?: string, fields?: s
5757
return records
5858
}
5959

60-
export const create = async (env: Env, userId: string, param: any)
60+
export const create = async (env: Bindings, userId: string, param: any)
6161
: Promise<IAmenity | undefined> => {
6262
Util.logCurLine(getCurrentLine())
6363
if (param == null) throw new Error('Missing parameters')
@@ -116,7 +116,7 @@ export const create = async (env: Env, userId: string, param: any)
116116
return newRec;
117117
}
118118

119-
export const updateById = async (env: Env, id: string, param: any)
119+
export const updateById = async (env: Bindings, id: string, param: any)
120120
: Promise<boolean> => {
121121
Util.logCurLine(getCurrentLine())
122122
if (id == null) throw new Error('Missing id')
@@ -149,7 +149,7 @@ export const updateById = async (env: Env, id: string, param: any)
149149
return true
150150
}
151151

152-
export const deleteById = async (env: Env, id: string)
152+
export const deleteById = async (env: Bindings, id: string)
153153
: Promise<boolean> => {
154154
Util.logCurLine(getCurrentLine())
155155
if (id == null) throw new Error('Missing id')

src/models/estate.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import getCurrentLine from 'get-current-line'
22

3-
import { Env } from '@/bindings'
3+
import { Bindings } from '@/bindings'
44
import { nanoid } from 'nanoid'
55

66
import { Util } from '../util'
@@ -23,7 +23,7 @@ export interface IEstate {
2323
}
2424

2525
// D1 doc: https://developers.cloudflare.com/d1/client-api
26-
export const getById = async (env: Env, id: string, fields?: string)
26+
export const getById = async (env: Bindings, id: string, fields?: string)
2727
: Promise<IEstate | undefined> => {
2828
Util.logCurLine(getCurrentLine())
2929
if (id == null) throw new Error('Missing parameter: id')
@@ -34,7 +34,7 @@ export const getById = async (env: Env, id: string, fields?: string)
3434
return record
3535
}
3636

37-
export const getAll = async (env: Env, userId: string, crit?: string, fields?: string, sort?: string, pageNo?: number, pageSize?: number)
37+
export const getAll = async (env: Bindings, userId: string, crit?: string, fields?: string, sort?: string, pageNo?: number, pageSize?: number)
3838
: Promise<IEstate[] | undefined> => {
3939
Util.logCurLine(getCurrentLine())
4040
if (userId == null) throw new Error('Missing parameter: userId')
@@ -53,7 +53,7 @@ export const getAll = async (env: Env, userId: string, crit?: string, fields?: s
5353
return records
5454
}
5555

56-
export const create = async (env: Env, userId: string, param: any)
56+
export const create = async (env: Bindings, userId: string, param: any)
5757
: Promise<IEstate | undefined> => {
5858
Util.logCurLine(getCurrentLine())
5959
if (param == null) throw new Error('Missing parameters')
@@ -104,7 +104,7 @@ export const create = async (env: Env, userId: string, param: any)
104104
return newRec;
105105
}
106106

107-
export const updateById = async (env: Env, id: string, param: any)
107+
export const updateById = async (env: Bindings, id: string, param: any)
108108
: Promise<boolean> => {
109109
Util.logCurLine(getCurrentLine())
110110
if (id == null) throw new Error('Missing id')
@@ -135,7 +135,7 @@ export const updateById = async (env: Env, id: string, param: any)
135135
return true
136136
}
137137

138-
export const deleteById = async (env: Env, id: string)
138+
export const deleteById = async (env: Bindings, id: string)
139139
: Promise<boolean> => {
140140
Util.logCurLine(getCurrentLine())
141141
if (id == null) throw new Error('Missing id')

src/models/folder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import getCurrentLine from 'get-current-line'
22

3-
import { Env } from '@/bindings'
3+
import { Bindings } from '@/bindings'
44
import { nanoid } from 'nanoid'
55

66
import { Util } from '../util'
@@ -15,7 +15,7 @@ export interface IFolder {
1515
}
1616

1717
// D1 doc: https://developers.cloudflare.com/d1/client-api
18-
export const getById = async (env: Env, id: string, fields?: string)
18+
export const getById = async (env: Bindings, id: string, fields?: string)
1919
: Promise<IFolder | undefined> => {
2020
Util.logCurLine(getCurrentLine())
2121
if (id == null) throw new Error('Missing parameter: id')
@@ -26,7 +26,7 @@ export const getById = async (env: Env, id: string, fields?: string)
2626
return record
2727
}
2828

29-
export const getAll = async (env: Env, userId: string, crit?: string, fields?: string, sort?: string, pageNo?: number, pageSize?: number)
29+
export const getAll = async (env: Bindings, userId: string, crit?: string, fields?: string, sort?: string, pageNo?: number, pageSize?: number)
3030
: Promise<IFolder[] | undefined> => {
3131
Util.logCurLine(getCurrentLine())
3232
if (userId == null) throw new Error('Missing parameter: userId')
@@ -45,7 +45,7 @@ export const getAll = async (env: Env, userId: string, crit?: string, fields?: s
4545
return records
4646
}
4747

48-
export const create = async (env: Env, userId: string, param: any)
48+
export const create = async (env: Bindings, userId: string, param: any)
4949
: Promise<IFolder | undefined> => {
5050
Util.logCurLine(getCurrentLine())
5151
if (param == null) throw new Error('Missing parameters')
@@ -80,7 +80,7 @@ export const create = async (env: Env, userId: string, param: any)
8080
return newRec;
8181
}
8282

83-
export const updateById = async (env: Env, id: string, param: any)
83+
export const updateById = async (env: Bindings, id: string, param: any)
8484
: Promise<boolean> => {
8585
Util.logCurLine(getCurrentLine())
8686
if (id == null) throw new Error('Missing id')
@@ -111,7 +111,7 @@ export const updateById = async (env: Env, id: string, param: any)
111111
return true
112112
}
113113

114-
export const deleteById = async (env: Env, id: string)
114+
export const deleteById = async (env: Bindings, id: string)
115115
: Promise<boolean> => {
116116
Util.logCurLine(getCurrentLine())
117117
if (id == null) throw new Error('Missing id')

0 commit comments

Comments
 (0)