From 147958dc85900d9f03f6870603ed1839523e1258 Mon Sep 17 00:00:00 2001 From: lesion Date: Tue, 6 Sep 2022 21:42:32 +0200 Subject: [PATCH] auto-formatting --- server/api/controller/event.js | 84 +++++++++++++++++----------------- tests/app.test.js | 58 +++++++++++------------ 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/server/api/controller/event.js b/server/api/controller/event.js index bb2849cf..8824ce14 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -23,18 +23,18 @@ const log = require('../../log') const eventController = { - async searchMeta (req, res) { + async searchMeta(req, res) { const search = req.query.search const places = await Place.findAll({ order: [[Sequelize.col('w'), 'DESC']], where: { [Op.or]: [ - Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), 'LIKE', '%' + search + '%' ), + Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), 'LIKE', '%' + search + '%'), Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('address')), 'LIKE', '%' + search + '%') ] }, - attributes: [['name', 'label'], 'address', 'id', [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('events.placeId')),'INTEGER'), 'w']], + attributes: [['name', 'label'], 'address', 'id', [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('events.placeId')), 'INTEGER'), 'w']], include: [{ model: Event, where: { is_visible: true }, required: true, attributes: [] }], group: ['place.id'], raw: true @@ -44,8 +44,8 @@ const eventController = { order: [[Sequelize.col('w'), 'DESC']], where: { tag: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('tag')), 'LIKE', '%' + search + '%'), - }, - attributes: [['tag','label'], [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'INTEGER'), 'w']], + }, + attributes: [['tag', 'label'], [Sequelize.cast(Sequelize.fn('COUNT', Sequelize.col('tag.tag')), 'INTEGER'), 'w']], include: [{ model: Event, where: { is_visible: true }, attributes: [], through: { attributes: [] }, required: true }], group: ['tag.tag'], raw: true @@ -57,13 +57,13 @@ const eventController = { }).concat(tags.map(t => { t.type = 'tag' return t - })).sort( (a, b) => b.w - a.w).slice(0, 10) + })).sort((a, b) => b.w - a.w).slice(0, 10) return res.json(ret) }, - async search (req, res) { + async search(req, res) { const search = req.query.search.trim().toLocaleLowerCase() const show_recurrent = req.query.show_recurrent || false const end = req.query.end @@ -89,11 +89,11 @@ const eventController = { if (search) { replacements.push(search) where[Op.or] = - [ - { title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', '%' + search + '%') }, - Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), 'LIKE', '%' + search + '%'), - Sequelize.fn('EXISTS', Sequelize.literal(`SELECT 1 FROM event_tags WHERE ${Col('event_tags.eventId')}=${Col('event.id')} AND LOWER(${Col('tagTag')}) = ?`)) - ] + [ + { title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', '%' + search + '%') }, + Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), 'LIKE', '%' + search + '%'), + Sequelize.fn('EXISTS', Sequelize.literal(`SELECT 1 FROM event_tags WHERE ${Col('event_tags.eventId')}=${Col('event.id')} AND LOWER(${Col('tagTag')}) = ?`)) + ] } @@ -129,9 +129,9 @@ const eventController = { }, - async getNotifications (event, action) { + async getNotifications(event, action) { log.debug(`getNotifications ${event.title} ${action}`) - function match (event, filters) { + function match(event, filters) { // matches if no filter specified if (!filters) { return true } @@ -170,7 +170,7 @@ const eventController = { }) }, - async get (req, res) { + async get(req, res) { const format = req.params.format || 'json' const is_admin = res.locals.user && res.locals.user.is_admin const slug = req.params.event_slug @@ -222,7 +222,7 @@ const eventController = { recurrent: null, [Op.or]: [ { start_datetime: { [Op.gt]: event.start_datetime } }, - { + { start_datetime: event.start_datetime, id: { [Op.gt]: event.id } } @@ -239,7 +239,7 @@ const eventController = { recurrent: null, [Op.or]: [ { start_datetime: { [Op.lt]: event.start_datetime } }, - { + { start_datetime: event.start_datetime, id: { [Op.lt]: event.id } } @@ -270,7 +270,7 @@ const eventController = { /** confirm an anonymous event * and send related notifications */ - async confirm (req, res) { + async confirm(req, res) { const id = Number(req.params.event_id) const event = await Event.findByPk(id, { include: [Place, Tag] }) if (!event) { @@ -299,7 +299,7 @@ const eventController = { } }, - async unconfirm (req, res) { + async unconfirm(req, res) { const id = Number(req.params.event_id) const event = await Event.findByPk(id) if (!event) { return req.sendStatus(404) } @@ -318,7 +318,7 @@ const eventController = { }, /** get all unconfirmed events */ - async getUnconfirmed (_req, res) { + async getUnconfirmed(_req, res) { try { const events = await Event.findAll({ where: { @@ -336,7 +336,7 @@ const eventController = { } }, - async addNotification (req, res) { + async addNotification(req, res) { try { const notification = { filters: { is_visible: true }, @@ -351,7 +351,7 @@ const eventController = { } }, - async delNotification (req, res) { + async delNotification(req, res) { const remove_code = req.params.code try { const notification = await Notification.findOne({ where: { remove_code } }) @@ -362,14 +362,14 @@ const eventController = { res.sendStatus(200) }, - async isAnonEventAllowed (_req, res, next) { + async isAnonEventAllowed(_req, res, next) { if (!res.locals.settings.allow_anon_event && !res.locals.user) { return res.sendStatus(403) } next() }, - async add (req, res) { + async add(req, res) { // req.err comes from multer streaming error if (req.err) { log.warn(req.err) @@ -380,7 +380,7 @@ const eventController = { const body = req.body const recurrent = body.recurrent ? JSON.parse(body.recurrent) : null - const required_fields = [ 'title', 'start_datetime'] + const required_fields = ['title', 'start_datetime'] let missing_field = required_fields.find(required_field => !body[required_field]) if (missing_field) { log.warn(`${missing_field} required`) @@ -398,7 +398,7 @@ const eventController = { if (!body.place_name) { return res.status(400).send(`Place not found`) } - place = await Place.findOne({ where: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), Op.eq, body.place_name.trim().toLocaleLowerCase() )}) + place = await Place.findOne({ where: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), Op.eq, body.place_name.trim().toLocaleLowerCase()) }) if (!place) { if (!body.place_address || !body.place_name) { return res.status(400).send(`place_id or place_name and place_address required`) @@ -479,7 +479,7 @@ const eventController = { } }, - async update (req, res) { + async update(req, res) { if (res.err) { log.warn(req.err) return res.status(400).json(req.err.toString()) @@ -537,7 +537,7 @@ const eventController = { } else if (body.image_focalpoint && event.media.length) { let focalpoint = body.image_focalpoint ? body.image_focalpoint.split(',') : ['0', '0'] focalpoint = [parseFloat(parseFloat(focalpoint[0]).toFixed(2)), parseFloat(parseFloat(focalpoint[1]).toFixed(2))] - eventDetails.media = [ { ...event.media[0], focalpoint } ] // [0].focalpoint = focalpoint + eventDetails.media = [{ ...event.media[0], focalpoint }] // [0].focalpoint = focalpoint } await event.update(eventDetails) @@ -552,7 +552,7 @@ const eventController = { if (!body.place_name) { return res.status(400).send(`Place not found`) } - place = await Place.findOne({ where: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), Op.eq, body.place_name.trim().toLocaleLowerCase() )}) + place = await Place.findOne({ where: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), Op.eq, body.place_name.trim().toLocaleLowerCase()) }) if (!place) { if (!body.place_address || !body.place_name) { return res.status(400).send(`place_id or place_name and place_address required`) @@ -565,7 +565,7 @@ const eventController = { } await event.setPlace(place) - + // create/assign tags let tags = [] if (body.tags) { @@ -593,7 +593,7 @@ const eventController = { } }, - async remove (req, res) { + async remove(req, res) { const event = await Event.findByPk(req.params.id) // check if event is mine (or user is admin) if (event && (res.locals.user.is_admin || res.locals.user.id === event.userId)) { @@ -626,7 +626,7 @@ const eventController = { * Method to search for events with pagination and filtering * @returns */ - async _select ({ + async _select({ start = dayjs().unix(), end, tags, @@ -665,8 +665,8 @@ const eventController = { const replacements = [] if (tags && places) { - where[Op.and] = [ - { placeId: places ? places.split(',') : []}, + where[Op.and] = [ + { placeId: places ? places.split(',') : [] }, Sequelize.fn('EXISTS', Sequelize.literal(`SELECT 1 FROM event_tags WHERE ${Col('event_tags.eventId')}=${Col('event.id')} AND LOWER(${Col('tagTag')}) in (?)`)) ] replacements.push(tags) @@ -679,10 +679,10 @@ const eventController = { let pagination = {} if (limit) { - pagination = { + pagination = { limit, offset: limit * page, - } + } } const events = await Event.findAll({ @@ -690,7 +690,7 @@ const eventController = { attributes: { exclude: ['likes', 'boost', 'userId', 'is_visible', 'createdAt', 'description', 'resources', 'recurrent', 'placeId', 'image_path'] }, - order: [['start_datetime', older ? 'DESC' : 'ASC' ]], + order: [['start_datetime', older ? 'DESC' : 'ASC']], include: [ { model: Tag, @@ -717,7 +717,7 @@ const eventController = { /** * Select events based on params */ - async select (req, res) { + async select(req, res) { const settings = res.locals.settings const start = req.query.start || dayjs().unix() const end = req.query.end @@ -738,7 +738,7 @@ const eventController = { /** * Ensure we have the next instance of a recurrent event */ - async _createRecurrentOccurrence (e, startAt) { + async _createRecurrentOccurrence(e, startAt) { log.debug(`Create recurrent event [${e.id}] ${e.title}"`) const event = { parentId: e.id, @@ -798,12 +798,12 @@ const eventController = { /** * Create instances of recurrent events */ - async _createRecurrent (start_datetime = dayjs().unix()) { + async _createRecurrent(start_datetime = dayjs().unix()) { // select recurrent events and its childs const events = await Event.findAll({ where: { is_visible: true, recurrent: { [Op.ne]: null } }, include: [{ model: Tag, required: false }, - { model: Event, as: 'child', required: false, where: { start_datetime: { [Op.gte]: start_datetime } }}], + { model: Event, as: 'child', required: false, where: { start_datetime: { [Op.gte]: start_datetime } } }], order: [['child', 'start_datetime', 'DESC']] }) @@ -811,7 +811,7 @@ const eventController = { const creations = events.map(e => { if (e.child.length) { if (e.child.find(c => c.is_visible)) return - return eventController._createRecurrentOccurrence(e, dayjs.unix(e.child[0].start_datetime+1)) + return eventController._createRecurrentOccurrence(e, dayjs.unix(e.child[0].start_datetime + 1)) } return eventController._createRecurrentOccurrence(e, dayjs()) }) diff --git a/tests/app.test.js b/tests/app.test.js index 11edc388..3271aad3 100644 --- a/tests/app.test.js +++ b/tests/app.test.js @@ -8,7 +8,7 @@ let token let app let places = [] -beforeAll( async () => { +beforeAll(async () => { switch (process.env.DB) { case 'mariadb': process.env.config_path = path.resolve(__dirname, './seeds/config.mariadb.json') @@ -32,7 +32,7 @@ beforeAll( async () => { await sequelize.query('DELETE FROM filters') }) -afterAll( async () => { +afterAll(async () => { await require('../server/initialize.server.js').shutdown(false) }) @@ -77,7 +77,7 @@ describe('Authentication / Authorization', () => { expect(response.body.token_type).toBe('Bearer') token = response.body }) - + test('should get user when authenticated', async () => { const response = await request(app).get('/api/user') .auth(token.access_token, { type: 'bearer' }) @@ -105,16 +105,16 @@ describe('Settings', () => { test('should retrieve stored array settings', async () => { await request(app).post('/api/settings') .auth(token.access_token, { type: 'bearer' }) - .send({ key: 'test', value: [1,2,'test'] }) + .send({ key: 'test', value: [1, 2, 'test'] }) .expect(200) const response = await request(app) .get('/api/settings') .auth(token.access_token, { type: 'bearer' }) .expect(200) - + expect(response.body.test.length).toBe(3) - expect(response.body.test).toStrictEqual([1,2,'test']) + expect(response.body.test).toStrictEqual([1, 2, 'test']) }) @@ -128,7 +128,7 @@ describe('Settings', () => { .get('/api/settings') .auth(token.access_token, { type: 'bearer' }) .expect(200) - + expect(response.body.test.name).toBe('test object') }) @@ -143,9 +143,9 @@ describe('Settings', () => { .get('/api/settings') .auth(token.access_token, { type: 'bearer' }) .expect(200) - + expect(response.body.test).toBe('test string') - }) + }) }) @@ -155,7 +155,7 @@ describe('Events', () => { const required_fields = { 'title': {}, 'start_datetime': { title: 'test title' }, - 'place_id or place_name and place_address': { title: 'test title', start_datetime: dayjs().unix()+1000, place_name: 'test place name'}, + 'place_id or place_name and place_address': { title: 'test title', start_datetime: dayjs().unix() + 1000, place_name: 'test place name' }, } const promises = Object.keys(required_fields).map(async field => { @@ -171,15 +171,15 @@ describe('Events', () => { test('should create anon event only when allowed', async () => { await request(app).post('/api/settings') - .send({ key: 'allow_anon_event', value: false }) - .auth(token.access_token, { type: 'bearer' }) + .send({ key: 'allow_anon_event', value: false }) + .auth(token.access_token, { type: 'bearer' }) .expect(200) await request(app).post('/api/event') .expect(403) let response = await request(app).post('/api/event') - .send({ title: 'test title 2', place_name: 'place name', place_address: 'address', tags: ['test'], start_datetime: dayjs().unix()+1000 }) + .send({ title: 'test title 2', place_name: 'place name', place_address: 'address', tags: ['test'], start_datetime: dayjs().unix() + 1000 }) .auth(token.access_token, { type: 'bearer' }) .expect(200) @@ -189,10 +189,10 @@ describe('Events', () => { await request(app).post('/api/settings') .send({ key: 'allow_anon_event', value: true }) .auth(token.access_token, { type: 'bearer' }) - .expect(200) + .expect(200) response = await request(app).post('/api/event') - .send({ title: 'test title 3', place_name: 'place name 2', place_address: 'address 2', tags: ['test'], start_datetime: dayjs().unix()+1000 }) + .send({ title: 'test title 3', place_name: 'place name 2', place_address: 'address 2', tags: ['test'], start_datetime: dayjs().unix() + 1000 }) .expect(200) expect(response.body.place.id).toBeDefined() @@ -204,7 +204,7 @@ describe('Events', () => { const event = { title: 'test title 4', place_id: places[0], - start_datetime: dayjs().unix()+1000, + start_datetime: dayjs().unix() + 1000, tags: [' test tag '] } @@ -221,7 +221,7 @@ let event = {} describe('Tags', () => { test('should create event with tags', async () => { event = await request(app).post('/api/event') - .send({ title: 'test tags', place_id: places[1], start_datetime: dayjs().unix()+1000 , tags: ['tag1', 'Tag2', 'tAg3'] }) + .send({ title: 'test tags', place_id: places[1], start_datetime: dayjs().unix() + 1000, tags: ['tag1', 'Tag2', 'tAg3'] }) .auth(token.access_token, { type: 'bearer' }) .expect(200) @@ -231,7 +231,7 @@ describe('Tags', () => { test('should create event trimming tags / ignore sensitiviness', async () => { const ret = await request(app).post('/api/event') - .send({ title: 'test trimming tags', place_id: places[1], start_datetime: dayjs().unix()+1000, tags: ['Tag1', 'taG2 '] }) + .send({ title: 'test trimming tags', place_id: places[1], start_datetime: dayjs().unix() + 1000, tags: ['Tag1', 'taG2 '] }) .auth(token.access_token, { type: 'bearer' }) .expect(200) @@ -243,9 +243,9 @@ describe('Tags', () => { test('should modify event tags', async () => { const ret = await request(app).put('/api/event') - .send({ id: event.body.id, tags: ['tag1', 'tag3', 'tag4'], place_id: places[1] }) - .auth(token.access_token, { type: 'bearer' }) - .expect(200) + .send({ id: event.body.id, tags: ['tag1', 'tag3', 'tag4'], place_id: places[1] }) + .auth(token.access_token, { type: 'bearer' }) + .expect(200) expect(ret.body.tags).toStrictEqual(['tag1', 'tAg3', 'tag4']) }) @@ -253,7 +253,7 @@ describe('Tags', () => { test('should return events searching for tags', async () => { const response = await request(app).get('/api/events?tags=tAg3') .expect(200) - + expect(response.body.length).toBe(1) // expect(response.body[0].title).toBe('test tags') expect(response.body[0].tags.length).toBe(3) @@ -264,7 +264,7 @@ describe('Place', () => { test('should get events by place', async () => { const response = await request(app).get('/api/place/place name 2') .expect(200) - + expect(response.body.place.name).toBe('place name 2') expect(response.body.events.length).toBe(2) expect(response.body.events[0].place.name).toBe('place name 2') @@ -277,7 +277,7 @@ describe('Place', () => { const response = await request(app).get('/api/place/all') .auth(token.access_token, { type: 'bearer' }) .expect(200) - + expect(response.body.length).toBe(2) }) @@ -285,7 +285,7 @@ describe('Place', () => { test('should search for a place', async () => { const response = await request(app).get('/api/place?search=place') .expect(200) - + expect(response.body.length).toBe(2) }) @@ -293,7 +293,7 @@ describe('Place', () => { let collections = [] let filters = [] -describe ('Collection', () => { +describe('Collection', () => { test('should not create a new collection if not allowed', () => { return request(app).post('/api/collections') .send({ name: 'test collection' }) @@ -313,8 +313,8 @@ describe ('Collection', () => { const response = await request(app).get('/api/collections/test collection') .expect(200) - expect(response.body.length).toBe(0) - }) + expect(response.body.length).toBe(0) + }) test('should add a new filter', async () => { await request(app) @@ -384,7 +384,7 @@ describe ('Collection', () => { // .expect(200) // expect(response.body.length).toBe(1) - response = await request(app) + response = await request(app) .get(`/api/collections/test collection`) .expect(200)