Skip to content

Commit

Permalink
SOROKA-126: Added filtering of cards by orgs; added noAuth routes for…
Browse files Browse the repository at this point in the history
… Cards (#32)

* SOROKA-126: Added filtering of cards by org; added noAuth routes for card

* Update .env

* Update .env

* SOROKA-126-fix (#33)

* SOROKA-126-fix

* fixed pagination issue (#34)
  • Loading branch information
RomanKuzovlev authored Sep 21, 2022
1 parent a59fe49 commit f82a707
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 155 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ version: '3.4'
services:
db:
image: postgis/postgis
expose:
- 5432
ports:
- 5432:5432
volumes:
- ./dbs/postgres-data:/var/lib/postgresql
env_file:
Expand Down
176 changes: 39 additions & 137 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"prestart": "npm run build",
"start": "nodemon dist/index.js",
"start": "nodemon -L dist/index.js",
"build": "npx tsc",
"lint": "npx eslint . --ext .ts",
"migrate": "npx sequelize db:migrate"
Expand Down
13 changes: 12 additions & 1 deletion src/controllers/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ class CardController implements ICardController {
}

getAllByFirstOrganization = async (request: Request, response: Response) => {
const cardsResponse = await this.cardService.getAllByFirstOrganization(
const cardsResponse = await this.cardService.getAllById(
1,
Number(request.query.limit) || null,
Number(request.query.offset) || null
)

return response.send(cardsResponse)
}

getAllByOrgId = async (request: Request, response: Response) => {
const cardsResponse = await this.cardService.getAllById(
Number(request.params.orgId),
Number(request.query.limit) || null,
Number(request.query.offset) || null
)
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ interface ICardController {
update: any
getAll: any
getAllByFirstOrganization: any
getAllByOrgId: any
}

interface ICardService {
getByPk: any
create: any
update: any
getAll: any
getAllByFirstOrganization: any
getAllById: any
}

interface ICardTemplateController {
Expand Down
13 changes: 12 additions & 1 deletion src/routes/v1/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ function getRouter(
) {
const router: express.Router = express.Router()

// temporarily no auth routes for the demo
router.route('/noAuth')
.get(controller.getAll)

router.route('/noAuth/by-org/:orgId')
.get(controller.getAllByOrgId)
// end of temp no auth routs

router.route('/')
.get(passport.authenticate('jwt', { session: false }), controller.getAll)
.post(passport.authenticate('jwt', { session: false }), controller.create)

router.route('/by-first-organization')
router.route('/by-first-organization/')
.get(controller.getAllByFirstOrganization)

router.route('/by-org/:orgId')
.get(controller.getAllByOrgId)

router.route('/by-id/:cardId')
.get(controller.getByPk)
.patch(controller.update)
Expand Down
27 changes: 15 additions & 12 deletions src/services/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ class CardService implements ICardService {
async getAll (user: any, limit?: number, offset?: number): Promise<any> {
const ALLOWED_ROLES = ['ADMIN', 'EDITOR']

if (!user) {
return []
let hasPermission = null;
let filters = {}

if (user) {
const userRole: any = await UserRole.findByPk(user.userRole);
hasPermission = ALLOWED_ROLES.includes(userRole.key);
filters = hasPermission ? {} : { organizationId: user.organization }
}

const userRole: any = await UserRole.findByPk(user.userRole)

const hasPermission = ALLOWED_ROLES.includes(userRole.key)

const filters = hasPermission ? {} : { organizationId: user.organization }

const cards: any = await paginate(Card.scope('detail'), filters, limit, offset)

const cardsList = []
Expand Down Expand Up @@ -46,7 +45,7 @@ class CardService implements ICardService {
cardObj.propertiesList = props

cardObj.isFilled = props.every(
(prop: any) => prop.data && JSON.parse(prop.data).length > 0
(prop: any) => prop.data && prop.data.length > 0
)

cardsList.push(cardObj)
Expand All @@ -59,8 +58,12 @@ class CardService implements ICardService {
}
}

async getAllByFirstOrganization (limit?: number, offset?: number): Promise<any> {
const filters = { organizationId: 1 }
async getAllById (orgId: number, limit?: number, offset?: number): Promise<any> {
if (!Number.isInteger(orgId) || orgId < 0) {
return { detail: 'Invalid organization id', status: 400 }
}

const filters = { organizationId: orgId }

const cards: any = await paginate(Card.scope('detail'), filters, limit, offset)

Expand Down Expand Up @@ -90,7 +93,7 @@ class CardService implements ICardService {
cardObj.propertiesList = props

cardObj.isFilled = props.every(
(prop: any) => prop.data && JSON.parse(prop.data).length > 0
(prop: any) => prop.data && prop.data.length > 0
)

cardsList.push(cardObj)
Expand Down

0 comments on commit f82a707

Please sign in to comment.