Skip to content

Commit

Permalink
fix: Location not complete of create and update
Browse files Browse the repository at this point in the history
# Problems
- Miss id in location, i.e. http://example.com/Paitent

# Solutions
- Append id in location
  • Loading branch information
Chinlinlee committed Oct 7, 2023
1 parent e76139e commit adf9d1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 3 additions & 1 deletion api/FHIRApiService/services/create.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { BaseFhirApiService } = require("./base.service");


const { logger } = require("@root/utils/log");
const { urlJoin } = require("@root/utils/url");


class CreateService extends BaseFhirApiService {
Expand Down Expand Up @@ -49,8 +50,9 @@ class CreateService extends BaseFhirApiService {

doSuccessResponse(resource) {
let reqBaseUrl = `${this.request.protocol}://${this.request.get('host')}/`;
let fullAbsoluteUrl = new URL(this.request.originalUrl, reqBaseUrl).href;
let fullAbsoluteUrl = urlJoin(`${this.request.originalUrl}/${resource.id}`, reqBaseUrl);
this.response.set("Location", fullAbsoluteUrl);

this.response.append("Last-Modified", (new Date()).toUTCString());
logger.info(`[Info: create id: ${resource.id} successfully] [Resource Type: ${this.resourceType}]`);
return this.doResponse(201, resource);
Expand Down
18 changes: 9 additions & 9 deletions api/FHIRApiService/services/update.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
const { BaseFhirApiService } = require("./base.service");

const { logger } = require("@root/utils/log");
const { urlJoin } = require("@root/utils/url");

class UpdateService extends BaseFhirApiService {
constructor(req, res, resourceType) {
Expand Down Expand Up @@ -38,11 +39,10 @@ class UpdateService extends BaseFhirApiService {
}

doSuccessResponse(resource) {
if (resource.code === 201) {
let reqBaseUrl = `${this.request.protocol}://${this.request.get("host")}/`;
let fullAbsoluteUrl = new URL(this.request.originalUrl, reqBaseUrl).href;
this.response.set("Location", fullAbsoluteUrl);
}
let reqBaseUrl = `${this.request.protocol}://${this.request.get("host")}/`;
let fullAbsoluteUrl = urlJoin(this.request.originalUrl, reqBaseUrl);
this.response.set("Location", fullAbsoluteUrl);

this.response.append("Last-Modified", new Date().toUTCString());
return this.doResponse(resource.code, resource.doc);
}
Expand All @@ -51,7 +51,7 @@ class UpdateService extends BaseFhirApiService {
this.doResourceChangeFailureResponse(err, code);
}

static async insertOrUpdateResource(resourceType, resource, id, session=undefined) {
static async insertOrUpdateResource(resourceType, resource, id, session = undefined) {
let docExist = await UpdateService.isDocExist(resourceType, id);
if (docExist.status === 1) {
return await UpdateService.updateResource(resourceType, id, resource, session);
Expand All @@ -60,7 +60,7 @@ class UpdateService extends BaseFhirApiService {
}
}

static async updateResource(resourceType, id, resource, session=undefined) {
static async updateResource(resourceType, id, resource, session = undefined) {
delete resource.id;
renameCollectionFieldName(resource);
resource.id = id;
Expand All @@ -86,11 +86,11 @@ class UpdateService extends BaseFhirApiService {
};
}

static async insertResourceWithId(resourceType, id, resource, session=undefined) {
static async insertResourceWithId(resourceType, id, resource, session = undefined) {
resource.id = id;
renameCollectionFieldName(resource);
let resourceInstance = new mongoose.model(resourceType)(resource);
let doc = await resourceInstance.save({session});
let doc = await resourceInstance.save({ session });
return {
status: true,
code: 201,
Expand Down

0 comments on commit adf9d1f

Please sign in to comment.