From d135e58a1ade93996c70deba5890d91588e7418e Mon Sep 17 00:00:00 2001 From: Jon Bone Date: Thu, 10 Nov 2016 11:30:44 +0000 Subject: [PATCH] fixed utc conversion bug --- src/decorators/attribute.decorator.ts | 2 +- src/models/json-api.model.spec.ts | 34 +++++++++++++-------------- src/models/json-api.model.ts | 20 +++++++++++----- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/decorators/attribute.decorator.ts b/src/decorators/attribute.decorator.ts index 2733ba87..4bd77f78 100644 --- a/src/decorators/attribute.decorator.ts +++ b/src/decorators/attribute.decorator.ts @@ -7,7 +7,7 @@ export function Attribute(config: any = {}) { let converter = function(dataType: any, value: any, forSerialisation = false): any { if (!forSerialisation) { if (dataType === Date) { - return moment.utc(value).toDate(); + return moment(value).toDate(); } } else { if (dataType === Date) { diff --git a/src/models/json-api.model.spec.ts b/src/models/json-api.model.spec.ts index 7885cf73..cff2d9fb 100644 --- a/src/models/json-api.model.spec.ts +++ b/src/models/json-api.model.spec.ts @@ -35,7 +35,7 @@ describe('JsonApiModel', () => { describe('constructor', () => { - it('should be instanciated with attributes', () => { + it('should be instantiated with attributes', () => { const DATA = { id: '1', attributes: { @@ -48,10 +48,10 @@ describe('JsonApiModel', () => { expect(author).toBeDefined(); expect(author.id).toBe('1'); expect(author.name).toBe('Daniele'); - expect(author.date_of_birth.getTime()).toBe(moment('1987-05-25T00:00:00Z').toDate().getTime()); + expect(author.date_of_birth.getTime()).toBe(moment('1987-05-25').toDate().getTime()); }); - it('should be instanciated without attributes', () => { + it('should be instantiated without attributes', () => { let author: Author = new Author(datastore); expect(author).toBeDefined(); expect(author.id).toBeUndefined(); @@ -69,10 +69,10 @@ describe('JsonApiModel', () => { expect(author).toBeDefined(); expect(author.id).toBe(AUTHOR_ID); expect(author.name).toBe(AUTHOR_NAME); - expect(author.date_of_birth.valueOf()).toBe(moment.utc(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf()); - expect(author.date_of_death.valueOf()).toBe(moment.utc(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf()); - expect(author.created_at.valueOf()).toBe(moment.utc(AUTHOR_CREATED).valueOf()); - expect(author.updated_at.valueOf()).toBe(moment.utc(AUTHOR_UPDATED).valueOf()); + expect(author.date_of_birth.valueOf()).toBe(moment(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf()); + expect(author.date_of_death.valueOf()).toBe(moment(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf()); + expect(author.created_at.valueOf()).toBe(moment(AUTHOR_CREATED).valueOf()); + expect(author.updated_at.valueOf()).toBe(moment(AUTHOR_UPDATED).valueOf()); expect(author.books).toBeUndefined(); }); @@ -86,17 +86,17 @@ describe('JsonApiModel', () => { expect(author).toBeDefined(); expect(author.id).toBe(AUTHOR_ID); expect(author.name).toBe(AUTHOR_NAME); - expect(author.date_of_birth.valueOf()).toBe(moment.utc(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf()); - expect(author.date_of_death.valueOf()).toBe(moment.utc(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf()); - expect(author.created_at.valueOf()).toBe(moment.utc(AUTHOR_CREATED).valueOf()); - expect(author.updated_at.valueOf()).toBe(moment.utc(AUTHOR_UPDATED).valueOf()); + expect(author.date_of_birth.valueOf()).toBe(moment(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf()); + expect(author.date_of_death.valueOf()).toBe(moment(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf()); + expect(author.created_at.valueOf()).toBe(moment(AUTHOR_CREATED).valueOf()); + expect(author.updated_at.valueOf()).toBe(moment(AUTHOR_UPDATED).valueOf()); expect(author.books).toBeDefined(); expect(author.books.length).toBe(BOOK_NUMBER); author.books.forEach((book: Book, index: number) => { expect(book instanceof Book).toBeTruthy(); expect(+book.id).toBe(index + 1); expect(book.title).toBe(BOOK_TITLE); - expect(book.date_published.valueOf()).toBe(moment.utc(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf()); + expect(book.date_published.valueOf()).toBe(moment(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf()); }); }); @@ -125,17 +125,17 @@ describe('JsonApiModel', () => { expect(author).toBeDefined(); expect(author.id).toBe(AUTHOR_ID); expect(author.name).toBe(AUTHOR_NAME); - expect(author.date_of_birth.valueOf()).toBe(moment.utc(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf()); - expect(author.date_of_death.valueOf()).toBe(moment.utc(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf()); - expect(author.created_at.valueOf()).toBe(moment.utc(AUTHOR_CREATED).valueOf()); - expect(author.updated_at.valueOf()).toBe(moment.utc(AUTHOR_UPDATED).valueOf()); + expect(author.date_of_birth.valueOf()).toBe(moment(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf()); + expect(author.date_of_death.valueOf()).toBe(moment(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf()); + expect(author.created_at.valueOf()).toBe(moment(AUTHOR_CREATED).valueOf()); + expect(author.updated_at.valueOf()).toBe(moment(AUTHOR_UPDATED).valueOf()); expect(author.books).toBeDefined(); expect(author.books.length).toBe(BOOK_NUMBER); author.books.forEach((book: Book, index: number) => { expect(book instanceof Book).toBeTruthy(); expect(+book.id).toBe(index + 1); expect(book.title).toBe(BOOK_TITLE); - expect(book.date_published.valueOf()).toBe(moment.utc(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf()); + expect(book.date_published.valueOf()).toBe(moment(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf()); expect(book.chapters).toBeDefined(); expect(book.chapters.length).toBe(CHAPTERS_NUMBER); book.chapters.forEach((chapter: Chapter, cindex: number) => { diff --git a/src/models/json-api.model.ts b/src/models/json-api.model.ts index 791eaec6..9d511e41 100644 --- a/src/models/json-api.model.ts +++ b/src/models/json-api.model.ts @@ -70,9 +70,13 @@ export class JsonApiModel { if (relationship && relationship.data && relationship.data.length > 0) { let typeName: string = relationship.data[0].type; let modelType: ModelType = Reflect.getMetadata('JsonApiDatastoreConfig', this._datastore.constructor).models[typeName]; - let relationshipModel: JsonApiModel[] = this.getHasManyRelationship(modelType, relationship.data, included, typeName, level); - if (relationshipModel.length > 0) { - this[metadata.propertyName] = relationshipModel; + if (modelType) { + let relationshipModel: JsonApiModel[] = this.getHasManyRelationship(modelType, relationship.data, included, typeName, level); + if (relationshipModel.length > 0) { + this[metadata.propertyName] = relationshipModel; + } + } else { + throw {message: 'parseHasMany - Model type for relationship ' + typeName + ' not found.'}; } } } @@ -89,9 +93,13 @@ export class JsonApiModel { if (dataRelationship) { let typeName: string = dataRelationship.type; let modelType: ModelType = Reflect.getMetadata('JsonApiDatastoreConfig', this._datastore.constructor).models[typeName]; - let relationshipModel: JsonApiModel = this.getBelongsToRelationship(modelType, dataRelationship, included, typeName, level); - if (relationshipModel) { - this[metadata.propertyName] = relationshipModel; + if (modelType) { + let relationshipModel: JsonApiModel = this.getBelongsToRelationship(modelType, dataRelationship, included, typeName, level); + if (relationshipModel) { + this[metadata.propertyName] = relationshipModel; + } + } else { + throw { message: 'parseBelongsTo - Model type for relationship ' + typeName + ' not found.' }; } } }