Skip to content

Commit

Permalink
SOROKA-123: Добавлены даты-интервалы, даты-строки, корректная работа …
Browse files Browse the repository at this point in the history
…с DateCatalog, багфиксы (#35)

* + даты-интервалы, даты-строки, багфиксы

* Create .env

* fillRelatedData method refactoring
  • Loading branch information
RomanKuzovlev authored Sep 22, 2022
1 parent f82a707 commit 9d4b85a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
20 changes: 13 additions & 7 deletions src/models/cards/FilledProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import {
Length,
BelongsTo,
AfterCreate,
AfterDestroy,
Scopes
} from 'sequelize-typescript'
import Card, { FilledPropertyCard } from './Card'
import DataType from './DataType'
import Property from './Property'
import GeoProperty from './GeoProperty'
import { fillRelatedData } from '../../utils/filledProperties'
import { fillRelatedData, deleteRelatedData } from '../../utils/filledProperties'

@DefaultScope(() => ({
include: [Property.scope('dataType')],
Expand Down Expand Up @@ -49,23 +50,28 @@ class FilledProperty extends Model {
@BelongsTo(() => Property)
property: Property

// if property type is JulianDate | Geo => fill / update / create an entry in DateCatalog
@BeforeUpdate
static async onFilledPropertyChanged(instance: FilledProperty) {
const dataType = instance.property.dataType.name

const dataType: string | null = instance.property.dataType.name
fillRelatedData(instance, dataType)
}

@AfterCreate
static async onFilledPropertyCreated(instance: FilledProperty) {
// после создания мы ещё не имеем связи
const property = await Property.findByPk(instance.propertyId)
const dataType = await DataType.findByPk(property?.dataTypeId)

const property: Property | null = await Property.findByPk(instance.propertyId)
const dataType: DataType | null = await DataType.findByPk(property?.dataTypeId)

if (dataType?.name) {
fillRelatedData(instance, dataType.name)
}
}

@AfterDestroy
static async onFilledPropertyDestroyed(instance: FilledProperty) {
const filledPropertyId: number = instance.id;
deleteRelatedData(filledPropertyId)
}
}

export default FilledProperty
9 changes: 5 additions & 4 deletions src/routes/v1/cards/FilledProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ function getRouter(
.post(controller.create)
.delete(controller.delete)

router.route('/by-id/:propertyId')
.patch(controller.update)
.get(controller.getByPk)

router.route('/bulk/update')
.patch(controller.bulkUpdate)

router.route('/bulk/delete/:cardId')
.delete(controller.bulkDelete)

router.route('/by-id/:propertyId')
.patch(controller.update)
.get(controller.getByPk)


return router
}

Expand Down
8 changes: 7 additions & 1 deletion src/services/cards/FilledProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class FilledPropertyService implements IFilledPropertyService {

async delete(cardId: number, filledPropertyId: number): Promise<any> {
try {
await FilledPropertyCard.destroy({ where: { cardId, filledPropertyId } })
const propertyCard = await FilledPropertyCard.findOne({ where: { cardId, filledPropertyId } });
propertyCard?.destroy();

const filledProperty = await FilledProperty.findByPk(filledPropertyId);
filledProperty?.destroy();

return { status: 204 }
} catch (error) {
Expand Down Expand Up @@ -73,6 +77,8 @@ class FilledPropertyService implements IFilledPropertyService {
try {
for (const filledPropertyId of filledProperties) {
await FilledPropertyCard.destroy({ where: { cardId, filledPropertyId } })
const filledProperty = await FilledProperty.findByPk(filledPropertyId);
filledProperty?.destroy();
}

return { status: 204 }
Expand Down
66 changes: 40 additions & 26 deletions src/utils/filledProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,59 @@ import FilledProperty from "../models/cards/FilledProperty"
import GeoProperty from "../models/cards/GeoProperty"
import DateCatalog from "../models/dates/DateCatalog"

async function deleteRelatedData(filledPropertyId: number) {
try {
await DateCatalog.destroy({where: {filledPropertyId: filledPropertyId}});
} catch (e) {
console.log("deleteRelatedDataError: ", e)
}
}

async function fillRelatedData(instance: FilledProperty, dataType: string) {
const isJulianDate = dataType === 'JULIAN_DATE'
const isGeoProperty = dataType === 'GEO_POINT'

const data: any = JSON.parse(instance.data)
const data: any = instance.data;
const filledPropertyId = instance.id

if (isJulianDate) {
// FIXME: в будущем будут периоды дат и тд
// сейчас просто заглушка для определения
// даты начала и окончания
const dateStart = data[0].jd
const dateEnd = data[0].jd

if (!dateStart) {
return
try {
await DateCatalog.destroy({where: {filledPropertyId: filledPropertyId}});

for (const el of data) {
if (!el.startJD) continue;
const dateStart = el.startJD
const dateEnd = el.endJD || el.startJD

await DateCatalog.create({ dateStart, dateEnd, filledPropertyId })
}
} catch (e) {
console.log("fillRelatedDataDateError:", e);
}

const filledPropertyId = instance.id

await DateCatalog.create({ dateStart, dateEnd, filledPropertyId })
}

if (isGeoProperty) {
// FIXME в будущем сделать более оптимально

// удалим все предыдущие гео-свойства
await GeoProperty.destroy({ where: { filledPropertyId: instance.id } })

// сформируем массив с данными
const geoData = data.map((prop: any) => {
return { ...prop, filledPropertyId: instance.id }
})

// создадим новые гео-свойства
const results = await GeoProperty.bulkCreate(geoData)
console.log('geo results', results)
try {
// удалим все предыдущие гео-свойства
await GeoProperty.destroy({ where: { filledPropertyId: instance.id } })

// сформируем массив с данными
const geoData = data.map((prop: any) => {
return { ...prop, filledPropertyId: instance.id }
})

// создадим новые гео-свойства
const results = await GeoProperty.bulkCreate(geoData)
console.log('geo results', results)
} catch (e) {
console.log("fillRelatedDataGeoError: ", e)
}

}
}

export {
fillRelatedData
fillRelatedData,
deleteRelatedData
}

0 comments on commit 9d4b85a

Please sign in to comment.