Skip to content

Commit

Permalink
Update settingController.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Nov 14, 2024
1 parent 851cb37 commit caf20b0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions api/src/controllers/orderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const checkout = async (req: Request, res: Response) => {
// throw new Error(`Admin user ${env.ADMIN_EMAIL} not found`)
// }

const settings = await Setting.findOne()
const settings = await Setting.findOne({})
if (!settings) {
throw new Error('Settings not found')
}
Expand Down Expand Up @@ -301,7 +301,7 @@ export const update = async (req: Request, res: Response) => {
// user confirmation email
const _user = order.user as env.User

const settings = await Setting.findOne()
const settings = await Setting.findOne({})
if (!settings) {
throw new Error('Settings not found')
}
Expand Down
14 changes: 7 additions & 7 deletions api/src/controllers/settingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Setting from '../models/Setting'
*/
export const init = async () => {
try {
const count = await Setting.findOne().countDocuments()
const count = await Setting.findOne({}).countDocuments()

if (count === 0) {
await new Setting().save()
Expand All @@ -36,7 +36,7 @@ export const init = async () => {
*/
export const getLanguage = async (req: Request, res: Response) => {
try {
const settings = await Setting.findOne()
const settings = await Setting.findOne({})

return res.json(settings?.language || env.DEFAULT_LANGUAGE)
} catch (err) {
Expand All @@ -55,7 +55,7 @@ export const getLanguage = async (req: Request, res: Response) => {
*/
export const getCurrency = async (req: Request, res: Response) => {
try {
const settings = await Setting.findOne()
const settings = await Setting.findOne({})

return res.json(settings?.currency || env.DEFAULT_CURRENCY)
} catch (err) {
Expand All @@ -74,7 +74,7 @@ export const getCurrency = async (req: Request, res: Response) => {
*/
export const getStripeCurrency = async (req: Request, res: Response) => {
try {
const settings = await Setting.findOne()
const settings = await Setting.findOne({})

return res.json(settings?.stripeCurrency || env.DEFAULT_STRIPE_CURRENCY)
} catch (err) {
Expand All @@ -93,7 +93,7 @@ export const getStripeCurrency = async (req: Request, res: Response) => {
*/
export const getSettings = async (req: Request, res: Response) => {
try {
const settings = await Setting.findOne()
const settings = await Setting.findOne({})

return res.json(settings)
} catch (err) {
Expand All @@ -113,7 +113,7 @@ export const getSettings = async (req: Request, res: Response) => {
export const updateSettings = async (req: Request, res: Response) => {
try {
const { language, currency, stripeCurrency }: wexcommerceTypes.UpdateSettingsPayload = req.body
const settings = await Setting.findOne()
const settings = await Setting.findOne({})

if (settings) {
settings.language = language
Expand Down Expand Up @@ -143,7 +143,7 @@ export const updateSettings = async (req: Request, res: Response) => {
export const updateBankSettings = async (req: Request, res: Response) => {
try {
const { bankName, accountHolder, rib, iban }: wexcommerceTypes.UpdateBankSettingsPayload = req.body
const settings = await Setting.findOne()
const settings = await Setting.findOne({})

if (settings) {
settings.bankName = bankName
Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/stripeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const checkCheckoutSession = async (req: Request, res: Response) => {
user.expireAt = undefined
await user.save()

const settings = await Setting.findOne()
const settings = await Setting.findOne({})
if (!settings) {
throw new Error('Settings not found')
}
Expand Down
18 changes: 9 additions & 9 deletions api/tests/setting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ afterAll(async () => {
describe('Initialize settings', () => {
it('should initialize settings', async () => {
// init
const settings = await Setting.findOne().lean()
const settings = await Setting.findOne({}).lean()
await Setting.deleteMany({})

// test success
let res = await settingController.init()
expect(res).toBeTruthy()
const count = await Setting.findOne().countDocuments()
const count = await Setting.findOne({}).countDocuments()
expect(count).toBe(1)

// restore
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('GET /api/language', () => {
expect(res.body).toBeDefined()

// test default value
const settings = await Setting.findOne().lean()
const settings = await Setting.findOne({}).lean()
await Setting.deleteMany({})
res = await request(app)
.get('/api/language')
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('GET /api/currency', () => {
expect(res.body).toBeDefined()

// test default value
const settings = await Setting.findOne().lean()
const settings = await Setting.findOne({}).lean()
await Setting.deleteMany({})
res = await request(app)
.get('/api/currency')
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('GET /api/stripe-currency', () => {
expect(res.body).toBeDefined()

// test default value
const settings = await Setting.findOne().lean()
const settings = await Setting.findOne({}).lean()
await Setting.deleteMany({})
res = await request(app)
.get('/api/stripe-currency')
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('PUT /api/update-settings', () => {
const token = await testHelper.signinAsAdmin()

// init
const settings = await Setting.findOne().lean()
const settings = await Setting.findOne({}).lean()
expect(settings).toBeTruthy()
const {
language,
Expand All @@ -226,7 +226,7 @@ describe('PUT /api/update-settings', () => {
.set(env.X_ACCESS_TOKEN, token)
.send(payload)
expect(res.statusCode).toBe(200)
const _settings = await Setting.findOne()
const _settings = await Setting.findOne({})
expect(_settings).toBeTruthy()
expect(_settings!.language).toBe('fr')
expect(_settings!.currency).toBe('€')
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('PUT /api/update-bank-settings', () => {
const token = await testHelper.signinAsAdmin()

// init
const settings = await Setting.findOne().lean()
const settings = await Setting.findOne({}).lean()
expect(settings).toBeTruthy()
const {
language,
Expand All @@ -282,7 +282,7 @@ describe('PUT /api/update-bank-settings', () => {
.set(env.X_ACCESS_TOKEN, token)
.send(payload)
expect(res.statusCode).toBe(200)
const _settings = await Setting.findOne()
const _settings = await Setting.findOne({})
expect(_settings).toBeTruthy()
expect(_settings!.bankName).toBe('BANK_NAME')
expect(_settings!.accountHolder).toBe('ACCOUNT_HOLDER')
Expand Down

0 comments on commit caf20b0

Please sign in to comment.