Skip to content

Commit

Permalink
feat: users schema의 community innerschema 수정 #115
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon committed Nov 25, 2022
1 parent 779ede1 commit 20f1953
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
24 changes: 12 additions & 12 deletions server/apps/api/src/community/community.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ export class CommunityController {
}
}

@Get()
@UseGuards(JwtAccessGuard)
async getCommunities(@Req() req: any) {
try {
const _id = req.user._id;
const result = await this.communityService.getCommunities(_id);
return responseForm(200, result);
} catch (error) {
this.logger.error(JSON.stringify(error.response));
throw error;
}
}
// @Get()
// @UseGuards(JwtAccessGuard)
// async getCommunities(@Req() req: any) {
// try {
// const _id = req.user._id;
// const result = await this.communityService.getCommunities(_id);
// return responseForm(200, result);
// } catch (error) {
// this.logger.error(JSON.stringify(error.response));
// throw error;
// }
// }
}
7 changes: 3 additions & 4 deletions server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ export class CommunityService {
...createCommunityDto,
users: [createCommunityDto.managerId],
});
await this.userRepository.appendElementAtArr(
{ _id: createCommunityDto.managerId },
{ communities: community._id.toString() },
);
const newCommunity = {};
newCommunity[`communities.${community._id.toString()}`] = { _id: community._id.toString() };
await this.userRepository.updateObject({ _id: createCommunityDto.managerId }, newCommunity);
return community;
}

Expand Down
10 changes: 10 additions & 0 deletions server/dao/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class UserRepository {
await this.userModel.updateOne(filter, { $push: appendElement });
}

async updateObject(filter, appendElement) {
await this.userModel.updateOne(filter, { $set: appendElement });
}

async deleteElementAtArr(filter, removeElement) {
await this.userModel.updateOne(filter, { $pullAll: removeElement });
}
Expand All @@ -46,4 +50,10 @@ export class UserRepository {
addArr[attribute] = { $each: appendArr };
return await this.userModel.findByIdAndUpdate(filter, { $addToSet: addArr }, { new: true });
}

// async set(filter, obj) {
// const user = new User();
//
// this.userModel.find(filter).communities.set();
// }
}
25 changes: 15 additions & 10 deletions server/dao/schemas/user.schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Prop, raw, Schema, SchemaFactory } from '@nestjs/mongoose';
import { IsIn, IsString } from 'class-validator';
import mongoose, { Document } from 'mongoose';
import { STATUS } from '@utils/def';

const channels = {
_id: { type: String },
lastRead: { type: Date, default: new Date() },
};

export type UserDocument = User & Document;

@Schema()
@Schema({ timestamps: true })
export class User {
@Prop()
name: string;
Expand Down Expand Up @@ -49,12 +54,6 @@ export class User {
@Prop({ default: new Date(), type: mongoose.Schema.Types.Date })
lastLogin: Date;

@Prop({ default: new Date(), type: mongoose.Schema.Types.Date })
createdAt: Date;

@Prop({ default: new Date(), type: mongoose.Schema.Types.Date })
updatedAt: Date;

@Prop({ type: mongoose.Schema.Types.Date })
deletedAt: Date;

Expand All @@ -66,7 +65,13 @@ export class User {
@IsString()
followers: string[];

@Prop()
communities: string[];
@Prop(
raw({
type: Map,
of: new mongoose.Schema({ _id: { type: String }, channels: { type: Map, of: Date } }),
}),
)
communities; //: { type: Map<string, any> };
}

export const UserSchema = SchemaFactory.createForClass(User);

0 comments on commit 20f1953

Please sign in to comment.