Skip to content

Commit

Permalink
Update locationController.ts and location.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Nov 17, 2024
1 parent be34c11 commit f5fe073
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 27 deletions.
22 changes: 11 additions & 11 deletions api/src/controllers/locationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ export const create = async (req: Request, res: Response) => {
const _image = path.join(env.CDN_TEMP_LOCATIONS, image)

if (!await helper.exists(_image)) {
logger.error(i18n.t('LOCATION_IMAGE_NOT_FOUND'), body)
return res.status(400).send(i18n.t('LOCATION_IMAGE_NOT_FOUND'))
throw new Error(`Location image not found: ${_image}`)
}
}

Expand Down Expand Up @@ -124,14 +123,12 @@ export const create = async (req: Request, res: Response) => {
if (image) {
const _image = path.join(env.CDN_TEMP_LOCATIONS, image)

if (await helper.exists(_image)) {
const filename = `${location._id}_${Date.now()}${path.extname(image)}`
const newPath = path.join(env.CDN_LOCATIONS, filename)
const filename = `${location._id}_${Date.now()}${path.extname(image)}`
const newPath = path.join(env.CDN_LOCATIONS, filename)

await fs.rename(_image, newPath)
location.image = filename
await location.save()
}
await fs.rename(_image, newPath)
location.image = filename
await location.save()
}

return res.send(location)
Expand Down Expand Up @@ -284,7 +281,7 @@ export const deleteLocation = async (req: Request, res: Response) => {
await LocationValue.deleteMany({ _id: { $in: location.values } })
await Location.deleteOne({ _id: id })

if (location.parkingSpots) {
if (location.parkingSpots && location.parkingSpots.length > 0) {
const values = location.parkingSpots.map((ps) => ps.values).flat()
await LocationValue.deleteMany({ _id: { $in: values } })
const parkingSpots = location.parkingSpots.map((ps) => ps._id)
Expand Down Expand Up @@ -342,7 +339,7 @@ export const getLocation = async (req: Request, res: Response) => {
const countryName = ((location.country as env.CountryInfo).values as env.LocationValue[]).filter((value) => value.language === req.params.language)[0].value
location.country.name = countryName
}
if (location.parkingSpots) {
if (location.parkingSpots && location.parkingSpots.length > 0) {
for (const parkingSpot of location.parkingSpots) {
parkingSpot.name = (parkingSpot.values as env.LocationValue[]).filter((value) => value.language === req.params.language)[0].value
}
Expand Down Expand Up @@ -706,6 +703,9 @@ export const deleteTempImage = async (req: Request, res: Response) => {
const { image } = req.params

try {
if (!image.includes('.')) {
throw new Error('Filename not valid')
}
const imageFile = path.join(env.CDN_TEMP_LOCATIONS, image)
if (await helper.exists(imageFile)) {
await fs.unlink(imageFile)
Expand Down
96 changes: 80 additions & 16 deletions api/tests/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,6 @@ describe('POST /api/create-location', () => {
latitude: 28.0268755,
longitude: 1.6528399999999976,
image: 'unknown.jpg',
parkingSpots: [
{
latitude: 28.1268755,
longitude: 1.752839999999997,
values: [{ language: 'en', value: 'Parking spot 1' }, { language: 'fr', value: 'Parking 1' }],
},
{
latitude: 28.2268755,
longitude: 1.8528399999999976,
values: [{ language: 'en', value: 'Parking spot 2' }, { language: 'fr', value: 'Parking 2' }],
},
],
}

// image not found
Expand All @@ -147,7 +135,33 @@ describe('POST /api/create-location', () => {
.send(payload)
expect(res.statusCode).toBe(400)

// image found
// no image
payload.image = undefined
res = await request(app)
.post('/api/create-location')
.set(env.X_ACCESS_TOKEN, token)
.send(payload)
expect(res.statusCode).toBe(200)
expect(res.body._id).toBeTruthy()
await Location.deleteOne({ _id: res.body._id })

// image found and parkingspots
payload.parkingSpots = [
{
latitude: 28.1268755,
longitude: 1.752839999999997,
values: [{ language: 'en', value: 'Parking spot 1' }, { language: 'fr', value: 'Parking 1' }],
},
{
latitude: 28.2268755,
longitude: 1.8528399999999976,
values: [{ language: 'en', value: 'Parking spot 2' }, { language: 'fr', value: 'Parking 2' }],
},
{
latitude: 27.2268755,
longitude: 12.852839999999997,
},
]
const tempImage = path.join(env.CDN_TEMP_LOCATIONS, IMAGE0)
if (!await helper.exists(tempImage)) {
await fs.copyFile(IMAGE0_PATH, tempImage)
Expand All @@ -162,7 +176,7 @@ describe('POST /api/create-location', () => {
expect(res.body?.values?.length).toBe(2)
expect(res.body?.latitude).toBe(payload.latitude)
expect(res.body?.longitude).toBe(payload.longitude)
expect(res.body?.parkingSpots?.length).toBe(2)
expect(res.body?.parkingSpots?.length).toBe(3)
LOCATION_ID = res.body?._id

// no payload
Expand Down Expand Up @@ -204,7 +218,7 @@ describe('PUT /api/update-location/:id', () => {
},
})
.lean()
expect(location?.parkingSpots.length).toBe(2)
expect(location?.parkingSpots.length).toBe(3)

const parkingSpot2 = (location!.parkingSpots[1]) as unknown as FlattenMaps<bookcarsTypes.ParkingSpot>
expect(parkingSpot2.values!.length).toBe(2)
Expand Down Expand Up @@ -344,12 +358,22 @@ describe('POST /api/update-location-image/:id', () => {
.attach('image', IMAGE2_PATH)
expect(res.statusCode).toBe(200)
const filename = res.body as string
const imageExists = await helper.exists(path.join(env.CDN_LOCATIONS, filename))
const imageFile = path.join(env.CDN_LOCATIONS, filename)
const imageExists = await helper.exists(imageFile)
expect(imageExists).toBeTruthy()
const location = await Location.findById(LOCATION_ID)
expect(location).not.toBeNull()
expect(location?.image).toBe(filename)

await fs.unlink(imageFile)
location!.image = undefined
await location?.save()
res = await request(app)
.post(`/api/update-location-image/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)
.attach('image', IMAGE2_PATH)
expect(res.statusCode).toBe(200)

location!.image = `${nanoid()}.jpg`
await location?.save()
res = await request(app)
Expand Down Expand Up @@ -407,6 +431,19 @@ describe('POST /api/delete-location-image/:id', () => {
location = await Location.findById(LOCATION_ID)
expect(location?.image).toBeNull()

res = await request(app)
.post(`/api/delete-location-image/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(200)

location = await Location.findById(LOCATION_ID)
location!.image = `${nanoid()}.jpg`
await location!.save()
res = await request(app)
.post(`/api/delete-location-image/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(200)

res = await request(app)
.post(`/api/delete-location-image/${testHelper.GetRandromObjectIdAsString()}`)
.set(env.X_ACCESS_TOKEN, token)
Expand Down Expand Up @@ -441,6 +478,11 @@ describe('POST /api/delete-temp-location-image/:image', () => {
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(200)

res = await request(app)
.post('/api/delete-temp-location-image/unknown')
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(400)

await testHelper.signout(token)
})
})
Expand All @@ -454,6 +496,13 @@ describe('GET /api/location/:id/:language', () => {
expect(res.statusCode).toBe(200)
expect(res.body?.name).toBe(LOCATION_NAMES.filter((v) => v.language === language)[0].name)

const locationId = await testHelper.createLocation('loc1-en', 'loc1-fr')
res = await request(app)
.get(`/api/location/${locationId}/${language}`)
expect(res.statusCode).toBe(200)
expect(res.body?.name).toBe('loc1-en')
await Location.deleteOne({ _id: locationId })

res = await request(app)
.get(`/api/location/${testHelper.GetRandromObjectIdAsString()}/${language}`)
expect(res.statusCode).toBe(204)
Expand Down Expand Up @@ -573,6 +622,21 @@ describe('DELETE /api/delete-location/:id', () => {
location = await Location.findById(LOCATION_ID)
expect(location).toBeNull()

let locationId = await testHelper.createLocation('loc1-en', 'loc1-fr')
res = await request(app)
.delete(`/api/delete-location/${locationId}`)
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(200)

locationId = await testHelper.createLocation('loc2-en', 'loc2-fr')
location = await Location.findById(locationId)
location!.image = `${nanoid()}.jpg`
await location!.save()
res = await request(app)
.delete(`/api/delete-location/${locationId}`)
.set(env.X_ACCESS_TOKEN, token)
expect(res.statusCode).toBe(200)

res = await request(app)
.delete(`/api/delete-location/${LOCATION_ID}`)
.set(env.X_ACCESS_TOKEN, token)
Expand Down

0 comments on commit f5fe073

Please sign in to comment.