Skip to content

Commit

Permalink
Add organization logo to organization
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
otaviojacobi committed Oct 19, 2023
1 parent 8e1790e commit 2f4a9b1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 8 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@types/chai-as-promised": "^7.1.4",
"@types/lodash": "^4.14.195",
"@types/memoizee": "^0.4.7",
"@types/mime": "^3.0.3",
"@types/mocha": "^10.0.1",
"@types/ndjson": "^2.0.0",
"@types/sinon": "^10.0.6",
Expand Down Expand Up @@ -124,14 +125,15 @@
"balena-auth": "^5.1.0",
"balena-errors": "^4.8.0",
"balena-hup-action-utils": "~5.0.0",
"balena-register-device": "^9.0.1",
"balena-request": "^13.0.0",
"balena-register-device": "9.0.2-build-oj-test-deploy-with-new-balena-request-97726e6112f1bc6a1f3785f7edac31fd44c23caf-1",
"balena-request": "13.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-11b0087dafe086f4d8ff565f8fffa6aa924a909b-1",
"balena-semver": "^2.3.0",
"balena-settings-client": "^5.0.0",
"date-fns": "^2.29.3",
"handlebars": "^4.7.7",
"lodash": "^4.17.21",
"memoizee": "^0.4.15",
"mime": "^3.0.0",
"ndjson": "^2.0.0",
"p-throttle": "^4.1.1",
"pinejs-client-core": "^6.12.0",
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,29 @@ export const getSdk = function ($opts?: SdkOptions) {
* @memberof balena
*
* @description
* The utils instance used internally. This should not be necessary
* in normal usage, but can be useful to handle some specific cases.
* The utils instance offers some convenient features for clients.
*
* @example
* balena.utils.mergePineOptions(
* { $expand: { device: { $select: ['id'] } } },
* { $expand: { device: { $select: ['name'] } } },
* );
*
* @example
* // Creating a new WebResourceFile in case 'File' API is not available.
* new balena.utils.BalenaWebResourceFile(
* [fs.readFileSync('./file.tgz')],
* 'file.tgz'
* );
*/
Object.defineProperty(sdk, 'utils', {
enumerable: true,
configurable: true,
get() {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { mergePineOptions } = require('./util') as typeof import('./util');
return { mergePineOptions };
const { mergePineOptions, BalenaWebResourceFile } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('./util') as typeof import('./util');
return { mergePineOptions, BalenaWebResourceFile };
},
});

Expand Down
25 changes: 25 additions & 0 deletions src/models/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ const getOrganizationModel = function (
* balena.models.organization.create({ name:'MyOrganization' }).then(function(organization) {
* console.log(organization);
* });
*
* @example
* balena.models.organization.create({
* name:'MyOrganization',
* logo_image: new balena.utils.BalenaWebResourceFile(
* [fs.readFileSync('./img.jpeg')],
* 'img.jpeg'
* );
* })
* .then(function(organization) {
* console.log(organization);
* });
*
* @example
* balena.models.organization.create({
* name:'MyOrganization',
* // Only in case File API is avaialable (most browsers and Node 20+)
* logo_image: new File(
* imageContent,
* 'img.jpeg'
* );
* })
* .then(function(organization) {
* console.log(organization);
* });
*/
const create = function (
organization: BalenaSdk.PineSubmitBody<BalenaSdk.Organization>,
Expand Down
2 changes: 2 additions & 0 deletions src/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
OptionalNavigationResource,
ReverseNavigationResource,
ConceptTypeNavigationResource,
WebResource,
} from '../../typings/pinejs-client-core';
import type { AnyObject } from '../../typings/utils';

Expand Down Expand Up @@ -84,6 +85,7 @@ export interface Organization {
handle: string;
has_past_due_invoice_since__date: string | null;
is_frozen: boolean;
logo_image: WebResource;

application: ReverseNavigationResource<Application>;
/** includes__organization_membership */
Expand Down
15 changes: 15 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as errors from 'balena-errors';
import type * as Pine from '../../typings/pinejs-client-core';
import type { IfDefined } from '../../typings/utils';
import type { WebResourceFile } from 'balena-request';
import * as mime from 'mime';

export interface BalenaUtils {
mergePineOptions: typeof mergePineOptions;
BalenaWebResourceFile: typeof BalenaWebResourceFile;
}

export const notImplemented = () => {
Expand Down Expand Up @@ -344,3 +347,15 @@ export const limitedMap = <T, U>(
}
});
};

export class BalenaWebResourceFile extends Blob implements WebResourceFile {
public name: string;
constructor(blobParts: BlobPart[], name: string, options?: BlobPropertyBag) {
const opts = {
...options,
type: options?.type ?? mime.getType(name) ?? undefined,
};
super(blobParts, opts);
this.name = name;
}
}
21 changes: 21 additions & 0 deletions tests/integration/models/organization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
credentials,
givenLoggedInUser,
organizationRetrievalFields,
IS_BROWSER,
} from '../setup';
import { timeSuite } from '../../util';

Expand Down Expand Up @@ -98,6 +99,26 @@ describe('Organization model', function () {
.that.is.not.equal(ctx.newOrg1.handle);
ctx.newOrg2 = org;
});

it.skip('should be able to create an organization with a logo', async function () {
if (IS_BROWSER) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs') as typeof import('fs');
const org = await balena.models.organization.create({
name: 'org-with-logo',
logo_image: new balena.utils.BalenaWebResourceFile(
[fs.readFileSync('orglogo.png')],
'orglogo.png',
),
});

const fetchedOrg = await balena.models.organization.get(org.id, {
$select: ['id', 'logo_image'],
});
expect(fetchedOrg).to.have.property('logo_image').that.is.a('string');
});
});
});

Expand Down
12 changes: 11 additions & 1 deletion typings/pinejs-client-core.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WebResourceFile } from 'balena-request';
import type {
AnyObject,
PropsAssignableWithType,
Expand Down Expand Up @@ -143,6 +144,14 @@ export type PostResult<T> = SelectResultObject<
Exclude<StringKeyof<T>, PropsOfType<T, ReverseNavigationResource<object>>>
>;

export type WebResource = {
filename: string;
href: string;
content_type?: string;
content_disposition?: string;
size?: number;
};

// based on https://github.com/balena-io/pinejs-client-js/blob/master/core.d.ts

type RawFilter =
Expand Down Expand Up @@ -379,10 +388,11 @@ export type ODataOptionsStrict<T> = Omit<
export type ODataOptionsWithFilter<T> = ODataOptions<T> &
Required<Pick<ODataOptions<T>, '$filter'>>;

export type ReplaceWebResource<K> = K extends WebResource ? WebResourceFile : K;
export type SubmitBody<T> = {
[k in keyof T]?: T[k] extends AssociatedResource<object>
? number | null
: T[k];
: ReplaceWebResource<T[k]>;
};

type BaseResourceId =
Expand Down

0 comments on commit 2f4a9b1

Please sign in to comment.