Skip to content

Commit

Permalink
style(*): change prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
MM25Zamanian committed Oct 9, 2024
1 parent 7c49b3c commit b5416f5
Show file tree
Hide file tree
Showing 24 changed files with 260 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
uses: oven-sh/setup-bun@v1

- name: 🛒 Install Dependencies 🛒
run: bun install
run: bun install --frozen-lockfile

- name: 🔨 Build Project 🔨
run: bun run build:test
Expand Down
6 changes: 3 additions & 3 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
semi: false
semi: true
singleQuote: true
trailingComma: all
arrowParens: always
printWidth: 120
printWidth: 100
tabWidth: 2
useTabs: false
endOfLine: lf
quoteProps: as-needed
jsxSingleQuote: false
insertPragma: false
requirePragma: false
proseWrap: preserve
proseWrap: always
htmlWhitespaceSensitivity: ignore
overrides:
- files: '*.json'
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["gecut", "Jsonify", "Jsonize", "promeet", "Virtuals", "Zamanian"]
"cSpell.words": ["gecut", "hono", "Jsonify", "Jsonize", "promeet", "Virtuals", "Zamanian"]
}
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions packages/common/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@promeet/database",
"private": true,
"type": "module",
"main": "./src/index.ts",
"dependencies": {
"@promeet/schemas": "workspace:^",
"@promeet/types": "workspace:^",
Expand Down
61 changes: 41 additions & 20 deletions packages/common/database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GecutLogger } from '@gecut/logger'
import { $GroupSchema, $SessionSchema, $UserSchema } from '@promeet/schemas'
import mongoose from 'mongoose'
import { GecutLogger } from '@gecut/logger';
import { $GroupSchema, $SessionSchema, $UserSchema } from '@promeet/schemas';
import mongoose from 'mongoose';

import type { GroupInterface, SessionInterface, UserInterface } from '@promeet/types'
import type { GroupInterface, SessionInterface, UserInterface } from '@promeet/types';

export class Database {
constructor(
Expand All @@ -14,49 +14,70 @@ export class Database {
dbName: 'test',

...(this.options ?? {}),
}
};
}

$user = mongoose.model<UserInterface>('user', $UserSchema)
$group = mongoose.model<GroupInterface>('group', $GroupSchema)
$session = mongoose.model<SessionInterface>('session', $SessionSchema)
$user = mongoose.model<UserInterface>('user', $UserSchema);
$group = mongoose.model<GroupInterface>('group', $GroupSchema);
$session = mongoose.model<SessionInterface>('session', $SessionSchema);

connector?: typeof mongoose
connector?: typeof mongoose;

async connect() {
if (this.uri.startsWith('mongodb://') || this.uri.startsWith('mongodb+srv://')) {
this.logger.method?.('connect')
this.logger.method?.('connect');

try {
return (this.connector = await mongoose.connect(this.uri, this.options))
return (this.connector = await mongoose.connect(this.uri, this.options));
}
catch (error) {
return this.logger.error('connect', 'connect_failed', error)
return this.logger.error('connect', 'connect_failed', error);
}
}

this.logger.error('connect', 'uri_not_valid', { uri: this.uri, options: this.options })
this.logger.error('connect', 'uri_not_valid', { uri: this.uri, options: this.options });

return null
return null;
}

async initialize() {
if (this.connector == null) return -1
if (this.connector == null) return -1;

let total = await this.totalDocuments();

if (total.user + total.group + total.session < 1) {
await this.$user.db.dropDatabase();

await this.createDefaults();

const total = await this.totalDocuments()
total = await this.totalDocuments();
}

this.logger.methodFull?.('initialize', '', total)
this.logger.methodFull?.('initialize', '', total);

return total
return total;
}

private async totalDocuments() {
const [user, group, session] = await Promise.all([
this.$user.countDocuments(),
this.$group.countDocuments(),
this.$session.countDocuments(),
])
]);

return { user, group, session };
}

private async createDefaults() {
const [user] = await Promise.all([
this.$user.create({
firstName: 'سید محمدمهدی',
lastName: 'زمانیان',
phoneNumber: '09155595488',
email: '[email protected]',
}),
]);

return { user, group, session }
return { user };
}
}
8 changes: 4 additions & 4 deletions packages/common/schemas/src/group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, Types } from 'mongoose'
import { Schema, Types } from 'mongoose';

import type {
GroupInterface,
Expand All @@ -7,9 +7,9 @@ import type {
GroupInterfaceStatics,
GroupInterfaceInstanceMethods,
GroupInterfaceQueryHelpers,
} from '@promeet/types'
} from '@promeet/types';

const hour = 1000 * 60 * 60
const hour = 1000 * 60 * 60;

export const $GroupSchema = new Schema<
GroupInterface,
Expand All @@ -35,4 +35,4 @@ export const $GroupSchema = new Schema<
{
timestamps: true,
},
)
);
6 changes: 3 additions & 3 deletions packages/common/schemas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './group'
export * from './user'
export * from './session'
export * from './group';
export * from './user';
export * from './session';
22 changes: 11 additions & 11 deletions packages/common/schemas/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, Types } from 'mongoose'
import { Schema, Types } from 'mongoose';

import type {
SessionInterface,
Expand All @@ -7,7 +7,7 @@ import type {
SessionInterfaceStatics,
SessionInterfaceInstanceMethods,
SessionInterfaceQueryHelpers,
} from '@promeet/types'
} from '@promeet/types';

