Skip to content
Snippets Groups Projects
Verified Commit 4fa5e923 authored by Marc Hanisch's avatar Marc Hanisch
Browse files

defines interfaces for Action serializers, renames type properties

parent bb9843ba
No related branches found
No related tags found
2 merge requests!104Load and save Generic Device Actions,!93Actions UI for devices
......@@ -41,10 +41,20 @@ import {
import { DeviceAttachmentSerializer } from '@/serializers/jsonapi/DeviceAttachmentSerializer'
export abstract class GenericActionAttachmentSerializer {
export interface IGenericActionAttachmentSerializer {
targetType: string
convertModelToJsonApiData (attachment: Attachment, actionId: string): IJsonApiEntityWithOptionalId
convertJsonApiRelationshipsModelList (relationships: IJsonApiRelationships, included: IJsonApiEntityWithOptionalAttributes[]): Attachment[]
getActionTypeName (): string
getActionAttachmentTypeName (): string
getActionAttachmentTypeNamePlural (): string
getAttachmentTypeName (): string
}
export abstract class AbstractGenericActionAttachmentSerializer implements IGenericActionAttachmentSerializer {
private attachmentSerializer: DeviceAttachmentSerializer = new DeviceAttachmentSerializer()
abstract get type (): string
abstract get targetType (): string
convertModelToJsonApiData (attachment: Attachment, actionId: string): IJsonApiEntityWithOptionalId {
/**
......@@ -56,11 +66,11 @@ export abstract class GenericActionAttachmentSerializer {
* property whereas we need 'attachment' as the property and
* 'device_attachment' as the type.
*/
const type = this.getActionAttachmentTypeName()
const entityType = this.getActionAttachmentTypeName()
const actionType = this.getActionTypeName()
const attachmentType = this.getAttachmentTypeName()
const data: IJsonApiEntityWithOptionalId = {
type,
type: entityType,
attributes: {},
relationships: {
action: {
......@@ -82,7 +92,7 @@ export abstract class GenericActionAttachmentSerializer {
convertJsonApiRelationshipsModelList (relationships: IJsonApiRelationships, included: IJsonApiEntityWithOptionalAttributes[]): Attachment[] {
const actionAttachmentIds = []
const type = this.getActionAttachmentTypeName()
const entityType = this.getActionAttachmentTypeName()
const typePlural = this.getActionAttachmentTypeNamePlural()
if (relationships[typePlural]) {
const attachmentObject = relationships[typePlural] as IJsonApiEntityWithoutDetailsDataDictList
......@@ -97,7 +107,7 @@ export abstract class GenericActionAttachmentSerializer {
const attachmentIds = []
if (included && included.length > 0) {
for (const includedEntry of included) {
if (includedEntry.type === type) {
if (includedEntry.type === entityType) {
const actionAttachmentId = includedEntry.id
if (actionAttachmentIds.includes(actionAttachmentId)) {
if ((includedEntry.relationships?.attachment?.data as IJsonApiEntityWithoutDetails | undefined)?.id) {
......@@ -126,11 +136,11 @@ export abstract class GenericActionAttachmentSerializer {
}
getActionTypeName (): string {
return 'generic_' + this.type + '_action'
return 'generic_' + this.targetType + '_action'
}
getActionAttachmentTypeName (): string {
return 'generic_' + this.type + '_action_attachment'
return 'generic_' + this.targetType + '_action_attachment'
}
getActionAttachmentTypeNamePlural (): string {
......@@ -138,18 +148,18 @@ export abstract class GenericActionAttachmentSerializer {
}
getAttachmentTypeName (): string {
return this.type + '_attachment'
return this.targetType + '_attachment'
}
}
export class GenericDeviceActionAttachmentSerializer extends GenericActionAttachmentSerializer {
get type (): string {
export class GenericDeviceActionAttachmentSerializer extends AbstractGenericActionAttachmentSerializer {
get targetType (): string {
return 'device'
}
}
export class GenericPlatformActionAttachmentSerializer extends GenericActionAttachmentSerializer {
get type (): string {
export class GenericPlatformActionAttachmentSerializer extends AbstractGenericActionAttachmentSerializer {
get targetType (): string {
return 'platform'
}
}
......@@ -48,7 +48,7 @@ import {
} from '@/serializers/jsonapi/ContactSerializer'
import {
GenericActionAttachmentSerializer,
IGenericActionAttachmentSerializer,
GenericDeviceActionAttachmentSerializer,
GenericPlatformActionAttachmentSerializer
} from '@/serializers/jsonapi/GenericActionAttachmentSerializer'
......@@ -67,11 +67,27 @@ export interface IGenericActionAttachmentRelation {
attachmentId: string
}
export abstract class GenericActionSerializer {
export interface IGenericActionSerializer {
targetType: string
attachmentSerializer: IGenericActionAttachmentSerializer
convertJsonApiObjectToModel (jsonApiObject: IJsonApiEntityEnvelope): GenericAction
convertJsonApiDataToModel (jsonApiData: IJsonApiEntityWithOptionalAttributes, included: IJsonApiEntityWithOptionalAttributes[]): GenericAction
convertJsonApiObjectListToModelList (jsonApiObjectList: IJsonApiEntityListEnvelope): GenericAction[]
convertModelToJsonApiData (action: GenericAction, deviceOrPlatformId: string): IJsonApiEntityWithOptionalId
convertModelToJsonApiRelationshipObject (action: IGenericAction): IJsonApiRelationships
convertModelToTupleWithIdAndType (action: IGenericAction): IJsonApiEntityWithoutDetails
convertJsonApiRelationshipsModelList (relationships: IJsonApiRelationships, included: IJsonApiEntityWithOptionalAttributes[]): IGenericActionsAndMissing
convertJsonApiIncludedGenericActionAttachmentsToIdList (included: IJsonApiEntityWithOptionalAttributes[]): IGenericActionAttachmentRelation[]
getActionTypeName (): string
getActionTypeNamePlural (): string
getActionAttachmentTypeName (): string
}
export abstract class AbstractGenericActionSerializer implements IGenericActionSerializer {
private contactSerializer: ContactSerializer = new ContactSerializer()
abstract get type (): string
abstract get attachmentSerializer (): GenericActionAttachmentSerializer
abstract get targetType (): string
abstract get attachmentSerializer (): IGenericActionAttachmentSerializer
convertJsonApiObjectToModel (jsonApiObject: IJsonApiEntityEnvelope): GenericAction {
const data = jsonApiObject.data
......@@ -125,9 +141,9 @@ export abstract class GenericActionSerializer {
end_date: action.endDate != null ? action.endDate.setZone('UTC').toISO() : null
},
relationships: {
[this.type]: {
[this.targetType]: {
data: {
type: this.type,
type: this.targetType,
id: deviceOrPlatformId
}
}
......@@ -232,7 +248,7 @@ export abstract class GenericActionSerializer {
}
getActionTypeName (): string {
return 'generic_' + this.type + '_action'
return 'generic_' + this.targetType + '_action'
}
getActionTypeNamePlural (): string {
......@@ -240,18 +256,18 @@ export abstract class GenericActionSerializer {
}
getActionAttachmentTypeName (): string {
return 'generic_' + this.type + '_action_attachment'
return 'generic_' + this.targetType + '_action_attachment'
}
}
export class GenericDeviceActionSerializer extends GenericActionSerializer {
private _attachmentSerializer: GenericActionAttachmentSerializer
export class GenericDeviceActionSerializer extends AbstractGenericActionSerializer {
private _attachmentSerializer: IGenericActionAttachmentSerializer
get type (): string {
get targetType (): string {
return 'device'
}
get attachmentSerializer (): GenericActionAttachmentSerializer {
get attachmentSerializer (): IGenericActionAttachmentSerializer {
return this._attachmentSerializer
}
......@@ -261,14 +277,14 @@ export class GenericDeviceActionSerializer extends GenericActionSerializer {
}
}
export class GenericPlatformActionSerializer extends GenericActionSerializer {
private _attachmentSerializer: GenericActionAttachmentSerializer
export class GenericPlatformActionSerializer extends AbstractGenericActionSerializer {
private _attachmentSerializer: IGenericActionAttachmentSerializer
get type (): string {
get targetType (): string {
return 'platform'
}
get attachmentSerializer (): GenericActionAttachmentSerializer {
get attachmentSerializer (): IGenericActionAttachmentSerializer {
return this._attachmentSerializer
}
......
......@@ -34,11 +34,11 @@ import { AxiosInstance } from 'axios'
import { Attachment } from '@/models/Attachment'
import { GenericAction } from '@/models/GenericAction'
import { GenericDeviceActionAttachmentApi } from '@/services/sms/GenericDeviceActionAttachmentApi'
import { GenericActionSerializer, GenericDeviceActionSerializer } from '@/serializers/jsonapi/GenericActionSerializer'
import { IGenericActionSerializer, GenericDeviceActionSerializer } from '@/serializers/jsonapi/GenericActionSerializer'
export class GenericDeviceActionApi {
private axiosApi: AxiosInstance
private serializer: GenericActionSerializer
private serializer: IGenericActionSerializer
private attachmentApi: GenericDeviceActionAttachmentApi
constructor (axiosInstance: AxiosInstance, attachmentApi: GenericDeviceActionAttachmentApi) {
......
......@@ -32,11 +32,11 @@
import { AxiosInstance } from 'axios'
import { Attachment } from '@/models/Attachment'
import { GenericActionAttachmentSerializer, GenericDeviceActionAttachmentSerializer } from '@/serializers/jsonapi/GenericActionAttachmentSerializer'
import { IGenericActionAttachmentSerializer, GenericDeviceActionAttachmentSerializer } from '@/serializers/jsonapi/GenericActionAttachmentSerializer'
export class GenericDeviceActionAttachmentApi {
private axiosApi: AxiosInstance
private serializer: GenericActionAttachmentSerializer
private serializer: IGenericActionAttachmentSerializer
constructor (axiosInstance: AxiosInstance) {
this.axiosApi = axiosInstance
......
......@@ -210,7 +210,7 @@ describe('GenericActionAttachmentSerializer', () => {
describe('constructing and types', () => {
it('should return \'device\' as its type', () => {
const serializer = new GenericDeviceActionAttachmentSerializer()
expect(serializer.type).toEqual('device')
expect(serializer.targetType).toEqual('device')
})
it('should return a correct action type name', () => {
const serializer = new GenericDeviceActionAttachmentSerializer()
......@@ -291,7 +291,7 @@ describe('GenericActionAttachmentSerializer', () => {
describe('constructing and types', () => {
it('should return \'platform\' as its type', () => {
const serializer = new GenericPlatformActionAttachmentSerializer()
expect(serializer.type).toEqual('platform')
expect(serializer.targetType).toEqual('platform')
})
it('should return a correct action type name', () => {
const serializer = new GenericPlatformActionAttachmentSerializer()
......
......@@ -696,7 +696,7 @@ describe('GenericActionSerializer', () => {
describe('constructing and types', () => {
it('should return \'device\' as its type', () => {
const serializer = new GenericDeviceActionSerializer()
expect(serializer.type).toEqual('device')
expect(serializer.targetType).toEqual('device')
})
it('should return a correct action type name', () => {
const serializer = new GenericDeviceActionSerializer()
......@@ -934,7 +934,7 @@ describe('GenericActionSerializer', () => {
describe('constructing and types', () => {
it('should return \'platform\' as its type', () => {
const serializer = new GenericPlatformActionSerializer()
expect(serializer.type).toEqual('platform')
expect(serializer.targetType).toEqual('platform')
})
it('should return a correct action type name', () => {
const serializer = new GenericPlatformActionSerializer()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment