Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/.../update 호출 시 선택 인덱스를 삭제할 수 있도록 함 #39

Merged
merged 1 commit into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/server/src/config/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const updateConfig = async function(config: ConfigUpdateRequest) {
const attributes: ExpressionAttributeValueMap = {};
const attributeNames: ExpressionAttributeNameMap = {};
let updateExp = '';
let removeExp = '';
if (config.name) {
attributes[':name'] = { S: config.name };
updateExp = 'SET n = :name';
Expand All @@ -183,6 +184,13 @@ export const updateConfig = async function(config: ConfigUpdateRequest) {
attributeNames['#aT'] = 'aT';
updateExp += `${updateExp ? ',' : 'SET'} #aT = :activateTo`;
}
if (config.activateFrom === null) {
removeExp += `${removeExp ? ',' : 'REMOVE'} aF`;
}
if (config.activateTo === null) {
attributeNames['#aT'] = 'aT';
removeExp += `${removeExp ? ',' : 'REMOVE'} #aT`;
}
if ((config as ServiceConfigUpdateRequest).buildings) {
const buildings = (config as ServiceConfigUpdateRequest).buildings;
attributes[':buildings'] = { M: Object.fromEntries(Object.entries(buildings).map(([s, b]) => [s, { M: toBuildingData(b) }])) };
Expand All @@ -199,9 +207,9 @@ export const updateConfig = async function(config: ConfigUpdateRequest) {
type: { S: 'config' },
id: { S: config.id }
},
UpdateExpression: updateExp,
UpdateExpression: updateExp + `${removeExp ? ' ' + removeExp : ''}`,
...(Object.keys(attributeNames).length && { ExpressionAttributeNames: attributeNames }),
ExpressionAttributeValues: attributes
...(Object.keys(attributes).length && { ExpressionAttributeValues: attributes })
};
await dynamoDB.updateItem(req).promise();
return config;
Expand Down
25 changes: 16 additions & 9 deletions packages/server/src/user/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const toUserDao = (user: User): UserDao => ({
...(user.claimedUntil && { cU: { S: user.claimedUntil } })
});

export const getUser = async function(id: string): Promise<User> {
export const getUser = async function (id: string): Promise<User> {
const req: GetItemInput = {
TableName,
Key: {
Expand All @@ -46,7 +46,7 @@ export const getUser = async function(id: string): Promise<User> {
const dao: UserDao = res.Item as unknown as UserDao;
return fromUserDao(dao);
};
export const queryUser = async function(startsWith: string): Promise<Array<User>> {
export const queryUser = async function (startsWith: string): Promise<Array<User>> {
let composedRes: Array<User> = [];
const req: QueryInput = {
TableName,
Expand Down Expand Up @@ -77,9 +77,10 @@ export const queryUser = async function(startsWith: string): Promise<Array<User>
return composedRes;
};

export const updateUser = async function(info: UserUpdateRequest): Promise<UserUpdateRequest> {
export const updateUser = async function (info: UserUpdateRequest): Promise<UserUpdateRequest> {
const attributes: ExpressionAttributeValueMap = {};
let updateExp = '';
let removeExp = '';
if (info.name) {
attributes[':name'] = { S: info.name };
updateExp = 'SET n = :name';
Expand All @@ -100,20 +101,26 @@ export const updateUser = async function(info: UserUpdateRequest): Promise<UserU
attributes[':claimedUntil'] = { S: info.claimedUntil };
updateExp += `${updateExp ? ',' : 'SET'} cU = :claimedUntil`;
}
if (info.lockerId === null) {
removeExp += `${removeExp ? ',' : 'REMOVE'} lockerId`;
}
if (info.claimedUntil === null) {
removeExp += `${removeExp ? ',' : 'REMOVE'} cU`;
}
const req: UpdateItemInput = {
TableName,
Key: {
type: { S: 'user' },
id: { S: `${info.id}` }
},
UpdateExpression: updateExp,
ExpressionAttributeValues: attributes
UpdateExpression: updateExp + `${removeExp ? ' ' + removeExp : ''}`,
...(Object.keys(attributes).length && { ExpressionAttributeValues: attributes })
};
await dynamoDB.updateItem(req).promise();
return info;
};

export const deleteUser = async function(id: string): Promise<string> {
export const deleteUser = async function (id: string): Promise<string> {
const req: DeleteItemInput = {
TableName,
Key: {
Expand All @@ -124,7 +131,7 @@ export const deleteUser = async function(id: string): Promise<string> {
await dynamoDB.deleteItem(req).promise();
return id;
};
export const batchPutUser = async function(infos: Array<User>): Promise<Array<User>> {
export const batchPutUser = async function (infos: Array<User>): Promise<Array<User>> {
if (infos.length === 0) return infos;
if (infos.length > 25) throw new ResponsibleError(500, 'Maximum amount of batch creation is 25');
const requests: WriteRequest[] = infos.map((v: User) => ({
Expand All @@ -143,7 +150,7 @@ export const batchPutUser = async function(infos: Array<User>): Promise<Array<Us
return infos;
};

export const batchDeleteUser = async function(ids: Array<string>): Promise<Array<string>> {
export const batchDeleteUser = async function (ids: Array<string>): Promise<Array<string>> {
if (ids.length === 0) return ids;
if (ids.length > 25) throw new ResponsibleError(500, 'Maximum amount of batch creation is 25');
const requests: WriteRequest[] = ids.map((v: string) => ({
Expand All @@ -165,4 +172,4 @@ export const batchDeleteUser = async function(ids: Array<string>): Promise<Array
const res = await dynamoDB.batchWriteItem(req).promise();
console.log(res);
return ids;
};
};
11 changes: 6 additions & 5 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type UserUpdateRequest = {
name?: string;
isAdmin?: boolean;
department?: string;
lockerId?: string;
claimedUntil?: string;
lockerId?: string | null;
claimedUntil?: string | null;
};

type UserDeleteRequest = {
Expand Down Expand Up @@ -83,7 +83,8 @@ type Building = {
id: string;
name: string;
lockers: {
[floor: string]: { // TODO: Make buildings distinguishable
[floor: string]: {
// TODO: Make buildings distinguishable
[lockerName: string]: LockerSection;
};
};
Expand Down Expand Up @@ -128,8 +129,8 @@ type LockerSubsectionData = {
type ConfigUpdateRequest = {
id: string;
name?: string;
activateFrom?: string;
activateTo?: string;
activateFrom?: string | null;
activateTo?: string | null;
};

type DepartmentConfigUpdateRequest = ConfigUpdateRequest & {
Expand Down