Skip to content

Commit

Permalink
[feat] Add getting card by first organization endpoint & speed up get…
Browse files Browse the repository at this point in the history
…All endpoint (#30)
  • Loading branch information
kantegory authored Jul 11, 2022
1 parent f17a48c commit a59fe49
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/controllers/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class CardController implements ICardController {
return response.send(cardsResponse)
}

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

return response.send(cardsResponse)
}

create = async (request: Request, response: Response) => {
const createdCard = await this.cardService.create(request.user, request.body)

Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ interface ICardController {
create: any
update: any
getAll: any
getAllByFirstOrganization: any
}

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

interface ICardTemplateController {
Expand Down
15 changes: 14 additions & 1 deletion src/models/cards/Card.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { Table, Column, Model, AllowNull, ForeignKey, BelongsToMany } from 'sequelize-typescript'
import {
Table,
Column,
Model,
AllowNull,
ForeignKey,
BelongsToMany,
Scopes
} from 'sequelize-typescript'
import Organization from '../organizations/Organization'
import User from '../users/User'
import FilledProperty from './FilledProperty'

@Scopes(() => ({
detail: {
include: [FilledProperty.scope('short')]
}
}))
@Table
class Card extends Model {
@AllowNull(false)
Expand Down
8 changes: 7 additions & 1 deletion src/models/cards/FilledProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
DefaultScope,
Length,
BelongsTo,
AfterCreate
AfterCreate,
Scopes
} from 'sequelize-typescript'
import Card, { FilledPropertyCard } from './Card'
import DataType from './DataType'
Expand All @@ -23,6 +24,11 @@ import { fillRelatedData } from '../../utils/filledProperties'
include: [Property.scope('dataType')],
attributes: { exclude: ['FilledPropertyCard', 'createdAt', 'updatedAt'] }
}))
@Scopes(() => ({
short: {
attributes: { exclude: ['createdAt', 'updatedAt'] }
}
}))
@Table
class FilledProperty extends Model {
@AllowNull(false)
Expand Down
3 changes: 3 additions & 0 deletions src/routes/v1/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function getRouter(
router.route('/')
.get(passport.authenticate('jwt', { session: false }), controller.getAll)
.post(passport.authenticate('jwt', { session: false }), controller.create)

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

router.route('/by-id/:cardId')
.get(controller.getByPk)
Expand Down
64 changes: 59 additions & 5 deletions src/services/cards/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,70 @@ class CardService implements ICardService {

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

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

const cardsList = []

for (const card of cards.results) {
const cardObj = card.toJSON()
const cardObj = {
id: card.id,
name: card.name,
userId: card.userId,
organizationId: card.organizationId,
preventDelete: card.preventDelete,
createdAt: card.createdAt,
updatedAt: card.updatedAt,
propertiesList: [],
isFilled: null
}

let props = card.properties

props = props.map((prop: any) => {
const { id, propertyId, data } = prop

return { id, propertyId, data }
})

cardObj.propertiesList = props

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

cardsList.push(cardObj)
}

return {
total: cards.total,
results: cardsList,
hasNextPage: cards.hasNextPage
}
}

async getAllByFirstOrganization (limit?: number, offset?: number): Promise<any> {
const filters = { organizationId: 1 }

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

const cardsList = []

for (const card of cards.results) {
const cardObj = {
id: card.id,
name: card.name,
userId: card.userId,
organizationId: card.organizationId,
preventDelete: card.preventDelete,
createdAt: card.createdAt,
updatedAt: card.updatedAt,
propertiesList: [],
isFilled: null
}

let props = await card.getProperties()
let props = card.properties

props = props.map((prop: FilledProperty) => {
props = props.map((prop: any) => {
const { id, propertyId, data } = prop

return { id, propertyId, data }
Expand All @@ -36,7 +90,7 @@ class CardService implements ICardService {
cardObj.propertiesList = props

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

cardsList.push(cardObj)
Expand Down

0 comments on commit a59fe49

Please sign in to comment.