export const $SessionSchema = new Schema<
SessionInterface,
Expand All @@ -21,11 +21,11 @@ export const $SessionSchema = new Schema<
startedAt: {
type: Date,
default: () => {
const now = new Date()
const now = new Date();

now.setHours(18)
now.setHours(18);

return now
return now;
},
},
length: {
Expand All @@ -39,20 +39,20 @@ export const $SessionSchema = new Schema<
timestamps: true,
methods: {
async makeFromGroup(startDate) {
const _this = await this.populate('group')
const _this = await this.populate('group');

this.startedAt = new Date(startDate + _this.group.defaultSessionStartedAt)
this.length = _this.group.defaultSessionLength
this.startedAt = new Date(startDate + _this.group.defaultSessionStartedAt);
this.length = _this.group.defaultSessionLength;

return await this.save()
return await this.save();
},
},
virtuals: {
endedAt: {
get() {
return new Date(this.startedAt.getTime() + this.length)
return new Date(this.startedAt.getTime() + this.length);
},
},
},
},
)
);
27 changes: 14 additions & 13 deletions packages/common/schemas/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomNumber } from '@gecut/utilities/data-types/number.js'
import { uid } from '@gecut/utilities/uid.js'
import { Schema } from 'mongoose'
import { randomNumber } from '@gecut/utilities/data-types/number.js';
import { uid } from '@gecut/utilities/uid.js';
import { Schema } from 'mongoose';

import type {
UserInterface,
Expand All @@ -9,7 +9,7 @@ import type {
UserInterfaceStatics,
UserInterfaceInstanceMethods,
UserInterfaceQueryHelpers,
} from '@promeet/types'
} from '@promeet/types';

export const $UserSchema = new Schema<
UserInterface,
Expand Down Expand Up @@ -48,31 +48,32 @@ export const $UserSchema = new Schema<
timestamps: true,
methods: {
makeToken() {
this.token = uid()
this.token = uid();

this.save()
this.save();

return this.token
return this.token;
},
makeOTP() {
if (this.otp?.code == null || Date.now() > new Date(this.otp.expiredAt).getTime()) delete this.otp
if (this.otp?.code == null || Date.now() > new Date(this.otp.expiredAt).getTime())
delete this.otp;

this.otp = {
code: randomNumber(999999, 111111).toString(),
expiredAt: new Date(Date.now() + 300000),
}
};

this.save()
this.save();

return this.otp.code
return this.otp.code;
},
},
virtuals: {
fullName: {
get() {
return this.firstName + ' ' + this.lastName
return this.firstName + ' ' + this.lastName;
},
},
},
},
)
);
16 changes: 8 additions & 8 deletions packages/common/types/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { ArrayValues, Jsonify } from '@gecut/types'
import type { Types, Document } from 'mongoose'
import type { ArrayValues, Jsonify } from '@gecut/types';
import type { Types, Document } from 'mongoose';

export const Models = ['user', 'group', 'session'] as const
export type ModelsType = ArrayValues<typeof Models>
export const Models = ['user', 'group', 'session'] as const;
export type ModelsType = ArrayValues<typeof Models>;

export interface Entity extends Document {
_id: Types.ObjectId
_id: Types.ObjectId;

disabled?: boolean
createdAt: Date
disabled?: boolean;
createdAt: Date;
}

export type Jsonize<T extends Entity> = Jsonify<Omit<T, keyof Document> & { _id: string }>
export type Jsonize<T extends Entity> = Jsonify<Omit<T, keyof Document> & { _id: string }>;
18 changes: 9 additions & 9 deletions packages/common/types/src/group.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Entity, Jsonize } from './core'
import type { UserInterface } from './user'
import type { Model } from 'mongoose'
import type { Entity, Jsonize } from './core';
import type { UserInterface } from './user';
import type { Model } from 'mongoose';

export interface GroupInterface extends Entity {
teachers: UserInterface[]
students: UserInterface[]
teachers: UserInterface[];
students: UserInterface[];

defaultSessionStartedAt: number
defaultSessionLength: number
defaultSessionStartedAt: number;
defaultSessionLength: number;
}

export interface GroupInterfaceQueryHelpers {}
Expand All @@ -20,6 +20,6 @@ export type GroupModel = Model<
GroupInterfaceQueryHelpers,
GroupInterfaceInstanceMethods,
GroupInterfaceVirtuals
>
>;

export type GroupData = Jsonize<GroupInterface & GroupInterfaceVirtuals>
export type GroupData = Jsonize<GroupInterface & GroupInterfaceVirtuals>;
6 changes: 3 additions & 3 deletions packages/common/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './group'
export * from './user'
export * from './session'
export * from './group';
export * from './user';
export * from './session';
20 changes: 10 additions & 10 deletions packages/common/types/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { Entity, Jsonize } from './core'
import type { GroupInterface } from './group'
import type { Model } from 'mongoose'
import type { Entity, Jsonize } from './core';
import type { GroupInterface } from './group';
import type { Model } from 'mongoose';

export interface SessionInterface extends Entity {
startedAt: Date
length: number
startedAt: Date;
length: number;

group: GroupInterface
group: GroupInterface;
}

export interface SessionInterfaceQueryHelpers {}
export interface SessionInterfaceInstanceMethods {
makeFromGroup(startDate: number): Promise<SessionInterface>
makeFromGroup(startDate: number): Promise<SessionInterface>;
}
export interface SessionInterfaceVirtuals {
readonly endedAt: Date
readonly endedAt: Date;
}
export interface SessionInterfaceStatics {}

Expand All @@ -23,6 +23,6 @@ export type SessionModel = Model<
SessionInterfaceQueryHelpers,
SessionInterfaceInstanceMethods,
SessionInterfaceVirtuals
>
>;

export type SessionData = Jsonize<SessionInterface & SessionInterfaceVirtuals>
export type SessionData = Jsonize<SessionInterface & SessionInterfaceVirtuals>;
Loading

0 comments on commit b5416f5

Please sign in to comment.