Skip to content

Commit

Permalink
fix: optional chaining on custom libs
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Oct 25, 2023
1 parent 137b5ac commit 82a777b
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
node-version: 18.x
cache: pnpm

- run: pnpm install --frozen-lockfile
- run: pnpm run build
- run: pnpm install
- run: pnpm build
1 change: 1 addition & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/entities/conversation.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection, Entity, ManyToMany, OneToMany, Property } from "@mikro-orm/core";
import { BaseEntity } from "@common/database";
import { Collection, Entity, ManyToMany, OneToMany, Property } from "@mikro-orm/core";
import { Message, User } from "./index";

@Entity()
Expand All @@ -16,7 +16,7 @@ export class Conversation extends BaseEntity {
})
messages = new Collection<Message>(this);

constructor(partial?: Partial<Comment>) {
constructor(partial?: Partial<Conversation>) {
super();
Object.assign(this, partial);
}
Expand Down
2 changes: 1 addition & 1 deletion src/entities/message.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Message extends BaseEntity {
@Property()
readAt?: Date;

constructor(partial?: Partial<Comment>) {
constructor(partial?: Partial<Message>) {
super();
Object.assign(this, partial);
}
Expand Down
29 changes: 14 additions & 15 deletions src/entities/post.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,47 @@ import { Category, Comment, Tag } from "./index";
@Entity()
export class Post extends BaseEntity {
@Property({ index: true })
slug?: string;
slug?: string;

@Property({ index: true })
title!: string;
title!: string;

@Property({ type: "text" })
description!: string;
description!: string;

@Property({ type: "text" })
content!: string;
content!: string;

@Property()
readingTime? = 0;
readingTime?= 0;

@Property()
readCount? = 0;
readCount?= 0;

@Property()
favoritesCount? = 0;
favoritesCount?= 0;

@ManyToOne({
eager: false,
index: true,
})
author!: Rel<Ref<User>>;
author!: Rel<Ref<User>>;

@OneToMany(() => Comment, comment => comment.post, {
eager: false,
orphanRemoval: true,
nullable: true,
})
comments = new Collection<Comment>(this);
comments = new Collection<Comment>(this);

@ManyToMany(() => Tag, "posts", { owner: true })
tags = new Collection<Tag>(this);
tags = new Collection<Tag>(this);

@ManyToMany(() => Category, "posts", { owner: true })
categories = new Collection<Category>(this);
categories = new Collection<Category>(this);

@Enum({ items: () => PostStateEnum })
state? = PostStateEnum.DRAFT;
state?= PostStateEnum.DRAFT;

constructor(partial?: Partial<Post>) {
super();
Expand All @@ -75,9 +75,8 @@ export class Post extends BaseEntity {
async generateSlug(arguments_: EventArgs<this>) {
if (arguments_.changeSet?.payload?.title) {
this.slug
= `${slugify(this.title)
}-${
Math.trunc(Math.random() * 36 ** 6).toString(36)}`;
= `${slugify(this.title)
}-${Math.trunc(Math.random() * 36 ** 6).toString(36)}`;
}
this.readingTime = this.getReadingTime(this.content);
}
Expand Down
5 changes: 5 additions & 0 deletions src/entities/referral.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ export class Referral extends BaseEntity {
@Index()
@Enum(() => ReferralStatus)
status?: ReferralStatus = ReferralStatus.PENDING;

constructor(partial?: Partial<Referral>) {
super();
Object.assign(this, partial);
}
}
6 changes: 6 additions & 0 deletions src/entities/subscribers.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ import { BaseEntity } from "@common/database";
export class Subscriber extends BaseEntity {
@Property({ index: true, unique: true })
email!: string;


constructor(partial?: Partial<Subscriber>) {
super();
Object.assign(this, partial);
}
}
50 changes: 25 additions & 25 deletions src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,71 +21,71 @@ import { Conversation, Post } from "@entities";
@Embeddable()
export class Social {
@Property()
twitter?: string;
twitter?: string;

@Property()
facebook?: string;
facebook?: string;

@Property()
linkedin?: string;
linkedin?: string;
}

@Entity()
export class User extends BaseEntity {
@Property()
firstName!: string;
firstName!: string;

@Property()
middleName?: string;
middleName?: string;

@Property()
lastName!: string;
lastName!: string;

@Property({ index: true, unique: true })
username!: string;
username!: string;

@Property({ index: true, unique: true })
email!: string;
email!: string;

@Property({ columnType: "text" })
bio!: string;
bio!: string;

@Property({ columnType: "text" })
avatar!: string;
avatar!: string;

@Property({ hidden: true, columnType: "text", lazy: true })
password!: string;
password!: string;

@Property()
twoFactorSecret?: string;
twoFactorSecret?: string;

@Property()
isTwoFactorEnabled? = false;
isTwoFactorEnabled?= false;

@Enum({ items: () => Roles, array: true })
roles?: Roles[] = [Roles.AUTHOR];
roles?: Roles[] = [Roles.AUTHOR];

@Property({ index: true, unique: true })
mobileNumber?: string;
mobileNumber?: string;

@Property()
isVerified? = false;
isVerified?= false;

@OneToMany(() => Post, post => post.author, {
orphanRemoval: true,
eager: false,
nullable: true,
})
posts = new Collection<Post>(this);
posts = new Collection<Post>(this);

@ManyToMany(() => Conversation, "users", { owner: true })
conversations = new Collection<Conversation>(this);
conversations = new Collection<Conversation>(this);

@ManyToMany({ hidden: true })
favorites = new Collection<Post>(this);
favorites = new Collection<Post>(this);

@Embedded(() => Social, { object: true, nullable: true })
social?: Social;
social?: Social;

@ManyToMany({
entity: () => User,
Expand All @@ -96,13 +96,13 @@ export class User extends BaseEntity {
inverseJoinColumn: "following",
hidden: true,
})
followers = new Collection<User>(this);
followers = new Collection<User>(this);

@ManyToMany(() => User, u => u.followers)
followed = new Collection<User>(this);
followed = new Collection<User>(this);

@Property()
lastLogin? = new Date();
lastLogin?= new Date();

constructor(data?: Pick<User, "idx">) {
super();
Expand All @@ -113,8 +113,8 @@ export class User extends BaseEntity {
const o = wrap<User>(this).toObject();

o.avatar
= this.avatar
|| `https://ui-avatars.com/api/?name=${this.firstName}+${this.lastName}&background=0D8ABC&color=fff`;
= this.avatar
|| `https://ui-avatars.com/api/?name=${this.firstName}+${this.lastName}&background=0D8ABC&color=fff`;

return o;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/mailer/mailer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class MailerService {
switchMap((html) => {
mailOptions.html = html;

if (this.options.previewEmail === true) {
if (this.options?.previewEmail) {
try {
previewEmail(mailOptions);
}
Expand All @@ -88,7 +88,7 @@ export class MailerService {
}

return from(this.transporter.sendMail(mailOptions)).pipe(
retry(this.options.retryAttempts),
retry(this.options?.retryAttempts ?? 1),
);
}),
);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/twilio/twilio.options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface TwilioModuleOptions {
import type { ClientOpts as IClientOptions } from "twilio";

export interface TwilioModuleOptions extends IClientOptions{
accountSid: string
authToken: string
from: string
retryAttempts?: number
}
14 changes: 7 additions & 7 deletions src/lib/twilio/twilio.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { TwilioModuleOptions } from "@lib/twilio/twilio.options";
import { Inject, Injectable, Logger } from "@nestjs/common";
import { omit } from "helper-fns";
import type { Observable } from "rxjs";
import { catchError, from, retry, tap, throwError } from "rxjs";
import { catchError, from, tap, throwError } from "rxjs";
import twilio from "twilio";
import type {
MessageInstance,
MessageListInstanceCreateOptions,
} from "twilio/lib/rest/api/v2010/account/message";
import { TwilioModuleOptions } from "@lib/twilio/twilio.options";
import { MODULE_OPTIONS_TOKEN } from "./twilio.module-definition";

@Injectable()
export class TwilioService {
private readonly logger = new Logger(TwilioService.name);

constructor(
@Inject(MODULE_OPTIONS_TOKEN)
private readonly options: TwilioModuleOptions,
) {}
@Inject(MODULE_OPTIONS_TOKEN)
private readonly options: TwilioModuleOptions,
) { }

/**
* It takes in an options object, creates a Twilio client, and returns an observable of the message
Expand All @@ -28,7 +29,7 @@ export class TwilioService {
sendSms(
options: MessageListInstanceCreateOptions & { prefix: string },
): Observable<MessageInstance> {
const client = twilio(this.options.accountSid, this.options.authToken);
const client = twilio(this.options.accountSid, this.options.authToken, omit(this.options, ["accountSid", "authToken", "from"]));

return from(
client.messages.create({
Expand All @@ -37,7 +38,6 @@ export class TwilioService {
to: `${options.prefix}${options.to}`,
}),
).pipe(
retry(this.options.retryAttempts),
tap(message => this.logger.log(`SMS sent to ${message.sid}`)),
catchError((error: Error) => {
this.logger.error(error);
Expand Down
6 changes: 1 addition & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
]
},
"types": [
"./src/common/@types/typings/global.d.ts",
"./src/common/@types/typings/global.d.ts"
],
"declaration": true,
"outDir": "dist"
Expand All @@ -32,9 +32,5 @@
"test/**/*",
"src/**/*",
"eslint.config.js"
],
"exclude": [
"node_modules",
"dist"
]
}

0 comments on commit 82a777b

Please sign in to comment.