Skip to content

Commit

Permalink
Merge pull request #3351 from energywebfoundation/fix/ptt-issues
Browse files Browse the repository at this point in the history
fix: irec uploading files and synchronizing certificates
  • Loading branch information
soanvig authored Apr 28, 2022
2 parents 4403871 + 503e03a commit 44bb904
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class DeviceService {
);
fileIds = await this.irecService.uploadFiles(
user.organizationId,
list.map((file) => file.data)
list.map((file) => ({ data: file.data, filename: file.filename }))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ export class IrecMockService implements IIrecService {
params: CreateAccountParams
): Promise<void> {}

async uploadFiles(user: UserIdentifier, files: Buffer[] | Blob[] | ReadStream[]) {
async uploadFiles(
user: UserIdentifier,
files: { data: Buffer | Blob | ReadStream; filename: string }[]
) {
return files.map(() => randomString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export interface IIrecService {

getIssueRequest(user: UserIdentifier, code: string): Promise<IssueWithStatus>;

uploadFiles(user: UserIdentifier, files: Buffer[] | Blob[] | ReadStream[]): Promise<string[]>;
uploadFiles(
user: UserIdentifier,
files: { data: Buffer | Blob | ReadStream; filename: string }[]
): Promise<string[]>;

verifyIssueRequest(user: UserIdentifier, issueRequestCode: string): Promise<void>;

Expand Down Expand Up @@ -374,7 +377,7 @@ export class IrecService implements IIrecService {

async uploadFiles(
user: UserIdentifier,
files: Buffer[] | Blob[] | ReadStream[]
files: { data: Buffer | Blob | ReadStream; filename: string }[]
): Promise<string[]> {
const irecClient = await this.getIrecClient(user);
return irecClient.file.upload(files);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,15 @@ export class IRECAPIClient {
const fileManagementUrl = `${this.endPointUrl}/api/irec/v1/file-management`;

return {
upload: async (files: Buffer[] | Blob[] | ReadStream[]): Promise<string[]> => {
upload: async (
files: { data: Buffer | Blob | ReadStream; filename: string }[]
): Promise<string[]> => {
const url = `${fileManagementUrl}/upload`;

const data = new FormData();
files.forEach((file: Buffer | Blob | ReadStream) => data.append('files', file));
files.forEach((file) =>
data.append('files', file.data, { filename: file.filename })
);

const headers = data.getHeaders();
const response = await this.axiosInstance.post<{ file_uids: string[] }>(url, data, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('API flows', () => {

it('should pass create and approve device flow', async () => {
const file = fs.createReadStream(`${__dirname}/file-sample_150kB.pdf`);
const [fileId] = await registrantClient.file.upload([file]);
const [fileId] = await registrantClient.file.upload([{ data: file, filename: 'test' }]);

const params = getDeviceParams();
const createdDevice: Device = await registrantClient.device.create({
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('API flows', () => {

it('should pass create and approve issue flow', async () => {
const file = fs.createReadStream(`${__dirname}/file-sample_150kB.pdf`);
const [fileId] = await registrantClient.file.upload([file]);
const [fileId] = await registrantClient.file.upload([{ data: file, filename: 'test' }]);

const params = getDeviceParams();
const device: Device = await registrantClient.device.create(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class CheckCertificationRequestStateTask {
const user = await this.userService.findOne(certificateRequest.userId);
const irecIssue = await this.irecService.getIssueRequest(
user.organization.id,
certificateRequest.irecIssueId
certificateRequest.irecIssueRequestId
);

if (!irecIssue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class CreateIrecCertificationRequestHandler
);
fileIds = await this.irecService.uploadFiles(
user.organizationId,
list.map((file) => file.data)
list.map((file) => ({ data: file.data, filename: file.filename }))
);
}

Expand Down

0 comments on commit 44bb904

Please sign in to comment.