-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/web api missing functions #69
base: develop
Are you sure you want to change the base?
Changes from all commits
4c5c91a
9f51e6f
28dab16
0f140f8
30ebd22
6feba78
91c69b4
0923acc
6449a99
37e736b
9a2fcf1
b7b52a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||||||||||
const projectBaladiya = require('../utils/projections/projectObject'); | ||||||||||||||
|
||||||||||||||
const getBaladiyaByCodeAndDairaCodeAndWilayaCode = (data) => | ||||||||||||||
/** | ||||||||||||||
* Takes a wilaya mattricule ,a daira code , bladiya code and return the baladyia. | ||||||||||||||
* | ||||||||||||||
* @example Get a baladiya by code, daira code, and wilaya mattricule(mattricule:1, code:101, bcode:101) | ||||||||||||||
* | ||||||||||||||
* //returns {name : "ADRAR"} | ||||||||||||||
* getWilayaByBaladyiaName(1, 101,101) | ||||||||||||||
* | ||||||||||||||
* @param { integer } mattricule wilaya mattricule | ||||||||||||||
* @param { integer } code daira code | ||||||||||||||
* @param { integer } bcode baladiya code | ||||||||||||||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean?
Suggested change
|
||||||||||||||
* @param { String[] } projection a list of Baladyia object attributes to keep | ||||||||||||||
* @returns { Object | null } Returns a baldiya's object, or null | ||||||||||||||
*/ | ||||||||||||||
(mattricule, code, bcode, projection)=>{ | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix linting issues There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I liked everything and learned a lot from it 😁; there is no cause to dislike you, and thank you very much for your valuable feedbacks. |
||||||||||||||
const wilaya = data.find((w) => w.mattricule === mattricule); | ||||||||||||||
if (wilaya) { | ||||||||||||||
const daira = wilaya.dairats.find((d)=> d.code === code); | ||||||||||||||
if( daira){ | ||||||||||||||
const baladiya = daira.baladyiats.find((b)=> b.code === bcode); | ||||||||||||||
return projectBaladiya(baladiya, projection); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
} | ||||||||||||||
return null; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
module.exports = getBaladiyaByCodeAndDairaCodeAndWilayaCode; |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||||||
const projectBaladiya = require('../utils/projections/projectObject'); | ||||||||||
|
||||||||||
const getBaldiyaByCodeForWilaya = (data) => | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd name it |
||||||||||
/** | ||||||||||
* Takes a wilaya code (mattricule) and a baladiya code and return a baladiya | ||||||||||
* | ||||||||||
* @example Get the baladiya of adrar in adrara(mattricule:1 ,code: 101) | ||||||||||
* | ||||||||||
* | ||||||||||
* getBaldiyaByCodeForWilaya(1, 101) | ||||||||||
* | ||||||||||
* @param { Number } mattricule wilaya code (mattricule) | ||||||||||
* @param { Number } code baladiya code (code) | ||||||||||
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check
Suggested change
|
||||||||||
* @param { String[] } projection a list of Baladyia object attributes to keep | ||||||||||
* @returns { Object[] | null } get baladiya's object | ||||||||||
*/ | ||||||||||
|
||||||||||
(mattricule, code, projections) => { | ||||||||||
const wilaya = data.find((w) => w.mattricule === mattricule); | ||||||||||
if (wilaya) { | ||||||||||
const baladyiats = wilaya.dairats.reduce( | ||||||||||
(acc, daira) => [...acc, ...daira.baladyiats], | ||||||||||
[], | ||||||||||
); | ||||||||||
|
||||||||||
const baladiya = baladyiats.find((w)=> w.code === code); | ||||||||||
if (baladiya){ | ||||||||||
return projectBaladiya(baladiya, projections); | ||||||||||
} | ||||||||||
|
||||||||||
|
||||||||||
} | ||||||||||
|
||||||||||
|
||||||||||
return null; | ||||||||||
}; | ||||||||||
|
||||||||||
module.exports = getBaldiyaByCodeForWilaya; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const projectBaladiya = require('../utils/projections/projectObject'); | ||
|
||
const getBaldiyatsForDairaByCodeForWilayaByCode = (data) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as my previous comments, plus this name can be simplified to |
||
/** | ||
* Takes a wilaya code (mattricule), daira code (code) and returns array of Baladiyates of wilaya. | ||
* | ||
* @example Get Baladiyats list of adrar daira in adrar wilaya (mattricule:1 ,code: 101) | ||
* | ||
* | ||
* getBaldiyatsForDairaByCodeForWilayaByCode(1, 101) | ||
* | ||
* @param { Number } mattricule wilaya code (mattricule) | ||
* @param { Number } code baladiya code (code) | ||
* @param { String[] } projections a list of Baladyia object attributes to keep | ||
* @returns { Object[] | null } list of all baladiyas for wilaya | ||
*/ | ||
|
||
(mattricule, code, projections) => { | ||
const wilaya = data.find((w) => w.mattricule === mattricule); | ||
if (wilaya) { | ||
const daira = wilaya.dairats.find((d) => d.code === code); | ||
|
||
if(daira){ | ||
return projectBaladiya(daira.baladyiats, projections); | ||
} | ||
|
||
} | ||
return null; | ||
}; | ||
|
||
module.exports = getBaldiyatsForDairaByCodeForWilayaByCode; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const projectDaira = require('../utils/projections/projectObject'); | ||
|
||
const getDairaByCodeAndWilayaCode = (data)=> | ||
/** | ||
* Takes a wilaya mattricule and a daira code and return daira. | ||
* | ||
* @example Get a daira by code and wilaya mattricule(mattricule:1, code:101) | ||
* | ||
* //returns {name : "ADRAR"} | ||
* getWilayaByBaladyiaName(1, 101) | ||
* | ||
* @param { integer } mattricule wilaya mattricule | ||
* @param { integer } code daira code | ||
* @param { String[] } projection a list of Baladyia object attributes to keep | ||
* @returns { Object | null } Returns daira's object, or null | ||
*/ | ||
(mattricule, code, projection)=>{ | ||
const wilaya = data.find((w) => w.mattricule === mattricule); | ||
if (wilaya) { | ||
const daira = wilaya.dairats.find((d)=> d.code === code); | ||
return projectDaira(daira, projection); | ||
} | ||
return null; | ||
}; | ||
|
||
module.exports = getDairaByCodeAndWilayaCode; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,10 @@ const getBaladyiatsForWilaya = require('./api/getBaladyiatsForWilaya'); | |
const getBaladyiatsForDaira = require('./api/getBaladyiatsForDaira'); | ||
const getWilayaByBaladyiaName = require('./api/getWilayaByBaladyiaName'); | ||
const getDairaByBaladyiaName = require('./api/getDairaByBaladyiaName'); | ||
|
||
const getDairaByCodeAndWilayaCode = require('./api/getDairaByCodeAndWilayaCode'); | ||
const getBaladiyaByCodeAndDairaCodeAndWilayaCode = require('./api/getBaladiyaByCodeAndDairaCodeAndWilayaCode'); | ||
const getBaldiyatsForDairaByCodeForWilayaByCode = require('./api/getBaldiyatsForDairaByCodeForWilayaByCode'); | ||
const getBaldiyaByCodeForWilaya = require('./api/getBaldiyaByCodeForWilaya'); | ||
const data = require('../data/WilayaList.json'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd leave a blank line before the dataset import just for the sake of clarity and readablity |
||
|
||
const _getData = () => [...data]; | ||
|
@@ -35,6 +38,10 @@ module.exports = { | |
getBaladyiatsForDaira: getBaladyiatsForDaira(_getData()), | ||
getWilayaByBaladyiaName: getWilayaByBaladyiaName(_getData()), | ||
getDairaByBaladyiaName: getDairaByBaladyiaName(_getData()), | ||
getDairaByCodeAndWilayaCode: getDairaByCodeAndWilayaCode(_getData()), | ||
getBaladiyaByCodeAndDairaCodeAndWilayaCode: getBaladiyaByCodeAndDairaCodeAndWilayaCode(_getData()), | ||
getBaldiyatsForDairaByCodeForWilayaByCode: getBaldiyatsForDairaByCodeForWilayaByCode(_getData()), | ||
getBaldiyaByCodeForWilaya: getBaldiyaByCodeForWilaya(_getData()), | ||
utils: { | ||
projectObject, | ||
isValidWilayaCode, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
describe('get matching daira by code and wilaya mattricule', ()=>{ | ||
const mockData = [ | ||
{ | ||
mattricule: 47, | ||
name: 'Ghardaïa', | ||
dairats: [ | ||
{ | ||
code: 4701, | ||
name: 'GHARDAIA', | ||
name_ar: 'غرداية', | ||
name_en: 'GHARDAIA', | ||
baladyiats: [ | ||
{ | ||
code: 4701, | ||
name: 'GHARDAIA', | ||
name_en: 'GHARDAIA', | ||
name_ar: 'غرداية', | ||
}, | ||
{ | ||
code: 4702, | ||
name: 'EL MENIAA', | ||
name_en: 'EL MENIAA', | ||
name_ar: 'المنيعة', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
|
||
|
||
let getBaladiyaByCodeAndDairaCodeAndWilayaCode; | ||
beforeEach(()=>{ | ||
getBaladiyaByCodeAndDairaCodeAndWilayaCode = require('../../src/api/getBaladiyaByCodeAndDairaCodeAndWilayaCode'); | ||
}); | ||
|
||
it('should export a function', () => { | ||
expect(typeof getBaladiyaByCodeAndDairaCodeAndWilayaCode).toBe('function'); | ||
}); | ||
|
||
it('should return a curried function that returns the data', () => { | ||
const fn = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData); | ||
|
||
expect(typeof fn).toBe('function'); | ||
}); | ||
|
||
it('should return null for invalid wilaya mattricule and baladiya and daira code ', () => { | ||
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(99, 1, 99); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return null for invalid baladiya and daira code ', () => { | ||
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(47, 88, 99); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
|
||
it('should return null for invalid baladiya code ', () => { | ||
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(47, 4701, 99); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should return a matching baldiya for valide wilaya mattricule, baladiya and daira code ', () => { | ||
const result = getBaladiyaByCodeAndDairaCodeAndWilayaCode(mockData)(47, 4701, 4702); | ||
expect(result).toEqual(mockData[0].dairats[0].baladyiats[1]); | ||
}); | ||
|
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do you have any use case for such a feature? I believe that Baladyia code is unique overall, so if you'd use it it won't matter to add extra params. And if you want to get a baladyia by wilaya or daira code you'd just use their methods with a projection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please explain how can i get the same result by using projection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do have these methods in place:
Maybe it'd be nice to have another
getBaladyiatsForDairaCode
method. If you want to create it, please do it on a separate PR, based ondevelop