Skip to content

Commit

Permalink
Merge pull request #83 from codex-team/chore/rename-enabled-to-ispublic
Browse files Browse the repository at this point in the history
chore: renamed note settings enabled to isPublic
  • Loading branch information
slaveeks authored Oct 21, 2023
2 parents 50b32fe + 2bd9176 commit 9bc6420
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 56 deletions.
2 changes: 2 additions & 0 deletions migrations/tenant/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE IF EXISTS public.note_settings
RENAME COLUMN enabled TO is_public;
2 changes: 1 addition & 1 deletion src/domain/entities/noteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default interface NoteSettings {
/**
* Is note public for everyone or only for collaborators
*/
enabled: boolean;
isPublic: boolean;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/domain/service/noteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export default class NoteSettingsService {
* Adds note settings
*
* @param noteId - note id
* @param enabled - is note enabled
* @param isPublic - is note public
* @returns added note settings
*/
public async addNoteSettings(noteId: NoteInternalId, enabled: boolean = true): Promise<NoteSettings> {
public async addNoteSettings(noteId: NoteInternalId, isPublic: boolean = true): Promise<NoteSettings> {
return await this.repository.addNoteSettings({
noteId: noteId,
enabled: enabled,
isPublic: isPublic,
});
}

Expand Down
5 changes: 4 additions & 1 deletion src/presentation/http/router/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ const NoteRouter: FastifyPluginCallback<NoteRouterOptions> = (fastify, opts, don

const noteSettings = await noteSettingsService.getNoteSettingsByNoteId(note.id);

if (noteSettings?.enabled === true) {
/**
* Check if note is public or user is owner
*/
if (noteSettings.isPublic || note.creatorId === request.userId) {
return reply.send(note);
}

Expand Down
2 changes: 1 addition & 1 deletion src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface NoteListRouterOptions {
* @param done - callback
*/
const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, opts, done) => {

const noteListService = opts.noteListService;

/**
Expand All @@ -41,4 +40,5 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (fastify, o

done();
};

export default NoteListRouter;
7 changes: 3 additions & 4 deletions src/presentation/http/router/noteSettings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { FastifyPluginCallback } from 'fastify';
import type NoteSettingsService from '@domain/service/noteSettings.js';
import type { NoteInternalId } from '@domain/entities/note.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import notEmpty from '@infrastructure/utils/notEmpty.js';
import useNoteResolver from '../middlewares/note/useNoteResolver.js';
import type NoteService from '@domain/service/note.js';
import { NotePublicId } from '@domain/entities/note.js';
import type { NotePublicId } from '@domain/entities/note.js';

/**
* Interface for the note settings router.
Expand Down Expand Up @@ -96,15 +95,15 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
/**
* @todo validate data
*/
const { customHostname, enabled } = request.body;
const { customHostname, isPublic } = request.body;

/**
* TODO: check is user collaborator
*/

const updatedNoteSettings = await noteSettingsService.patchNoteSettingsByNoteId(noteId, {
customHostname,
enabled,
isPublic,
});

if (updatedNoteSettings === null) {
Expand Down
59 changes: 15 additions & 44 deletions src/repository/storage/postgres/orm/sequelize/noteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { NoteModel } from '@repository/storage/postgres/orm/sequelize/note.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import type { NoteSettingsCreationAttributes } from '@domain/entities/noteSettings.js';

/* eslint-disable @typescript-eslint/naming-convention */

/**
* Class representing a notes settings model in database
*/
Expand All @@ -19,17 +17,17 @@ export class NoteSettingsModel extends Model<InferAttributes<NoteSettingsModel>,
/**
* Note ID
*/
public declare note_id: NoteSettings['noteId'];
public declare noteId: NoteSettings['noteId'];

/**
* Custom hostname
*/
public declare custom_hostname: CreationOptional<NoteSettings['customHostname']>;
public declare customHostname: CreationOptional<NoteSettings['customHostname']>;

/**
* Is note public
*/
public declare enabled: CreationOptional<NoteSettings['enabled']>;
public declare isPublic: CreationOptional<NoteSettings['isPublic']>;
}

/**
Expand Down Expand Up @@ -73,19 +71,19 @@ export default class NoteSettingsSequelizeStorage {
autoIncrement: true,
primaryKey: true,
},
note_id: {
noteId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: NoteModel.tableName,
key: 'id',
},
},
custom_hostname: {
customHostname: {
type: DataTypes.STRING,
allowNull: true,
},
enabled: {
isPublic: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
Expand Down Expand Up @@ -118,12 +116,7 @@ export default class NoteSettingsSequelizeStorage {
return null;
}

return {
id: noteSettings.id,
noteId: noteSettings.note_id,
enabled: noteSettings.enabled,
customHostname: noteSettings.custom_hostname,
};
return noteSettings;
}

/**
Expand All @@ -135,7 +128,7 @@ export default class NoteSettingsSequelizeStorage {
public async getNoteSettingsByNoteId(noteId: NoteSettings['noteId']): Promise<NoteSettings> {
const settings = await this.model.findOne({
where: {
note_id: noteId,
noteId: noteId,
},
});

Expand All @@ -146,12 +139,7 @@ export default class NoteSettingsSequelizeStorage {
throw new Error('Note settings not found');
}

return {
id: settings.id,
noteId: settings.note_id,
customHostname: settings.custom_hostname,
enabled: settings.enabled,
};
return settings;
}

/**
Expand Down Expand Up @@ -181,21 +169,16 @@ export default class NoteSettingsSequelizeStorage {
public async insertNoteSettings({
noteId,
customHostname,
enabled,
isPublic,
}: NoteSettingsCreationAttributes
): Promise<NoteSettings> {
const settings = await this.model.create({
note_id: noteId,
custom_hostname: customHostname,
enabled: enabled,
noteId,
customHostname,
isPublic,
});

return {
id: settings.id,
noteId: settings.note_id,
customHostname: settings.custom_hostname,
enabled: settings.enabled,
};
return settings;
}

/**
Expand All @@ -214,18 +197,6 @@ export default class NoteSettingsSequelizeStorage {
return null;
}

const values = {
enabled: data.enabled,
custom_hostname: data.customHostname,
};

const updatedSettings = await settingsToUpdate.update(values);

return {
id: updatedSettings.id,
noteId: updatedSettings.note_id,
customHostname: updatedSettings.custom_hostname,
enabled: updatedSettings.enabled,
};
return await settingsToUpdate.update(data);
}
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5999,11 +5999,11 @@ __metadata:

"typescript@patch:typescript@^5.0.4#~builtin<compat/typescript>":
version: 5.2.2
resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin<compat/typescript>::version=5.2.2&hash=f3b441"
resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin<compat/typescript>::version=5.2.2&hash=ad5954"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 0f4da2f15e6f1245e49db15801dbee52f2bbfb267e1c39225afdab5afee1a72839cd86000e65ee9d7e4dfaff12239d28beaf5ee431357fcced15fb08583d72ca
checksum: 07106822b4305de3f22835cbba949a2b35451cad50888759b6818421290ff95d522b38ef7919e70fb381c5fe9c1c643d7dea22c8b31652a717ddbd57b7f4d554
languageName: node
linkType: hard

Expand Down

0 comments on commit 9bc6420

Please sign in to comment.